Jump to content

Java Excel API

From Alfawiki
Adapted from the English Wikipedia article Java Excel API, available under CC BY-SA 4.0Updated 9 August 2025
Java Excel API
Company typeSoftware library
IndustrySoftware development
Area served
Cross-platform
ProductsJava library for Microsoft Excel (.xls) file manipulation

Java Excel API (JXL API) allows developers to read, write, create, and alter worksheets within Microsoft Excel (.xls) files during program execution. It does not provide compatibility with the .xlsx file format.

Microsoft Excel compatibility

This API supports Excel editions 95, 97, 2000, XP, and 2003, specifically working with files that have the .xls extension.

Application

Java Excel API is frequently utilized alongside Selenium to facilitate automated testing and manage data within Excel spreadsheets.

Sample code

An illustrative snippet demonstrating how to insert data into an Excel workbook:

<syntaxhighlight lang="Java"> import java.io.File; import jxl.Workbook; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.Label; import jxl.write.WriteException;

public class DataSheet {

   private Workbook wbook;
   private WritableWorkbook wwbCopy;
   public void readExcel() {
       try {
           wbook = Workbook.getWorkbook(new File("path/testSampleData.xls"));
           wwbCopy = Workbook.createWorkbook(new File("path/testSampleDataCopy.xls"), wbook);
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
   public void setValueIntoCell(String sheetName, int col, int row, String data) throws WriteException {
       WritableSheet sheet = wwbCopy.getSheet(sheetName);
       try {
           sheet.addCell(new Label(col, row, data));
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
   public void closeFile() {
       try {
           wwbCopy.write();
           wwbCopy.close();
           wbook.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
   public static void main(String[] args) throws WriteException {
       DataSheet ds = new DataSheet();
       ds.readExcel();
       ds.setValueIntoCell("sheet1", 5, 1, "PASS");
       ds.setValueIntoCell("sheet1", 5, 2, "FAIL");
       ds.setValueIntoCell("sheet1", 5, 3, "PASS");
       ds.closeFile();
   }

} </syntaxhighlight>

Related topics

External resources