【问题标题】:Read file excel version 4 from Java [duplicate]从Java读取文件excel版本4 [重复]
【发布时间】:2018-11-05 08:25:57
【问题描述】:

我想读取由 Power Builder 应用生成的 Excel 文件(Office 版本 4)。 这是代码:

File xls = new File("D:/excel.xls");
InputStream fis = new FileInputStream(xls);
HSSFWorkbook book = new HSSFWorkbook(fis);

当我尝试使用 POI 库 v3.17 读取 Excel 文件时,它会抛出此异常:

org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0x0010000200040009, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
        at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:144)

由于 Excel 文件是一个非常旧的版本,我尝试使用 POI v3.0-FINAL 读取它(我找不到旧版本)并抛出此异常:

java.io.IOException: Invalid header signature; read 4503608217567241, expected -2226271756974174256
    at org.apache.poi.poifs.storage.HeaderBlockReader.<init>(HeaderBlockReader.java:100)

我尝试使用 ODBC 读取 Excel 文件

String myDB = "jdbc: odbc: Driver = {Microsoft Excel Driver (*.xls)}; DBQ = d: /excel.xls;" + "DriverID = 22; READONLY = false";
Connection with = DriverManager.getConnection (myDB, "", "");

但它会抛出异常:

java.sql.SQLException: [Microsoft] [ODBC Driver Manager] The name of
the data source is not found and no default drivers were specified

我也试过用 jxl 库读取 Excel 文件

Workbook workbook = Workbook.getWorkbook (new File ("d:/excel.xls"));

但是抛出这个异常:

jxl.read.biff.BiffException: Unable to recognize OLE stream

谁能帮我阅读带有任何 Java 库的 Excel 文件版本 4 (1994)?

我使用的是 JDK 1.7

【问题讨论】:

    标签: java excel apache-poi excel-4.0


    【解决方案1】:

    Apache POI HSSF 格式适用于 .xls 文件。您尝试读取的文件是 .xml。尝试使用 XSSF 类。

    XSSFWorkbook book = new XSSFWorkbook(fis);

    【讨论】:

    • 很抱歉@LiveNLearn,他正在做的测试中保留了这一点,我正在阅读一个 xls 文件
    猜你喜欢
    • 1970-01-01
    • 2016-04-14
    • 2023-03-22
    • 2013-05-03
    • 2010-11-29
    • 2017-07-24
    • 1970-01-01
    • 2011-11-23
    • 1970-01-01
    相关资源
    最近更新 更多