Apache POI HSSF Excel Write Example

Thursday, September 17, 2009

Apache POI is used to generate Excel Reports. Apache POI HSSF is good one to generate excel reports.

Now a days every banking application requires Excel Report generation that's why i am giving the example related to Apache POI HSSF.

For this application you need one jar file
That is : poi-2.5.1-final-20040804.jar


ApchePOIExample.java

/**
 * File Name         :  ApchePOIExample.java
 * Created By        :  NagarajuV
 * Created Date      :  Aug 12, 2009
 * Purpose           :  Writing data to excel Using Apache POI.
 */
package com.raj.ApachePOI;

import java.io.*;
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;
import org.apache.poi.hssf.util.HSSFColor;
/**
 * @author nagarajuv
 *
 */

class ApchePOIExample {
 public static void main(String[] args) {
  try {
   //Writing Data to ExcelSheet
   HSSFWorkbook wb = null;

   //This is for creating Workbook
   wb = new HSSFWorkbook();
         HSSFSheet spreadSheet = wb.createSheet("Query_Report");
         HSSFRow row = spreadSheet.createRow(0);
         
         //This is for Header Style
         HSSFCellStyle headerCellStyle = wb.createCellStyle();
         headerCellStyle.setFillForegroundColor(HSSFColor.BROWN.index);
         headerCellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
         HSSFFont setFont = wb.createFont();
         setFont.setFontHeightInPoints((short)10);
         setFont.setColor(HSSFColor.WHITE.index);
         setFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
         headerCellStyle.setBorderBottom(headerCellStyle.BORDER_THIN);
         headerCellStyle.setFont(setFont);
                  
         HSSFCell cell = null;
         
         spreadSheet.setColumnWidth((short)0, (short) (256 * 25));
         spreadSheet.setColumnWidth((short)1, (short) (256 * 25));
         spreadSheet.setColumnWidth((short)2, (short) (256 * 25));
         
         cell = row.createCell((short) 0);
         cell.setCellValue("EMPID");
         cell.setCellStyle(headerCellStyle);
         
         cell = row.createCell((short) 1);
         cell.setCellValue("EMPNAME");
         cell.setCellStyle(headerCellStyle);
         
         cell = row.createCell((short) 2);
         cell.setCellValue("EMPAGE");
         cell.setCellStyle(headerCellStyle);
         
         //This is for Data Style
         HSSFCellStyle dataCellStyle = wb.createCellStyle();
         HSSFFont setDataFont = wb.createFont();
         setDataFont.setColor(HSSFColor.LIGHT_BLUE.index);
         dataCellStyle.setBorderBottom(dataCellStyle.BORDER_THIN);
         dataCellStyle.setFont(setDataFont);
         
         row = spreadSheet.createRow((short) 1); //First Data Row
   cell = row.createCell((short) 0);
   cell.setCellValue("101");
   cell.setCellStyle(dataCellStyle);
   
   cell = row.createCell((short) 1);
   cell.setCellValue("Ram");
   cell.setCellStyle(dataCellStyle);
   
   cell = row.createCell((short) 2);
   cell.setCellValue("23");
   cell.setCellStyle(dataCellStyle);
   
   row = spreadSheet.createRow((short) 2); //Second Data Row
   cell = row.createCell((short) 0);
   cell.setCellValue("105");
   cell.setCellStyle(dataCellStyle);
   
   cell = row.createCell((short) 1);
   cell.setCellValue("Mahesh");
   cell.setCellStyle(dataCellStyle);
   
   cell = row.createCell((short) 2);
   cell.setCellValue("29");
   cell.setCellStyle(dataCellStyle);
   
      //Write the output to a file
      FileOutputStream fileOut = new FileOutputStream("Query_Report.xls");
      wb.write(fileOut);
      fileOut.close();
      
      System.out.println("XL Generated....!");

  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}
If you want to do any more changes or beautify the excel go to this link you will get sufficient . guide
Image Hosted by ImageShack.us