5

I was trying to connect my Java program with an Excel file. I have did upto this. But it throws this excepion

Unable to recognize OLE stream

Please, help me to complete this.

import jxl.*;
import java.io.*;

public class excel
{
      public static void main(String[] args)throws Exception
      {

       File ex=new File("D:/worksps/test.xlsx");
       Workbook w= Workbook.getWorkbook(ex);
       Sheet s= w.getSheet(0);
       for(int i=0;i<s.getColumns();i++)
       {
         for(int j=0;j<s.getRows();j++)
         {
               Cell cell=s.getCell(i, j);
               System.out.println("     "+cell.getContents());
         }
         System.out.println("\n");
       }
      }
}
0

2 Answers 2

16

JXL supports Excel worksheets created in Excecl 95/97 and 2000 -

Read the below in the official JXL site - http://www.andykhan.com/jexcelapi/

Features

Reads data from Excel 95, 97, 2000 workbooks Reads and writes formulas (Excel 97 and later only) Generates spreadsheets in Excel 2000 format

Your excel sheet seems to be created after Excel 2000. That seems to be the problem.

If you want to read Excel files created after Excel 2000 then you should use Apache POI. It is also an easy to use API and supports MS Excel 97 to MS Excel 2008.

3
  • Thank you. I was using Excel 2003. Mar 31, 2011 at 9:55
  • @Apache Fan: Does Jexcel API support Excel 97-2003 Worksheet? Jul 23, 2012 at 0:23
  • @Piyush - No. According to documentation it only supports upto Excel 2000
    – Pushkar
    Jul 26, 2012 at 9:24
0

Usually testers will save the excel document in current installed version but for data driven framework - excel file need to save in 97-2003 file format.

below is the code to call the excel sheets.

String[][] data=null;

@DataProvider(name="loginData")
public String[][] loginDataProvider() throws BiffException, IOException {
    data=loginUsingExcel();
    return data;

}

public String[][] loginUsingExcel() throws BiffException, IOException {
    
    FileInputStream excel=new FileInputStream("C:\\Users\\ME\\Desktop\\Filename.xls");

    Workbook workbook=Workbook.getWorkbook(excel);
    Sheet sheet = workbook.getSheet(0);
    int rows = sheet.getRows();
    int columns = sheet.getColumns();
    
    String[][] testData=new String[rows-1][columns];
    for (int i = 1; i < rows; i++) {
        for (int j = 0; j < columns; j++) {
            testData[i-1][j]=sheet.getCell(j, i).getContents();
        }
    }
    return testData;
}
 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.