"Unable to recognize OLE stream" excepion while connecting to Excel
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++)
{
开发者_如何学Go for(int j=0;j<s.getRows();j++)
{
Cell cell=s.getCell(i, j);
System.out.println(" "+cell.getContents());
}
System.out.println("\n");
}
}
}
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.
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;
}
精彩评论