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



Create Excel(.xlsx) document using Apache POI
Posted on: August 9, 2011 at 12:00 AM
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

Related Tags for Create Excel(.xlsx) document using Apache POI:


More Tutorials from this section

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.