How to Write to MS Excel with Java Using Apache POI ? - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

How to Write to MS Excel with Java Using Apache POI ?

Share This


In this tutorial, we will discuss how to write data in an excel document. Here we consider that the MS Excel file contains only one sheet.

In case you are a complete stranger about Apache POI, you can check out this Introduction Tutorial, otherwise, if you know about Apache POI a bit, but do not how to read from the MS Excel document, you can check out this tutorial as well.

Dependencies

You can download the dependency files from any repository.

After downloading the libraries, you have to add them in JAVA_PATH . Alternatively, you can create an Eclipse project and add them to the project as a library. You are being suggested to add the libraries as an internal library instead of an external library.

If you are not familiar with library linking, follow these steps.

  1. Create a directory, lib , in your project. 
  2. Paste the jars in lib directory. 
  3. Right-click on the project, select Properties and go to Java Build Path
  4. Click on Add Jars and browse the lib directory you have created. It's done! 


Now to write to an MS Excel document, try out the following code

package com.t4b.demo.poi; import java.io.File; import java.io.FileOutputStream; import java.util.Map; import java.util.Set; import java.util.TreeMap; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class WriteExcelDemo { public static void main(String[] args) { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Book Information"); Map data = new TreeMap(); data.put("1", new Object[] { "ISBN", "NAME", "AUTHOR" }); data.put("2", new Object[] { "01-8978-76366", "Data Structure", "Anand" }); data.put("3", new Object[] { "01-8933-74566", "C Programming", "Richi" }); data.put("4", new Object[] { "02-6366-89788", "Java", "John" }); Set keyset = data.keySet(); int rownum = 0; for (String key : keyset) { Row row = sheet.createRow(rownum++); Object[] objects = data.get(key); int cellnum = 0; for (Object o : objects) { Cell cell = row.createCell(cellnum++); if (o instanceof String) cell.setCellValue((String) o); else if (o instanceof Integer) cell.setCellValue((Integer) o); } } try { FileOutputStream fileOutputStream = new FileOutputStream(new File("Demo.xlsx")); workbook.write(fileOutputStream); fileOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } } }

Explanation of Source Code

  1. Create an instance of XSSFWorkbook class for .xlsx file.
  2. Create an instance of XSSFSheet. A new sheet can be created by createSheet() method of class XSSFWorkbook, passing the name of the sheet as an argument.
  3. Prepare content to be written in a sheet using Map
  4. Now create Rows using createRow() method and create cells using createCell() methods as per your requirements.
  5. Create an instance of FileOutputStream for Demo.xlsx file.
  6. Finally, write the content.

Hope you have found this tutorial useful!
We will be coming up with lots of other tutorials featuring multiple features of Apache POI. Stay Tuned :)


Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.