Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



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

Related Tutorials


 
 

Share on Google+Share on Google+

Create Excel(.xlsx) document using Apache POI

Advertisement
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);
cell.setCellValue(1);

// Or do it on one line.
row.createCell(1).setCellValue(1.2);
row.createCell(2).setCellValue(
createHelper.createRichTextString("This is a string"));
row.createCell(3).setCellValue(true);

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

OUTPUT

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

A       
B         C D          
1 1.2 This is a string TRUE

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



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

Related Tutorials

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
alekhya
September 18, 2011
xlsx

data
Guest
January 7, 2013
Thanks

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

testing excel download
Mohammed
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
DMCA.com