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.
-
Create a directory,
lib
, in your project. - Paste the
jars
inlib
directory. - Right-click on the project, select Properties and go to
Java Build Path
, - Click on
Add Jars
and browse thelib
directory you have created. It's done!
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
- Create an instance of
XSSFWorkbook
class for.xlsx
file. - Create an instance of
XSSFSheet
. A new sheet can be created bycreateSheet()
method of classXSSFWorkbook
, passing the name of the sheet as an argument. - Prepare content to be written in a sheet using
Map
- Now create Rows using
createRow()
method and create cells usingcreateCell()
methods as per your requirements. - Create an instance of
FileOutputStream
forDemo.xlsx
file. - 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 :)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.