Generic way of writing data in excel using apache poi

The example is about generic way of writing data in excel using apache poi library in Java programming language. With this example I will show you how to create an excel file and write any kind of objects using Java’s reflection API to excel file with the help of Apache POI. In this example I am writing to single sheet, you can also check how to write to multiple sheets of excel file.

The Apache POI is to create and maintain Java APIs for manipulating various file formats based upon the Office Open XML standards (OOXML) and Microsoft’s OLE 2 Compound Document format (OLE2).


At least Java 1.8, Apache POI 3.12, 4.1.1, 4.1.2

Project Setup

You can create either gradle or maven based project in Eclipse or in your favorite IDE or tool. The name of the project is apache-poi-excel-write-data-generic-way.

If you are creating gradle based project then you can use below build.gradle script:

plugins {
    id 'java-library'

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {
    implementation 'org.apache.poi:poi-ooxml:4.1.2'
    //required only for jdk 9 or above

If you are creating maven based project then you can use below pom.xml file:

<project xmlns=""

		<jdk.version>at least 1.8</jdk.version>
		<!-- apache poi for xlsx, docx etc reading/writing -->

VO Class

This is a simple value object, a POJO class called Person. I have added three attributes to the below class.


public class Person {

	private String name;
	private String email;
	private String address;

	public Person(String name, String email, String address) { = name; = email;
		this.address = address;

	//getters and setters


Write Data Generic Way

Create a generic method to handle any kind of data to be written to excel file through single method. I have created a file object followed by excel workbook and finally I have created a sheet on the workbook.

I retrieved the attribute names from the class object, for example, Person class using Java’s reflection API. Then I determined the getter methods from the class using the reflection API and finally write the value of different types from all getter methods to the excel sheet.


import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelFileWriter {

	public static <T> void writeToExcel(String fileName, List<T> data) {
		OutputStream fos = null;
		XSSFWorkbook workbook = null;
		try {
			File file = new File(fileName);
			workbook = new XSSFWorkbook();
			Sheet sheet = workbook.createSheet();
			List<String> fieldNames = getFieldNamesForClass(data.get(0).getClass());
			int rowCount = 0;
			int columnCount = 0;
			Row row = sheet.createRow(rowCount++);
			for (String fieldName : fieldNames) {
				Cell cell = row.createCell(columnCount++);
			Class<? extends Object> classz = data.get(0).getClass();
			for (T t : data) {
				row = sheet.createRow(rowCount++);
				columnCount = 0;
				for (String fieldName : fieldNames) {
					Cell cell = row.createCell(columnCount);
					Method method = null;
					try {
						method = classz.getMethod("get" + capitalize(fieldName));
					} catch (NoSuchMethodException nme) {
						method = classz.getMethod("get" + fieldName);
					Object value = method.invoke(t, (Object[]) null);
					if (value != null) {
						if (value instanceof String) {
							cell.setCellValue((String) value);
						} else if (value instanceof Long) {
							cell.setCellValue((Long) value);
						} else if (value instanceof Integer) {
							cell.setCellValue((Integer) value);
						} else if (value instanceof Double) {
							cell.setCellValue((Double) value);
			fos = new FileOutputStream(file);
		} catch (Exception e) {
		} finally {
			try {
				if (fos != null) {
			} catch (IOException e) {
			try {
				if (workbook != null) {
			} catch (IOException e) {

	// retrieve field names from a POJO class
	private static List<String> getFieldNamesForClass(Class<?> clazz) throws Exception {
		List<String> fieldNames = new ArrayList<String>();
		Field[] fields = clazz.getDeclaredFields();
		for (int i = 0; i < fields.length; i++) {
		return fieldNames;

	// capitalize the first letter of the field name for retriving value of the
	// field later
	private static String capitalize(String s) {
		if (s.length() == 0)
			return s;
		return s.substring(0, 1).toUpperCase() + s.substring(1);


Main Class

I am writing a class with main method to write to an excel file generic way using Java reflection. In the below class I have taken few dummy data which will be written into the excel file.


import java.util.ArrayList;
import java.util.List;

public class ExcelFileWriterTest {

	public static void main(String[] args) {
		List<Person> persons = new ArrayList<>();

		Person p1 = new Person("A", "", "Kolkata");
		Person p2 = new Person("B", "", "Mumbai");
		Person p3 = new Person("C", "", "Delhi");
		Person p4 = new Person("D", "", "Chennai");
		Person p5 = new Person("E", "", "Bangalore");
		Person p6 = new Person("F", "", "Hyderabad");


		ExcelFileWriter.writeToExcel("excel-person.xlsx", persons);


Testing the Application

Now running the above class will give you the following output in excel file. The excel file is generated under the project’s root directory.

generic way of writing excel using apache poi

Hope you have enjoyed learning the example generic way of writing data in excel using apache poi.

Source Code


Thanks for reading.

3 Thoughts to “Generic way of writing data in excel using apache poi”

  1. Jay

    it didn’t work for multiple sheet

  2. M

    finally a tutorial without mistakes,
    thx, this helped me a-lot!

Leave a Comment