Pages

Thursday, January 20, 2011

Export data in excel formate using java

import  java.io.*;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;

public class CreateExcelFile{
public static void main(String[]args){
try{
String filename="c:/hello.xls" ;
HSSFWorkbook hwb=new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");

HSSFRow rowhead= sheet.createRow((short)0);
rowhead
.createCell((short) 0).setCellValue("SNo");
rowhead
.createCell((short) 1).setCellValue("First Name");
rowhead
.createCell((short) 2).setCellValue("Last Name");
rowhead
.createCell((short) 3).setCellValue("Username");
rowhead
.createCell((short) 4).setCellValue("E-mail");
rowhead
.createCell((short) 5).setCellValue("Country");

HSSFRow row= sheet.createRow((short)1);
row
.createCell((short) 0).setCellValue("1");
row
.createCell((short) 1).setCellValue("jaid");
row
.createCell((short) 2).setCellValue("shaik");
row
.createCell((short) 3).setCellValue(mpowerglobal);
row
.createCell((short) 4).setCellValue("jaid.sk@gmail.com");
row
.createCell((short) 5).setCellValue("India");

FileOutputStream fileOut = new FileOutputStream(filename);
hwb
.write(fileOut);
fileOut
.close();
System.out.println("Your excel file has been generated!");

} catch ( Exception ex ) {
System.out.println(ex);

}
}
}



For the above file you need poi library for that go through this link:
http://www.apache.org/dyn/closer.cgi/poi/release/bin/poi-bin-3.7-20101029.zip