Thursday, August 14, 2008

Writing in MS Excel through JAVA using POI

Whether you have account information, data for employees, students, salary slips, calculations sheets for tax, Microsoft Excel is the best editor to compile such kind of things. The person whether he is from IT background or non-IT background, he feels comfortable working with MS Excel. The Jakarta POI (Poor Obfuscation Implementation) API is a fantastic way for Java programmers to access Microsoft document formats. The most mature API from Jakarta POI is the HSSF (Horrible Spreadsheet Format) API, which accesses Microsoft Excel documents.
POI terminology:

The key terms associated with Jakarta POI are as follows:
POIFS (Poor Obfuscation Implementation File System): Java APIs for reading and writing OLE (Object Linking and Embedding) 2 compound document formats
HSSF (Horrible Spreadsheet Format): Java API to read Microsoft Excel
HDF (Horrible Document Format): Java API to read and write Microsoft Word 97
HPSF (Horrible Property Set Format): Java API for reading property sets using (only) Java
Here is the code which can really guide you to use the POI package to write into the Excel file.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class JavaExcelPOI{
BufferedWriter out;
File file = null;
FileOutputStream fileoutputstream;
HSSFWorkbook workbook;
HSSFCell cell;
HSSFRow row;
HSSFSheet sheet;
HSSFFont font;
HSSFCellStyle cellStyle;
int i = 0;
int j = 1;
JavaExcelPOI() {
}
private void doWriteIntoExcelFile(){
try {
fileoutputstream = new FileOutputStream(new File("C:\\Demo"+".xls"));
workbook = new HSSFWorkbook();
sheet = workbook.createSheet("Demo");
String header[] = {"Header1","Header2"};
row = sheet.createRow((short)0);
cellStyle = workbook.createCellStyle();
font = workbook.createFont();
font.setColor(HSSFFont.COLOR_RED);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
cellStyle.setFont(font);
for(i = 0;i"lessthan"header.length;i++){
cell = row.createCell((short)i);
cell.setCellValue(header[i]);
cell.setCellStyle(cellStyle);
}
String celldetails = "";
row = sheet.createRow((short)j);
for(i =0;i"lessthan"header.length;i++){
cell = row.createCell((short)i);
celldetails = "Cell Value";
cell.setCellValue(celldetails+i);
}
j = j+1;
workbook.write(fileoutputstream);
} catch(Exception se){
se.printStackTrace();
}

}

public static void main(String args[]) {
JavaExcelPOI jePOI= new JavaExcelPOI();
jePOI.doWriteIntoExcelFile();
}
}
You can download the latest version of the POI package from the following link:
http://poi.apache.org/index.html.
You can also view the API documentation of the POI package:
http://poi.apache.org/apidocs/

The code is also using the functionality of the POI packages that cause the cell to define with definite style and font.

The next sections will guide you through more details about it.

No comments:

Total Pageviews