Home Tutorials Poi Create Excel(.xlsx) document using Apache POI


Share on Google+Share on Google+

Create Excel(.xlsx) document using Apache POI

In this section, you will learn how to create a Excel sheet having .xlsx extension using Apache POI library.

Create Excel(.xlsx) document using Apache POI

In this section, you will learn how to create a Excel sheet having .xlsx extension using Apache POI library.

In the given below example, we will going to create excel document having one sheet named as "new sheet" which have values on the first row. The row contains numeric, boolean  as well as text cells.

In the below example, i have used Apache POI version 3.7. For downloading the above library click here.

import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
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.xssf.usermodel.XSSFWorkbook;

public class CreateExcelDemo {
public static void main(String[] args) throws IOException {
Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");

// Create a row and put some cells in it. Rows are 0 based.
Row row = sheet.createRow((short) 0);
// Create a cell and put a value in it.
Cell cell = row.createCell(0);

// Or do it on one line.
createHelper.createRichTextString("This is a string"));

// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");


The output of the above code will create a file workbook.xlsx whose content is given below :

B         C D          
1 1.2 This is a string TRUE

Download Source Code


Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 9, 2011

Ask Questions?    Discuss: Create Excel(.xlsx) document using Apache POI  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
September 18, 2011

January 7, 2013

Thank you. it helps a lot. Keep going man Regards
inyeol oh
July 30, 2013
excel download

testing excel download
September 4, 2013
Getting exception

I have poi 3.7 jar and used above code but I am getting below exception: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/formula/udf/UDFFinder at CreateExcelDemo.main(CreateExcelDemo.java:12) Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.formula.udf.UDFFinder at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more