Print

Create Excel File In Java

Java Excel API is a mature, open source java API enabling developers to read, write, and modifiy Excel spreadsheets dynamically. Here we explain how to create an excel file using jxl library. You create a maven based project and add jxl library dependency in the pom.xml file. 

 

<dependency>
	<groupId>net.sourceforge.jexcelapi</groupId>
	<artifactId>jxl</artifactId>
	<version>2.6.12</version>
</dependency>

Write the following code in the java class file "CreateExcelFile.java".

package com.example.excel;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import java.io.File;
import java.io.IOException;

public class CreateExcelFile {
    public static void main(String[] args) throws WriteException {
        try {
            //create a new workbook to write
            WritableWorkbook workbook = Workbook.createWorkbook(new File("output.xls"));
            //add a new sheet to the work book
            WritableSheet sheet = workbook.createSheet("TestCases", 0);
            //add test Id to col 0 row 0
            Label testId = new Label(0, 0, "TestId");
            //add test name to col 1 row 0
            Label testName = new Label(1, 0, "TestName");
            //add test module to col 2 row 0
            Label testModule = new Label(2, 0, "TestModule");
            //add test type to col 3 row 0
            Label testType = new Label(3, 0, "TestType");
            //add test steps to col 4 row 0
            Label testSteps = new Label(4, 0, "TestSteps");
            //add test actions to col 5 row 0
            Label testActions = new Label(5, 0, "Action");
            //add test result to col 6 row 0
            Label testResult = new Label(5, 0, "TestResult");
            //add test note to col 7 row 0
            Label testNote = new Label(6, 0, "Note");
            //add to the cells
            sheet.addCell(testId);
            sheet.addCell(testName);
            sheet.addCell(testModule);
            sheet.addCell(testType);
            sheet.addCell(testSteps);
            sheet.addCell(testActions);
            sheet.addCell(testResult);
            sheet.addCell(testNote);
            //define row 1 content
            Number testIdNumber = new Number(0, 1, 1234);
            Label testNameContent=new Label(1,1,"Login");
            Label testModuleContent=new Label(2,1,"Dashboard");
            Label testTypeContent=new Label(3,1,"Regression");
            Label testStepsContent=new Label(4,1,"1");
            Label testActionContent=new Label(5,1,"Open Browser");
            Label testResultContent=new Label(6,1,"");
            Label testNoteContent=new Label(7,1,"");
            //add row 1 content
            sheet.addCell(testIdNumber);
            sheet.addCell(testNameContent);
            sheet.addCell(testModuleContent);
            sheet.addCell(testTypeContent);
            sheet.addCell(testStepsContent);
            sheet.addCell(testActionContent);
            sheet.addCell(testResultContent);
            sheet.addCell(testNoteContent);
            // All sheets and cells added. Now write out the workbook
            workbook.write();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Run the code above. An excel file "output.xls" is created with the following content. 

TestId	TestName	TestModule	TestType	TestSteps	TestResult	Note
1234	Login		Dashboard	Regression	1				Open Browser