In our previous posts we have seen on How to Create an Excel File and Read an Excel File.
Now lets see how to Write Excel File in Java using Apache POI
Writing a excel file in java using Apache POI is very easy and can be divided into below steps:
1. Create a workbook
2. Create a sheet in workbook
3. Create a row in sheet to write the headers
4. Get Data and then create Row’s and Cell’s to write data.
To make the example readable, I created separate methods like getRowHeaders , getEmployeeDetails and writeIntoCell
Note that we are using a Employee Object to write data into excel. You can modify to use any of your objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
package com.kscodes.sampleproject; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; 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 WriteExcelUsingPOI { public static void main(String[] args) { // Create instance of the class and call the writeExcelFile() WriteExcelUsingPOI writeExcelUsingPOI = new WriteExcelUsingPOI(); writeExcelUsingPOI.writeExcelFile("C:\\kscodes\\EmployeeDetails.xlsx"); } public void writeExcelFile(String fileName) { XSSFWorkbook workbook = null; FileOutputStream fileOutputStream = null; try { int rowNum = 0; fileOutputStream = new FileOutputStream(new File(fileName)); // Create a Workbook workbook = new XSSFWorkbook(); // Create an Empty Sheet XSSFSheet sheet = workbook.createSheet("Employee Details"); // Create a Row and write the Row Header details Row row = sheet.createRow(rowNum++); List<String> rowHeaders = getRowHeaders(); int cellNum = 0; for (String rowHeader : rowHeaders) { Cell cell = row.createCell(cellNum); cell.setCellValue(rowHeader); cellNum++; } // Get Data and Write into the Excel List<Employee> employeeDetails = getEmployeeDetails(); for (Employee employee : employeeDetails) { Row detailsRow = sheet.createRow(rowNum++); cellNum = 0; writeIntoCell(detailsRow, employee.getName(), cellNum++); writeIntoCell(detailsRow, employee.getYearsOfExp(), cellNum++); writeIntoCell(detailsRow, employee.getSalary(), cellNum++); } workbook.write(fileOutputStream); System.out.println("Excel File created and written !!!!"); } catch (Exception e) { System.out.println("An Exception occured while writing Excel"); } finally { try { if (fileOutputStream != null) { fileOutputStream.close(); } if (workbook != null) { workbook.close(); } } catch (Exception e) { } } } private List<Employee> getEmployeeDetails() { List<Employee> employeeDetails = new ArrayList<>(); employeeDetails.add(new Employee("John Doe", 4500,4,)); employeeDetails.add(new Employee("Steve Smith", 7500, 6)); employeeDetails.add(new Employee("Mr Junior", 2500, 1)); employeeDetails.add(new Employee("Mr Boss", 9999999, 99)); return employeeDetails; } private List<String> getRowHeaders() { List<String> rowHeaders = new ArrayList<>(); rowHeaders.add("Name"); rowHeaders.add("Years Of Exp"); rowHeaders.add("Salary"); return rowHeaders; } private void writeIntoCell(Row row, Object value, int cellNum) { Cell cell = row.createCell(cellNum++); if (value instanceof String) { cell.setCellType(Cell.CELL_TYPE_STRING); cell.setCellValue((String) value); } else if (value instanceof Long) { cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellValue((Long) value); } else if (value instanceof Integer) { cell.setCellType(Cell.CELL_TYPE_NUMERIC); cell.setCellValue((Integer) value); } } } |
Employee.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.kscodes.sampleproject; public class Employee { private String name; private long salary; private int yearsOfExp; public Employee(String name, long salary, int yearsOfExp) { super(); this.name = name; this.salary = salary; this.yearsOfExp = yearsOfExp; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getSalary() { return salary; } public void setSalary(long salary) { this.salary = salary; } public int getYearsOfExp() { return yearsOfExp; } public void setYearsOfExp(int yearsOfExp) { this.yearsOfExp = yearsOfExp; } } |
Output –