Create new excel file using apache POI

This example shows you how to create the new Excel workbook using Apache POI framework. The Apache POI framework is used to read and write the Microsoft documents like Excel, Word, Power point etc. Mostly the POI framework used to read and write the Excel file.

You can find the latest jar file from the POI framework home page here . Download the jar file and set it to your classpath variable and start using the Apache POI framework.

Our first example using Apache POI is writing the Microsoft Excel file without any data. Below is the example code of writing an Excel file using Apache POI.

package com.java.connect.poi;

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

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

// Create new Workbook using POI
public class POINewWorkbookExample {
	public static void main(String[] args) throws IOException {
		// Create a workbook object.
		Workbook workbook = new HSSFWorkbook();
		// Create a FileOutputStream by passing the excel file name.
		FileOutputStream outputStream = new FileOutputStream("POINewWorkbookExample.xls");
		// Write the FileOutputStream to workbook object.
		workbook.write(outputStream);
		// Finally close the FileOutputStream.
		outputStream.close();
	}
}

When you run this example, It will create an excel file named POINewWorkbookExample.xls

Below is the screen shot of created Excel file by above example.

The excel file