从Java读取文件excel版本4

时间:2018-05-25 22:36:11

标签: java excel apache-poi

我想阅读一个由Power Builder应用程序生成的excel文件(Office的第4版)。 这是代码:

Users
 |
 +--UserID
 |   |
 |   +--UserInfo
 |   |   |
 |   |   +--Name
 |   |   +--Phone
 |   |   +--etc..
 |   |
 |   +--Records
 |   |   |
 |   |   +--RecordName
 |   |   |   |
 |   |   |   +--recordValue
 |   |   |   +--recordType
 |   |   +--RecordName
 |   |   |   |
 |   |   |   +--recordValue
 |   |   |   +--recordType

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

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

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

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)

我尝试用odbc

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

但它引发了异常:

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

我还尝试使用jxl库

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

但抛出这个例外:

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

任何人都可以帮我阅读任何Java库的excel文件版本4(1994)吗? 提前致谢

我正在使用jdk 1.7

1 个答案:

答案 0 :(得分:0)

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

XSSFWorkbook book = new XSSFWorkbook(fis);