Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Write Excel File In Java Using Apache POI

Apache POI is a powerful Java library to work with different Microsoft Office file formats such as Excel, Power point, Visio, MS Word etc.In this tutorial we will use Apache POI library to create an Excel spreadsheet with the content below. 

 

TestId,TestName,TestModule,TestType,TestSteps,Action,TestResult,Note
1,Login,Dashboard,Regression,1,Open Browser,Browser Should Open,This is a note

Create a Java maven-based project and add the following Apache POI dependency in the pom.xml file.

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.12</version>
</dependency>

Write the following code in the class "CreateExcelFileWithJavaPOI.java".

package com.example.excel;

import jxl.write.WriteException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileOutputStream;
import java.io.IOException;

public class CreateExcelFileWithJavaPOI {
    public static void main(String[] args) throws WriteException {
        try {
            //define a workbook
            Workbook wb = new HSSFWorkbook();
            //define output file
            FileOutputStream fileOut = new FileOutputStream("workbook.xls");
            //add a sheet to the workbook
            Sheet sheet = wb.createSheet("TestCases");
            // Create a row and put some cells in it. Rows are 0 based.
            //define first row
            Row row0 = sheet.createRow((short)0);
            // Create a cell and put a value in it.
            Cell cell = row0.createCell(0);
            cell.setCellValue("TestId");
            //add more cells and values
            row0.createCell(1).setCellValue("TestName");
            row0.createCell(2).setCellValue("TestModule");
            row0.createCell(3).setCellValue("TestType");
            row0.createCell(4).setCellValue("TestSteps");
            row0.createCell(5).setCellValue("Action");
            row0.createCell(6).setCellValue("TestResult");
            row0.createCell(7).setCellValue("Note");
            //add row 1 content
            Row row1=sheet.createRow((short)1);
            row1.createCell(0).setCellValue("1");
            row1.createCell(1).setCellValue("Login");
            row1.createCell(2).setCellValue("Dashboard");
            row1.createCell(3).setCellValue("Regression");
            row1.createCell(4).setCellValue("1");
            row1.createCell(5).setCellValue("Open Browser");
            row1.createCell(6).setCellValue("Browser Should Open");
            row1.createCell(7).setCellValue("This is a note");
            // All sheets and cells added. Now write out the workbook
            wb.write(fileOut);
            fileOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Run the above code and see the result. An excel file "workbook.xls" is created with the content below.

TestId,TestName,TestModule,TestType,TestSteps,Action,TestResult,Note
1,Login,Dashboard,Regression,1,Open Browser,Browser Should Open,This is a note