我目前有这段代码可以使用 apache POI 打开一个 xlsx 文件
File existingXlsx = new File("/app/app.xlsx");
System.out.println("File Exists: " + existingXlsx.exists());
Workbook workbook = WorkbookFactory.create(existingXlsx);
当我尝试执行此操作时,我得到以下输出
File Exists: true
java.lang.NullPointerException
at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:270)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:186)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:91)
我试图打开的文件可以在 Excel 中打开并正确显示数据,我该怎么做才能让 POI 读取 XLSX 文件?
这是破坏的文件;
https://mega.co.nz/#!FJMWjQKI!CzihQgMVpxOQDTXzSnb3UFYSKbx4yFTb03-LI3iLmkE
编辑
我也试过,这会导致同样的错误;
Workbook workbook = new XSSFWorkbook(new FileInputStream(existingXlsx));
编辑
我找到了引发异常的那一行;
WorkbookDocument doc = WorkbookDocument.Factory.parse(getPackagePart().getInputStream());
this.workbook = doc.getWorkbook();
Map<String, XSSFSheet> shIdMap = new HashMap<String, XSSFSheet>();
for(POIXMLDocumentPart p : getRelations())
{
if(p instanceof SharedStringsTable) sharedStringSource = (SharedStringsTable)p;
else if(p instanceof StylesTable) stylesSource = (StylesTable)p;
else if(p instanceof ThemesTable) theme = (ThemesTable)p;
else if(p instanceof CalculationChain) calcChain = (CalculationChain)p;
else if(p instanceof MapInfo) mapInfo = (MapInfo)p;
else if (p instanceof XSSFSheet) {
shIdMap.put(p.getPackageRelationship().getId(), (XSSFSheet)p);
}
}
stylesSource.setTheme(theme); <== BREAKS HERE
编辑
经过一些研究 POI 似乎无法找到 styles.xml 和 workbook.xml,我觉得这很奇怪,因为像 TextWrangler 这样显示存档结构的简单阅读器向我显示了样式 xml。
我该如何解决?是否有可以插入存档的默认 styles.xml 和 workbook.xml?