我收到此错误:
org.apache.poi.poifs.filesystem.OfficeXmlFileException:提供的数据似乎位于Office 2007 XML中 . 您正在调用处理OLE2 Office文档的POI部分 . 您需要调用POI的不同部分来处理此数据(例如XSSF而不是HSSF)
我读了扔谷歌,我发现我需要使用XSSF而不是HSSF,因为我的Excel文件是xlsx,但正如你在我的maven中看到的,我已经在使用xlsx了 . 我哪里出错了?
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13-beta1</version>
</dependency>
产生异常的代码是:
POIFSFileSystem fs;
fs = new POIFSFileSystem(new FileInputStream(getFilePath()));
我的新代码
public void getBColum() {
try {
OPCPackage fs;
fs = new OPCPackage.open(new File(getFilePath()));
XSSFWorkbook wb = new XSSFWorkbook(fs);
XSSFSheet sheet = wb.getSheet("Master column name - Used Car");
XSSFRow row;
CellReference cr = new CellReference("A1");
row = sheet.getRow(cr.getCol());
System.out.println(row.getCell(3));
} catch (FileNotFoundException e) {
if (logger.isDebugEnabled()) {
logger.debug("How can this error be possible? we should have already thrown an exception in the construction");
}
} catch (IOException e) {
logger.error(String.format("Exception in reading the file: %s",
e.getMessage()));
}
}
我在 new oPCPackage.open
中有一个编译错误,它是:
OPCPackage.open无法解析为某种类型
2 回答
根据Apache POI Quick Guide,
POIFSFileSystem
(或类似地,NPOIFSFileSystem
)仅用于.xls(Excel版本到2003)文档 ..xlsx文档(Excel 2007)的等效项是
OPCPackage
.您可以从
OPCPackage
创建XSSFWorkbook
:或者您可以直接创建它:
通常,最好使用
File
而不是InputStream
来创建工作簿,以节省内存 .此外,如果您想要的代码不关心它是.xls还是.xlsx:
我正在使用XSSF和xlsx文件,但是当我尝试处理使用密码加密/保护的文件时出现此错误 .
一旦我删除了密码,一切都按预期工作 .