【问题标题】:"Unable to recognize OLE stream" excepion while connecting to Excel连接到 Excel 时出现“无法识别 OLE 流”异常
【发布时间】:2011-03-31 09:23:55
【问题描述】:

我试图将我的 Java 程序与 Excel 文件连接起来。我已经做到了这一点。但它抛出了这个异常

无法识别 OLE 流

请帮我完成这个。

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");
       }
      }
}

【问题讨论】:

    标签: java excel ole


    【解决方案1】:

    JXL 支持在 Excel 95/97 和 2000 中创建的 Excel 工作表 -

    在官方 JXL 网站上阅读以下内容 - http://www.andykhan.com/jexcelapi/

    特点

    从 Excel 95、97、2000 读取数据 工作簿 读取和写入公式 (仅限 Excel 97 及更高版本)生成 Excel 2000 格式的电子表格

    您的 Excel 工作表似乎是在 Excel 2000 之后创建的。这似乎是问题所在。

    如果您想读取 Excel 2000 之后创建的 Excel 文件,那么您应该使用 Apache POI。它也是一个易于使用的 API,支持 MS Excel 97 到 MS Excel 2008。

    【讨论】:

    • 谢谢。我使用的是 Excel 2003。
    • @Apache Fan:Jexcel API 是否支持 Excel 97-2003 Worksheet?
    • @Piyush - 不。根据文档,它只支持 Excel 2000
    【解决方案2】:

    通常测试人员会将 excel 文档保存在当前安装的版本中,但对于数据驱动框架 - excel 文件需要保存为 97-2003 文件格式。

    下面是调用excel表格的代码。

    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;
    }
     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      相关资源
      最近更新 更多