Read, Write Excel With Java - POI

Read And Write Excel With Java - POI Example explains step by step details of reading and writing excel with Apache POI.

Consider an application where you need to manipulate Microsoft office formats, in that case you can use Apache POI

Apache POI project consists of APIs that is used for manipulating various Microsoft office formats, this includes pure java libraries for writing and reading files in Microsoft Office formats, such as Excel, Word and PowerPoint presentations.

Apache POI uses event based API's, this will reduce the memory foot print drastically.

You can see the below example, which is demonstrating Reading & Writing Excel With Apache POI

Required Libraries

You need to download

  1. Apache POI

Following jar must be in classpath

  1. poi-3.7-20101029.jar

Write Excel With Java - POI

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
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;
import org.apache.poi.ss.usermodel.Cell;

public class PoiWriteExcelFile {

 
public static void main(String[] args) {
   
try {
     
FileOutputStream fileOut = new FileOutputStream("test.xls");
      HSSFWorkbook workbook =
new HSSFWorkbook();
      HSSFSheet worksheet = workbook.createSheet
("Worksheet");

      HSSFRow row = worksheet.createRow
((short) 0);

      HSSFCell cellA1 = row.createCell
(0);
      cellA1.setCellValue
("Hello");
      HSSFCellStyle styleOfCell = workbook.createCellStyle
();
      styleOfCell.setFillForegroundColor
(HSSFColor.AQUA.index);
      styleOfCell.setFillPattern
(HSSFCellStyle.BORDER_THIN);
      cellA1.setCellStyle
(styleOfCell);

      HSSFCell cellB1 = row.createCell
(1);
      cellB1.setCellValue
("World");
      styleOfCell = workbook.createCellStyle
();
      styleOfCell.setFillForegroundColor
(HSSFColor.AQUA.index);
      styleOfCell.setFillPattern
(HSSFCellStyle.BORDER_THIN);
      cellB1.setCellStyle
(styleOfCell);

      HSSFCell cellC1 = row.createCell
(2);
      cellC1.setCellValue
("Happy");
      styleOfCell = workbook.createCellStyle
();
      styleOfCell.setFillForegroundColor
(HSSFColor.AQUA.index);
      styleOfCell.setFillPattern
(HSSFCellStyle.BORDER_THIN);
      cellC1.setCellStyle
(styleOfCell);

      HSSFCell cellD1 = row.createCell
(3);
      cellD1.setCellValue
(new Date());
      styleOfCell = workbook.createCellStyle
();
      styleOfCell.setDataFormat
(HSSFDataFormat
          .getBuiltinFormat
("m/d/yy h:mm"));
      styleOfCell.setFillForegroundColor
(HSSFColor.AQUA.index);
      styleOfCell.setFillPattern
(HSSFCellStyle.BORDER_THIN);
      cellD1.setCellStyle
(styleOfCell);
     
      row = worksheet.createRow
(1);
        row.createCell
(0).setCellValue(Calendar.getInstance().getTime().toString());
        row.createCell
(1).setCellValue("a string");
        row.createCell
(2).setCellValue(true);
        row.createCell
(3).setCellType(Cell.CELL_TYPE_ERROR);

      workbook.write
(fileOut);
      fileOut.flush
();
      fileOut.close
();
   
} catch (FileNotFoundException e) {
     
e.printStackTrace();
   
} catch (IOException e) {
     
e.printStackTrace();
   
}

  }

}
Output
Read And Write Excel With Java - POI

1 2 Next





2 Responses to "Read, Write Excel With Java - POI"
  1. sheraz khan 2011-12-10 07:18:55.0
  1. Khoa 2011-12-11 07:18:55.0

Your email address will not be published. Required fields are marked *