当我尝试初始化Workbook对象时,我总是收到此错误:
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 (eg XSSF instead of HSSF)
但我按照办公室样本执行此操作,以下是我的代码:
File inputFile = new File(inputFileName);
InputStream is = new FileInputStream(inputFile);
Workbook wb = new XSSFWorkbook(is);
在代码行发生异常:
Workbook wb = new XSSFWorkbook(is);
这是POI jar包括:
poi-3.8-20120326.jar
poi-ooxml-3.8-20120326.jar
poi-ooxml-schemas-3.8-20120326.jar
xmlbeans-2.3.0.jar
任何人都可以给我指导吗?将显示如何阅读完整的Excel 2007文档的示例。提前谢谢!
答案 0 :(得分:3)
我假设您已经重新检查过您的原始文件确实是Office 2007 + XML格式,对吗?
修改强>
然后,如果您确定格式正常,并且它适用于您使用WorkbookFactory.create,您可以在此类方法的代码中找到答案:
/**
* Creates the appropriate HSSFWorkbook / XSSFWorkbook from
* the given InputStream.
* Your input stream MUST either support mark/reset, or
* be wrapped as a {@link PushbackInputStream}!
*/
public static Workbook create(InputStream inp) throws IOException, InvalidFormatException {
// If clearly doesn't do mark/reset, wrap up
if(! inp.markSupported()) {
inp = new PushbackInputStream(inp, 8);
}
if(POIFSFileSystem.hasPOIFSHeader(inp)) {
return new HSSFWorkbook(inp);
}
if(POIXMLDocument.hasOOXMLHeader(inp)) {
return new XSSFWorkbook(OPCPackage.open(inp));
}
throw new IllegalArgumentException("Your InputStream was neither an OLE2 stream, nor an OOXML stream");
}
这是你遗漏的一点:new XSSFWorkbook(OPCPackage.open(inp))