我收到此错误:
org.apache.poi.poifs.filesystem.OfficeXmlFileException:提供的数据似乎位于 Office 2007+ XML 中。您正在调用 POI 中处理 OLE2 Office 文档的部分。您需要调用 POI 的不同部分来处理此数据(例如 XSSF 而不是 HSSF)
我读过 throw Google,发现我需要使用 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 无法解析为类型
根据Apache POI 快速指南 http://poi.apache.org/spreadsheet/quick-guide.html#FileInputStream, the POIFSFileSystem
(或者类似地,NPOIFSFileSystem
) 仅与 .xls(2003 年之前的 Excel 版本)文档一起使用。
.xlsx 文档 (Excel 2007+) 的等效项是OPCPackage
.
OPCPackage pkg = OPCPackage.open(new File("file.xlsx"));
您可以创建一个XSSFWorkbook
来自OPCPackage
:
XSSFWorkbook wb = new XSSFWorkbook(pkg);
或者你可以直接创建它:
XSSFWorkbook wb = new XSSFWorkbook(new File("file.xlsx"));
一般来说,最好使用File
而不是InputStream
,以节省内存。
另外,如果您想要的代码不关心它是 .xls 还是 .xlsx:
// or "file.xlsx"
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls"));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)