Wednesday, August 27, 2014

Write Excel File Using Apache POI in Java

Hi All,

Editing Microsoft documents using a java program might be useful in certain cases. For that there are various libraries. Here I'm going to show you how you can use the Apache POI with Microsoft Documents. More details are available here.

I had to use Apache POI to create and write values received from Arduino, to an Excel Sheet. So here I'm going to show you how you can write values to an Excel Sheet.

First download the latest release from Apache POI. It can be downloaded from here.

Create a new project in Eclipse and create a new class named 'WriteExcel.java'. Add a new folder to the project named 'lib'. Extract the Apache POI library and copy the 'poi-3.10.1-20140818.jar' to the lib folder. Then add the jar to the project by Right Click the Project -> Build Path -> Configure Build Path -> Add JARs.



Now we can start coding the 'WriteExcel.java' class.
Following is the complete code.

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.WorkbookUtil;


public class WriteExcel {
 
 public static void main(String[] args) {
  Workbook workbook = new HSSFWorkbook(); //Create a workbook object
  
  //Create a sheet inside the workbook. There are several ways to do this. 
  //1.Create sheet with default name
  Sheet sheet1 = workbook.createSheet(); 
  //2.Create Sheet with Valid Name
  Sheet sheet2 = workbook.createSheet("ValidName"); 
  //3.Create Sheet with any (may be invalid) name
  //createSafeSheetName() will make it correct
  Sheet sheet3 = workbook.createSheet(WorkbookUtil.createSafeSheetName("%?%#23InvalidName"));
  
  //Let's write some values to rows and columns in sheet1
  
  for (int i = 0; i < 20; i++) {
   //Create a row in sheet 1 assign it to "Row" object.
   Row row = sheet1.createRow(i);
   for (int j = 0; j < 10; j++) {
    //Create a cell in 'row' and assign it to "Cell" object
    Cell cell = row.createCell(j);
    //Write Values to Cell
    cell.setCellValue("( "+i+","+j+" )");
   }
  }
  
  try {
   //Create a new file output stream to write data to a File
   FileOutputStream fileOutputStream = new FileOutputStream("FirstExcel.xls");
   //Write values to the file
   workbook.write(fileOutputStream);
   //Cose file output stream
   fileOutputStream.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 
}

Now run the file and once you refresh the project, you'll see the "FirstExcel.xls" file added to your project. Open it. Following is the output.


Hope that helps, 
Thank You. :-) 

No comments:

Post a Comment