Java Generate XLS Apache POI

Java Generate XLS Apache POI

To start with Apache POI API to generate XLS sheet. Please have below sample class which will generate XLS sheet on path you specified:

  • Maven project structure:

Java Generate XLS Apache POI

  • dependencies: pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>XLSSheetGeneration</groupId>
	<artifactId>com.javahonk.xls</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>XLSSheetGeneration</name>
	<description>XLSSheetGeneration</description>

	<dependencies>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.11</version>
		</dependency>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.11</version>
		</dependency>
	</dependencies>
</project>
  • Java class: CreateXLSReportFile.java:
package com.javahonk.xls;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

public class CreateXLSReportFile {

	public static void main(String[] args) {

		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet("Java Honk Report");

		Map<String, Object[]> data = new HashMap<String, Object[]>();
		data.put("1", new Object[] { "Emp No.", "Name", "Salary" });
		data.put("2", new Object[] { 1d, "test1", 1500000.00 });
		data.put("3", new Object[] { 2d, "test1", 800000d });
		data.put("4", new Object[] { 3d, "test1", 700000d });
		data.put("5", new Object[] { 3d, "test1", 700000d });
		data.put("6", new Object[] { 3d, "test1", 700000d });
		data.put("7", new Object[] { 3d, "test1", 700000d });

		Set<String> keyset = data.keySet();
		int rownum = 0;
		for (String key : keyset) {
			Row row = sheet.createRow(rownum++);
			Object[] objArr = data.get(key);
			int cellnum = 0;
			for (Object obj : objArr) {
				Cell cell = row.createCell(cellnum++);
				if (obj instanceof Date)
					cell.setCellValue((Date) obj);
				else if (obj instanceof Boolean)
					cell.setCellValue((Boolean) obj);
				else if (obj instanceof String)
					cell.setCellValue((String) obj);
				else if (obj instanceof Double)
					cell.setCellValue((Double) obj);
			}
		}

		try {
			FileOutputStream out = new FileOutputStream(new File("C:\\JavaHonk\\XLSReport\\newTest.xls"));
			workbook.write(out);
			out.close();
			System.out.println("Excel written successfully..");

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				workbook.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}

For more information please visit Apache POI documentation here

Leave a Reply

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