如何使用Apache POI打开2007 xlsx ooxml文件?我已经将所有内容添加到了我的类路径中,但仍然遇到此java.lang.NullPointerException
错误:
"C:\Program Files\Java\jdk1.7.0_13\bin\java" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:1804,suspend=y,server=n -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_13\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_13\jre\lib\ext\zipfs.jar;C:\Users\Patrick\Desktop\xmlChanger\out\production\xmlChanger;C:\poi-3.9\poi-3.9-20121203.jar;C:\poi-3.9\poi-examples-3.9-20121203.jar;C:\poi-3.9\poi-excelant-3.9-20121203.jar;C:\poi-3.9\poi-ooxml-3.9-20121203.jar;C:\poi-3.9\poi-ooxml-schemas-3.9-20121203.jar;C:\poi-3.9\poi-scratchpad-3.9-20121203.jar;C:\poi-3.9\lib\commons-codec-1.5.jar;C:\poi-3.9\lib\commons-logging-1.1.jar;C:\poi-3.9\lib\junit-3.8.1.jar;C:\poi-3.9\lib\log4j-1.2.13.jar;C:\poi-3.9\ooxml-lib\dom4j-1.6.1.jar;C:\poi-3.9\ooxml-lib\stax-api-1.0.1.jar;C:\poi-3.9\ooxml-lib\xmlbeans-2.3.0.jar;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 12.0.1\lib\idea_rt.jar" Main
Connected to the target VM, address: '127.0.0.1:1804', transport: 'socket'
java.lang.NullPointerException
at org.apache.poi.openxml4j.opc.OPCPackage.getPart(OPCPackage.java:625)
at org.apache.poi.POIXMLDocumentPart.<init>(POIXMLDocumentPart.java:91)
at org.apache.poi.POIXMLDocument.<init>(POIXMLDocument.java:56)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:183)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:73)
at Main.main(Main.java:178)
Disconnected from the target VM, address: '127.0.0.1:1804', transport: 'socket'
Process finished with exit code 0
我的代码如下:
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.Iterator;
public class Main {
public static void main(String[] args) {
try {
File file = new File(root_dir + "2013-03-13 iom diff.xlsx");
Workbook workbook = WorkbookFactory.create(file); //fails here
//...
} catch (Exception e) {
e.printStackTrace();
}
}
}
我再次搜索过这个但是没有找到任何有用的答案(例如确保目录路径正确,超出范围等)。
之前我问了一个类似的问题,但错误不同,因为我相信在使用Microsoft Excel保存时,我只使用普通的xlsx格式而不是OOXML xlsx格式。
这个空指针异常的来源或者抛出异常的方法更有意义吗?
修改
当我使用以下代码创建工作簿时,会打开上面显示的代码:
Workbook wb = new XSSFWorkbook();
CreationHelper createHelper = wb.getCreationHelper();
Sheet sheet = wb.createSheet("new sheet");
Row r = sheet.createRow((short) 0);
Cell cell = r.createCell(0);
cell.setCellValue(1);
r.createCell(1).setCellValue(1.2);
r.createCell(2).setCellValue(createHelper.createRichTextString("This is a string"));
r.createCell(3).setCellValue(true);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
wb.write(fileOut);
fileOut.close();
这意味着格式无效,但我不确定这笔交易是什么,因为我在Excel中保存为OOXML