问题:
使用了两种方式读取excel,执行都报错:jxl.read.biff.BiffException: Unable to recognize OLE stream
以下代码片段:
package com.hlq.atm.excelCase; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; public class ExcelReadWrite { public static void main(String[] args) { ExcelReadWrite erw=new ExcelReadWrite(); String path=System.getProperty("user.dir"); // erw.readExcelByJxl(path+"/data/Students.xls"); erw.re(path+"/data/Students.xls"); } public void re(String pathName) { try { //创建workbook Workbook workbook=Workbook.getWorkbook(new File(pathName)); //获取第一个工作表sheet Sheet sheet=workbook.getSheet(0); //获取数据 for (int i=0;i<sheet.getRows();i++){ for (int j=0;j<sheet.getColumns();j++){ Cell cell=sheet.getCell(j,i); System.out.print(cell.getContents()+" "); } System.out.println(); } workbook.close(); } catch (IOException e) { e.printStackTrace(); } catch (BiffException e) { e.printStackTrace(); } }
public void readExcelByJxl(String fileName) {
File file=new File(fileName);
try {
// FileInputStream fis=new FileInputStream(file);
Workbook workbook=Workbook.getWorkbook(file);
Sheet[] sheet=workbook.getSheets();
for (int i=0;i<sheet.length;i++){
Sheet rs=workbook.getSheet(i);
//遍历行
for (int j=0;j<rs.getRows();j++){
Cell[] cells=rs.getRow(j);
//遍历列
for (int z=0;z<cells.length;z++){
System.out.print(cells[z].getContents());
System.out.print(" ");
}
System.out.println();
}
}
workbook.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
}
}
}
原因:
在网上查询改报错原因,是文件版本不兼容,jxl只支持excecl03版
解决办法:
尝试另存文件,选择Excel 97-2004工作簿,再次执行,执行通过
执行结果: