Excel in Java using Apache POI

This tutorial shows how to create an excel file. There are other options to create excel files using a Java program but in this case we are going to speak about Apache POI which is probably one of the most important frameworks to manage office files. For example you can read and write powerpoint, word or excel files. HSSF is the POI Project's pure Java implementation of the Excel '97(-2007) file format. XSSF is the POI Project's pure Java implementation of the Excel 2007 OOXML (.xlsx) file format. HSSF and XSSF provides ways to read spreadsheets create, modify, read and write XLS spreadsheets. They provide:
• low level structures for those with special needs
• an event model api for efficient read-only access
• a full user model api for creating, reading and modifying XLS files.

Jar files required: Download Here
1. ooxml-schemas-1.0.jar
2. poi-ooxml-3.5.jar
3. poi-3.5.jar
4. xmlbeans-2.3.0.jar
5. dom4j-1.6.1.jar

Java Source code using POI-XSSF


import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import org.apache.poi.hssf.util.HSSFColor.GREY_25_PERCENT;
import org.apache.poi.hssf.util.HSSFColor.GREY_50_PERCENT;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class XSSFExcelGenerator {
XSSFSheet sheet;// Sheet Declaration
XSSFWorkbook xssfWorkbook = new XSSFWorkbook();

protected void buildExcelDocument(String fileName) {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(fileName);
sheet = xssfWorkbook.createSheet("SampleExcelSheet"); // Create Sheet.
XSSFFont font = xssfWorkbook.createFont();// Font setting for sheet.
font.setBoldweight((short) 700);
sheet.setDefaultColumnWidth(30);
// Create Styles for sheet.

XSSFRow headerRow = sheet.createRow(0); // Create Header Row(0th Row)

XSSFCell headerCell01 = headerRow.createCell(0); // 0th Row’s 0th Cell
headerCell01.setCellStyle(headerStyle());
headerCell01.setCellValue("STUDENT NAME");

XSSFCell headerCell02 = headerRow.createCell(1); // 0th Row’s 1st Cell
headerCell02.setCellStyle(headerStyle());
headerCell02.setCellValue("CLASS");

XSSFCell headerCell03 = headerRow.createCell(2); // 0th Row’s 2nd Cell
headerCell03.setCellStyle(headerStyle());
headerCell03.setCellValue("DATE OF BIRTH");

XSSFRow informationRow1 = sheet.createRow(1); // Create First Information Row(1st Row)

XSSFCell cell11 = informationRow1.createCell(0);
Cell11.setCellStyle(cellstyle());
Cell11.setCellValue("Test1");

XSSFCell cell12 = informationRow1.createCell(1);
Cell12.setCellStyle(cellstyle());
Cell12.setCellValue("Fifth");

XSSFCell cell13 = informationRow1.createCell(2);
Cell13.setCellStyle(cellstyle());
Cell13.setCellValue("11/1/2012");
XSSFRow informationRow2 = sheet.createRow(2); // Create Second Information Row(2nd Row)

XSSFCell cell21 = informationRow2.createCell(0);
Cell21.setCellStyle(cellstyle());
Cell21.setCellValue("Test2");

XSSFCell cell22 = informationRow2.createCell(1);
Cell22.setCellStyle(cellstyle());
Cell22.setCellValue("Seventh");

XSSFCell cell23 = informationRow2.createCell(2);
Cell23.setCellStyle(cellstyle());
Cell23.setCellValue("11/1/2012");

XSSFRow informationRow3 = sheet.createRow(3); // Create Third Information Row(3rd Row)

XSSFCell cell31 = informationRow3.createCell(0);
Cell31.setCellStyle(cellstyle());
Cell31.setCellValue("Test3");

XSSFCell cell32 = informationRow3.createCell(1);
cell2.setCellStyle(cellstyle());
cell2.setCellValue("Ninth");

XSSFCell cell33 = informationRow3.createCell(2);
Cell33.setCellStyle(cellstyle());
Cell33.setCellValue("11/1/2012");

xssfWorkbook.write(fileOutputStream); // write in excel

} catch (Exception e) {
e.printStackTrace();
} finally {
/*
* Close File Output Stream
*/
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

protected XSSFCellStyle headerstyle(){

XSSFFont font = xssfWorkbook.createFont();
font.setBoldweight((short) 700);
XSSFCellStyle headerStyle = xssfWorkbook.createCellStyle();
headerStyle.setFillForegroundColor(GREY_50_PERCENT.index);
headerStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
headerStyle.setFont(font);
headerStyle.setBorderBottom((short) 1);
headerStyle.setBorderLeft((short) 1);
headerStyle.setBorderRight((short) 1);
headerStyle.setBorderTop((short) 1);
headerStyle.setAlignment(CellStyle.ALIGN_CENTER);
return headerStyle;

}

protected XSSFCellStyle cellstyle(){

XSSFCellStyle cellStyle = xssfWorkbook.createCellStyle();
cellStyle.setFillForegroundColor(GREY_25_PERCENT.index);
cellStyle.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
cellStyle.setBorderBottom((short) 1);
cellStyle.setBorderLeft((short) 1);
cellStyle.setBorderRight((short) 1);
cellStyle.setBorderTop((short) 1);
cellStyle.setAlignment(CellStyle. ALIGN_CENTER);
return cellStyle;
}


public static void main(String[] args) {
/*
* call method and pass as an argument path of excel file. Please make
* sure that path of excel file is correct otherwise it will throw
* FileNotFound Exception.
*/
new XSSFExcelGenerator().buildExcelDocument("D:" + File.separator + "excelfiles" + File.separator + "Testexcel.xlsx");
}
}

Created Excel File(Formatted):

0 comments:

Post a Comment