POI不能打开在Excel中打开工作簿 [英] POI cannot open a workbook that opens in Excel

查看:1059
本文介绍了POI不能打开在Excel中打开工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图打开POI的.xlsx文件,我得到一个异常:

When I try to open a .xlsx file in POI, I get an exception:

java.lang.IllegalArgumentException: The supplied POIFSFileSystem does not contain a BIFF8 'Workbook' entry. Is it really an excel file?
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.getWorkbookDirEntryName(HSSFWorkbook.java:223)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:245)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:188)

我注意到,code正在考虑它,即使这个名字的.xlsx一个.xls文件,而且我在使用 WorkbookFactory.create(的FileInputStream); 打开文件。

我试过重命名文件为.zip和WinZip的空位,我得到一个错误 - 无效的zip文件

I tried renaming the file to .zip and opening in WinZip and I get an error - invalid zip file.

该文件确实在Excel中打开,如果我保存它(而不进行一个变化),那么它的POI正确打开为好。

The file does open in Excel, and if I save it (without making a single change), then it opens correctly in POI as well.

推荐答案

从你说的话,这是你有一个的.xlsx 文件,但它在HSSF转动起来没有工作簿条目,我要推断,你有一个加密.xlsx文件

From what you say, which is that you have a .xlsx file but it's turning up in HSSF with no Workbook entry, I'm going to deduce that you have an Encrypted .xlsx file

有基本处理一个.xls或.xlsx文件4种方式:

There are basically 4 ways of handling a .xls or .xlsx file:


  • 未加密.xls文件 - OLE2存储,可与HSSF

  • 加密.xlsx文件 - OLE2存储,一些记录加密,作品有HSSF,如果你提供密码

  • 未加密.xlsx文件 - OOXML(XML的ZIP)贮存,适用于XSSF

  • 加密.xlsx文件 - 在OLE2存储,持有加密部分,它包装OOXML,这其中.....终于XSSF工作(这是讨厌)

所以,我认为正在发生的事情是,你正在检查的文件的类型,看到它的OLE2,然后轻描淡写地说要HSSF。 HSSF寻找它的工作着,无法看到它们的位,且放弃了

So, what I think is happening is that you're checking the type of the file, seeing it's OLE2, then passing that to HSSF. HSSF looks for the bits it works with, can't see them, and gives up

你需要做的是按照POI加密的文档页面上的说明。基本上,你的code需求是这样的:

What you need to do is follow the instructions on the POI Encrypted Documents page. Basically, your code needs to be something like:

EncryptionInfo info = new EncryptionInfo(filesystem);
Decryptor d = Decryptor.getInstance(info);

try {
    if (!d.verifyPassword(password)) {
        throw new RuntimeException("Unable to process: document is encrypted");
    }

    InputStream dataStream = d.getDataStream(filesystem);
    XSSFWorkbook wb = new XSSFWorkbook(dataStream);

    // Process XLSX file here
} catch (GeneralSecurityException ex) {
    throw new RuntimeException("Unable to process encrypted document", ex);
}

如果您使用最新版的POI,现在会抛出一个 EncryptedDocumentException 如果你给一个加密.xlsx文件到HSSF,这样你就可以制定出更轻松你在哪里错了

If you use the latest version of POI, it'll now throw an EncryptedDocumentException if you give an encrypted .xlsx file to HSSF, so you can work out more easily where you went wrong

这篇关于POI不能打开在Excel中打开工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆