opening excel ooxml 2007 xlsx document returns nul

2019-07-11 05:48发布

问题:

How do I open a 2007 xlsx ooxml file with Apache POI? I've added everything to my class path but keep getting this java.lang.NullPointerException error:

"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

My code is as follows:

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();
        }
    }
}

Again I have searched already for this but haven't found any answers that worked (e.g. making sure the directory path is correct, going out of bounds etc.)

I asked a similar question before but the error was different because I believe I was using just the normal xlsx format and not the OOXML xlsx format when saving in Microsoft Excel.

Any ideas where this null pointer exception is coming from or a way of making the exception thrown more meaningful?

EDIT

When I use the following code to create a workbook, it opens with the code posted above:

        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();

This implies an invalid format but I'm not sure what the deal is because I saved as OOXML in Excel