I am getting this error:
我得到了这个错误:
org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (e.g. XSSF instead of HSSF)
org.apache.poi.poifs.filesystem。OfficeXmlFileException:提供的数据显示在Office 2007+ XML中。您正在调用处理OLE2办公文档的POI部分。您需要调用POI的不同部分来处理这些数据(例如,XSSF而不是HSSF)
I read throw Google and I found out that I need to use XSSF instead of HSSF because my Excel file is xlsx, but as you see in my maven, I am already using xlsx. Where have I gone wrong please?
我阅读了throw谷歌,发现我需要使用XSSF而不是HSSF,因为我的Excel文件是xlsx,但是正如您在我的maven中看到的,我已经在使用xlsx。请问哪里出错了?
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13-beta1</version>
</dependency>
The code the makes the exception is:
例外的代码是:
POIFSFileSystem fs;
fs = new POIFSFileSystem(new FileInputStream(getFilePath()));
My new code
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()));
}
}
I have a compile error in new oPCPackage.open
which is:
我在新的oPCPackage中有一个编译错误。开放的:
OPCPackage.open cannot be resolved to a type
OPCPackage。不能将open解析为一个类型
1 个解决方案
#1
18
According to the Apache POI Quick Guide, the POIFSFileSystem
(or similarly, NPOIFSFileSystem
) is only used with .xls (Excel versions through 2003) documents.
根据Apache POI快速指南,poifsfiles(或类似的NPOIFSFileSystem)仅用于.xls (Excel版本,2003年)文档。
The equivalent for .xlsx documents (Excel 2007+) is OPCPackage
.
xlsx文档(Excel 2007+)的等效项是OPCPackage。
OPCPackage pkg = OPCPackage.open(new File("file.xlsx"));
You can create an XSSFWorkbook
from the OPCPackage
:
您可以从OPCPackage中创建一个XSSFWorkbook:
XSSFWorkbook wb = new XSSFWorkbook(pkg);
Or you can just create it directly:
或者你可以直接创建它:
XSSFWorkbook wb = new XSSFWorkbook(new File("file.xlsx"));
Generally it's better to create the workbook using a File
instead of an InputStream
, to save memory.
通常最好使用文件而不是InputStream来创建工作簿,以保存内存。
Also, if you want code that doesn't care whether it's an .xls or an .xlsx:
同样,如果你想要的代码不关心它是.xls还是.xlsx:
// or "file.xlsx"
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls"));
#1
18
According to the Apache POI Quick Guide, the POIFSFileSystem
(or similarly, NPOIFSFileSystem
) is only used with .xls (Excel versions through 2003) documents.
根据Apache POI快速指南,poifsfiles(或类似的NPOIFSFileSystem)仅用于.xls (Excel版本,2003年)文档。
The equivalent for .xlsx documents (Excel 2007+) is OPCPackage
.
xlsx文档(Excel 2007+)的等效项是OPCPackage。
OPCPackage pkg = OPCPackage.open(new File("file.xlsx"));
You can create an XSSFWorkbook
from the OPCPackage
:
您可以从OPCPackage中创建一个XSSFWorkbook:
XSSFWorkbook wb = new XSSFWorkbook(pkg);
Or you can just create it directly:
或者你可以直接创建它:
XSSFWorkbook wb = new XSSFWorkbook(new File("file.xlsx"));
Generally it's better to create the workbook using a File
instead of an InputStream
, to save memory.
通常最好使用文件而不是InputStream来创建工作簿,以保存内存。
Also, if you want code that doesn't care whether it's an .xls or an .xlsx:
同样,如果你想要的代码不关心它是.xls还是.xlsx:
// or "file.xlsx"
Workbook wb = WorkbookFactory.create(new File("MyExcel.xls"));