Jump to content

Java Excel API

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Shevonsilva (talk | contribs) at 21:53, 1 February 2016. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
JXL
Developer(s)Alex Lynch
Stable release
2.6.12
Written inJava
Operating systemCross-platform
TypeAPI to access Microsoft Excel format
LicenseGNU GPL v2[1]
Websitehttp://jexcelapi.sourceforge.net/

JXL API (a.k.a. Java Excel API) is the most widely used API for executing Selenium data-driven tests, which allows users to read, write, create, and modify sheets in an Excel(.xls) workbook at runtime. It is not supporting for .xlsx formats.[2]

Microsoft Excel Support

JXL API supports Excel documents with versions Excel 95, 97, 2000, XP, and 2003. These documets hold the extenstion .xls.[2]

Usage

JXL API is widely used with Selenium.

Example

Sample code to write to an Excel file might look like as follows:

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 {

    static Workbook wbook;
    static WritableWorkbook wwbCopy;
    static String ExecutedTestCasesSheet;
    static WritableSheet shSheet;
   
    public void readExcel()
    {
    try{
    wbook = Workbook.getWorkbook(new File("path\\testSampleData.xls"));
    wwbCopy = Workbook.createWorkbook(new File("path\\testSampleDataCopy.xls"), wbook);
    shSheet = wwbCopy.getSheet(0);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    }
   
    public void setValueIntoCell(String strSheetName,int iColumnNumber, int iRowNumber,String strData) throws WriteException
    {
        WritableSheet wshTemp = wwbCopy.getSheet(strSheetName);
        Label labTemp = new Label(iColumnNumber, iRowNumber, strData);
               
        try {
            wshTemp.addCell(labTemp);
             }
            catch (Exception e)
            {
                e.printStackTrace();
            }
    }
   
    public void closeFile()
    {
        try {
            // Closing the writable work book
            wwbCopy.write();
            wwbCopy.close();

            // Closing the original work book
            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();
    }
}

[3]

See also

References

  1. ^ "jxl". Sourceforge. Sourceforge. Retrieved 1 February 2016.
  2. ^ a b Sams, P. (2015). Selenium Essentials. Birmingham: Packt publishing Ltd. p. 133.
  3. ^ "How to Set Data into Excel sheet using jxl". Selenium Easy. Selenium Easy. Retrieved 1 February 2016.