| Java Excel API | |
|---|---|
| | |
| Developer(s) | Andy Khan, Eric H. Jung |
| Stable release | 2.6.12 / 8 October 2012 |
| Written in | Java |
| Operating system | Cross-platform |
| Type | API to access Microsoft Excel format |
| License | GNU LGPL v2.1+ [1] |
| Website | jexcelapi |
Java Excel API (a.k.a. JXL API) allows users to read, write, create, and modify sheets in an Excel (.xls) workbook at runtime. It doesn't support .xlsx format. [2]
Java Excel API supports Excel documents with versions Excel 95, 97, 2000, XP, and 2003. These documents hold the extension .xls. [2]
Java Excel API is widely used with Selenium.
Sample code to write to an Excel file might look like as follows:
importjava.io.File;importjxl.Workbook;importjxl.write.WritableSheet;importjxl.write.WritableWorkbook;importjxl.write.Label;importjxl.write.WriteException;publicclassDataSheet{privateWorkbookwbook;privateWritableWorkbookwwbCopy;privateWritableSheetshSheet;publicvoidreadExcel(){try{wbook=Workbook.getWorkbook(newFile("path/testSampleData.xls"));wwbCopy=Workbook.createWorkbook(newFile("path/testSampleDataCopy.xls"),wbook);shSheet=wwbCopy.getSheet(0);}catch(Exceptione){e.printStackTrace();}}publicvoidsetValueIntoCell(StringstrSheetName,intiColumnNumber,intiRowNumber,StringstrData)throwsWriteException{WritableSheetwshTemp=wwbCopy.getSheet(strSheetName);LabellabTemp=newLabel(iColumnNumber,iRowNumber,strData);try{wshTemp.addCell(labTemp);}catch(Exceptione){e.printStackTrace();}}publicvoidcloseFile(){try{// Closing the writable work bookwwbCopy.write();wwbCopy.close();// Closing the original work bookwbook.close();}catch(Exceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args)throwsWriteException{DataSheetds=newDataSheet();ds.readExcel();ds.setValueIntoCell("sheet1",5,1,"PASS");ds.setValueIntoCell("sheet1",5,2,"FAIL");ds.setValueIntoCell("sheet1",5,3,"PASS");ds.closeFile();}}