2 Replies Latest reply: Jun 26, 2012 3:39 PM by Ramesh Ganasala RSS

Read Excel (both xls and xlsx) cell in wdJava

Sattam Kumar Datta
Currently Being Moderated

Hi All,

Our requirement is to read a few cells from an input excel file into wd java application. The input file can be either xls or xlsx format.

I can read cells from xls files using jxl.jar, but i would like to know how to read cells from xlsx files.

 

Also it would be helpful if you can suggest a method that works for both types of files.

 

Thnaks in advance,

Sattam.

  • Re: Read Excel (both xls and xlsx) cell in wdJava
    Jun Wu
    Currently Being Moderated

    you can use apache poi

  • Re: Read Excel (both xls and xlsx) cell in wdJava
    Ramesh Ganasala
    Currently Being Moderated

    Hi Sattam,

     

    Please add the below jar files and use below code.

     

    1. dom4j-1.6.1

    2. poi-3.7-20101029

    3. poi-ooxml-3.7-20101029

    4. poi-ooxml-schemas-3.7-20101029

    5. xmlbeans-2.3.0

     

     

    if(fileName != null && fileName.endsWith(".xls")) {

          // 97 to 2003 format use HSSF class

          HSSFWorkbook wb     = new HSSFWorkbook(templateInputStream);

          HSSFSheet sheet = wb.getSheetAt(0);       // get first sheet

          iRowNum = sheet.getPhysicalNumberOfRows();

          HSSFRow row = null;

          HSSFCell cell   = null;

          for(int i = 1;i <iRowNum ; i++) {

     

    //your code to access the 2003 excel file.

     

    } else{

    // 2007 or 2010 format

          XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(templateInputStream);

          XSSFSheet sheet = wb.getSheetAt(0);

          iRowNum = sheet.getPhysicalNumberOfRows();

          XSSFRow row  = null;

          XSSFCell cell = null;

          for(int i = 1;i <iRowNum ; i++) {

     

    //code to access the 2007 or 2010 excel file

     

    }

     

    }

     

     

    With Regards,

    Ramesh G.

Actions