Java NotOLE2FileException:头签名无效;读取0x0000000000000000,应为0xE11AB1A1E011CFD0

Java NotOLE2FileException:头签名无效;读取0x0000000000000000,应为0xE11AB1A1E011CFD0,java,excel,apache,apache-poi,Java,Excel,Apache,Apache Poi,我要做的是询问用户是否要创建新的Excel工作簿或选择现有的Excel工作簿。选择现有文件没有问题。但是,当我为新Excel文件创建名称时,会出现一个错误,即“您的文件似乎不是有效的OLE2文档” public void selectExcelFile() { String excelFileName = null; // the name/directory/address of the excel file created

我要做的是询问用户是否要创建新的Excel工作簿或选择现有的Excel工作簿。选择现有文件没有问题。但是,当我为新Excel文件创建名称时,会出现一个错误,即“您的文件似乎不是有效的OLE2文档”

public void selectExcelFile() {
    String excelFileName = null;                                // the name/directory/address of the excel file created/selected
    FileInputStream excelFileIn = null;                         // allows us to connect to the Excel file so we can read it
    FileOutputStream excelFileOut = null;                       // allows us to connect to the Excel file so we can write to it
    ExcelFileUtility eUtil = new ExcelFileUtility();            // used open an excel file

    if(columnsQuery != null) {
        try {
            excelFileName = eUtil.getFile(FileExtensions.XLS); // file extension = ".xls"

            if(excelFileName != null) {
                excelFileIn = new FileInputStream(new File(excelFileName));
                workbook = new HSSFWorkbook(excelFileIn);

                exportColsToWorkbook(columnsQuery);

                excelFileOut = new FileOutputStream(excelFileName);
                workbook.write(excelFileOut);

                // close everything
                workbook.close();
                excelFileIn.close();
                excelFileOut.close();
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
Exception in thread "AWT-EventQueue-0" org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)
at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:216)
at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:166)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:278)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:250)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:229)
at mhhls.him.dbtoexcel.program.DBtoExcel.selectExcelFile(DBtoExcel.java:205)
at mhhls.him.dbtoexcel.program.DBtoExcel.exportToExcel(DBtoExcel.java:487)
at mhhls.him.dbtoexcel.ui.main.DBtoExcelWindow$7.actionPerformed(DBtoExcelWindow.java:190)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
然后:

public String getFile(String extension) {
    String result = null;

    if(extension != null) {
        int choice = askIfNewFile();

        if(choice == 0) { // yes, create new file
            result = createFile(extension);
        } 

        else { // no, select existing file
            result = getFileLocation();                 
        }
    }
    else {
        System.out.println("No file extension.");
    }
    return result;

}

public String createFile(String extension) throws IOException {
    String newFileName = "";
    File newFile = null;
    boolean isCreated = false;

    JFrame frame = new JFrame("Creating a New ." + extension + " File");
    String result = null;

    String dir = getFileDirectory();
    System.out.println("DIR: " + dir);

    if(dir != null) {
        while(newFileName.isEmpty() || newFileName == null) {
            // Used WorkbookUtil.createSafeSheetName to validate file name
            // Please replace if there is a better option
            newFileName = WorkbookUtil.createSafeSheetName(JOptionPane.showInputDialog(frame, "Enter new ." + extension + " file name:"));
        }

        newFile = new File(dir + "\\" + newFileName + "." + extension);
        System.out.println(newFile.toString());

        try {
            isCreated = newFile.createNewFile();

            if(isCreated) {
                result = newFile.getAbsolutePath();
            }
            else {
                System.out.println("File already exists.");
            }
        }
        catch(IOException ioe) {
            System.out.println(ioe);
        }
    }
    return result;
}

public String getFileLocation() {
    String result = null;
    JFileChooser pickFile = new JFileChooser();

    if (pickFile.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        try {
            result = pickFile.getSelectedFile().getCanonicalPath();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // check if file exists
    }
    System.out.println("File location: " + result);

    return result;
}

public String getFileDirectory() {
    String result = null;

    JFileChooser pickFile = new JFileChooser();

    pickFile.setDialogTitle("Choose Folder");
    pickFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    pickFile.setAcceptAllFileFilterUsed(false);

    if (pickFile.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        result = pickFile.getSelectedFile().toString();
    }
    else {
        System.out.println("No Selection ");
    }

    return result;
}
以下是我得到的错误:

org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0x0000000000000000, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:162)
at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:112)
at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:300)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:400)
at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:381)
at mhhls.him.dbtoexcel.program.DBtoExcel.selectExcelFile(DBtoExcel.java:159)
at mhhls.him.dbtoexcel.program.DBtoExcel.exportToExcel(DBtoExcel.java:422)
at mhhls.him.dbtoexcel.ui.main.DBtoExcelWindow$7.actionPerformed(DBtoExcelWindow.java:183)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
在Excel文件工作表上编写工作簿应该是有效的(没有任何错误),但当我打开Excel文件进行检查时,我突然发现:

我点击ok,它只是空的。没有工作表或工作簿

因此,这就解释了为什么我在下次运行程序并尝试在同一Excel文件上编写工作簿时会出现此错误

public void selectExcelFile() {
    String excelFileName = null;                                // the name/directory/address of the excel file created/selected
    FileInputStream excelFileIn = null;                         // allows us to connect to the Excel file so we can read it
    FileOutputStream excelFileOut = null;                       // allows us to connect to the Excel file so we can write to it
    ExcelFileUtility eUtil = new ExcelFileUtility();            // used open an excel file

    if(columnsQuery != null) {
        try {
            excelFileName = eUtil.getFile(FileExtensions.XLS); // file extension = ".xls"

            if(excelFileName != null) {
                excelFileIn = new FileInputStream(new File(excelFileName));
                workbook = new HSSFWorkbook(excelFileIn);

                exportColsToWorkbook(columnsQuery);

                excelFileOut = new FileOutputStream(excelFileName);
                workbook.write(excelFileOut);

                // close everything
                workbook.close();
                excelFileIn.close();
                excelFileOut.close();
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

    }
}
Exception in thread "AWT-EventQueue-0" org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long)
at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:216)
at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.<init>(NPOIFSFileSystem.java:166)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:278)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:250)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:229)
at mhhls.him.dbtoexcel.program.DBtoExcel.selectExcelFile(DBtoExcel.java:205)
at mhhls.him.dbtoexcel.program.DBtoExcel.exportToExcel(DBtoExcel.java:487)
at mhhls.him.dbtoexcel.ui.main.DBtoExcelWindow$7.actionPerformed(DBtoExcelWindow.java:190)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
线程“AWT-EventQueue-0”org.apache.poi.EmptyFileException中的异常:提供的文件为空(零字节长)
位于org.apache.poi.poifs.filesystem.NPOIFSFileSystem.(NPOIFSFileSystem.java:216)
位于org.apache.poi.poifs.filesystem.NPOIFSFileSystem.(NPOIFSFileSystem.java:166)
位于org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:278)
位于org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:250)
位于org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:229)
位于mhls.him.dbtoexcel.program.dbtoexcel.selectExcelFile(dbtoexcel.java:205)
在mhls.him.dbtoexcel.program.dbtoexcel.exportToExcel(dbtoexcel.java:487)
在mhls.him.dbtoexcel.ui.main.DBtoExcelWindow$7.actionPerformed(DBtoExcelWindow.java:190)
在javax.swing.AbstractButton.fireActionPerformed(未知源)
位于javax.swing.AbstractButton$Handler.actionPerformed(未知源)
在javax.swing.DefaultButtonModel.fireActionPerformed(未知源)
位于javax.swing.DefaultButtonModel.setPressed(未知源)
位于javax.swing.plaf.basic.BasicButtonListener.mouseReleased(未知源代码)
位于java.awt.Component.ProcessMouseeEvent(未知源)
位于javax.swing.JComponent.ProcessMouseeEvent(未知源)
位于java.awt.Component.processEvent(未知源)
位于java.awt.Container.processEvent(未知源)
位于java.awt.Component.dispatchEventImpl(未知源)
位于java.awt.Container.dispatchEventImpl(未知源)
位于java.awt.Component.dispatchEvent(未知源)
位于java.awt.LightweightDispatcher.RetargetMouseeEvent(未知源)
位于java.awt.LightweightDispatcher.ProcessMouseeEvent(未知源)
位于java.awt.LightweightDispatcher.dispatchEvent(未知源)
位于java.awt.Container.dispatchEventImpl(未知源)
位于java.awt.Window.dispatchEventImpl(未知源)
位于java.awt.Component.dispatchEvent(未知源)
位于java.awt.EventQueue.dispatchEventImpl(未知源)
位于java.awt.EventQueue.access$500(未知源)
在java.awt.EventQueue$3.run处(未知源)
在java.awt.EventQueue$3.run处(未知源)
位于java.security.AccessController.doPrivileged(本机方法)
位于java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(未知源)
位于java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(未知源)
在java.awt.EventQueue$4.run处(未知源)
在java.awt.EventQueue$4.run处(未知源)
位于java.security.AccessController.doPrivileged(本机方法)
位于java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(未知源)
位于java.awt.EventQueue.dispatchEvent(未知源)
位于java.awt.EventDispatchThread.pumpOneEventForFilters(未知源)
位于java.awt.EventDispatchThread.pumpEventsForFilter(未知源)
位于java.awt.EventDispatchThread.pumpEventsForHierarchy(未知源)
位于java.awt.EventDispatchThread.pumpEvents(未知源)
位于java.awt.EventDispatchThread.pumpEvents(未知源)
位于java.awt.EventDispatchThread.run(未知源)

我只是不明白为什么每次我试图将工作簿写入新工作表时,它都会破坏Excel文件。

您似乎遗漏了一些关键的代码行。但是,假设您当前正在执行以下操作:

File newFile = new File("output.xlsx");
if (!newFile.exists) { newFile.createNewFile(); }
Workbook wb = WorkbookFactory.create(newFile);
那么这就行不通了

您只能使用
WorkbookFactory
加载预先存在的Excel文件。不能使用
WorkbookFactory
创建全新的空工作簿。非常相关的是,如果从
输入流
文件
创建一个
*SSFWorkbook
,则必须存在并填充一个
*SSFWorkbook
,您也不能执行
新建HSSFWorkbook(清空流)
新建HSSFWorkbook(清空文件)

相反,如果您想创建一个全新的空工作簿,则需要执行以下操作:

Workbook wb;
File newFile = new File("output.xlsx");
if (newFile.exists) { 
   // Load existing
   wb = WorkbookFactory.create(newFile);
} else {
   // What kind of file are they trying to ask for?
   // Add additional supported types here
   if (newFile.getName().endsWith(".xls")) {
      wb = new HSSFWorkbook();
   }
   else if (newFile.getName().endsWith(".xlsx")) {
      wb = new XSSFWorkbook();
   }
   else {
      throw new IllegalArgumentException("I don't know how to create that kind of new file");
   }
}

对于全新的空文件,您需要决定要创建什么类型的文件,然后在没有传入流/文件的情况下为其新建相应的
*SSFWorkbook
实例。我对其进行了编辑以添加缺少的代码。同样的情况也适用于-如果您提供了一个文件或流,那么该文件或流必须存在,并且其内容为“无效的头签名;读取0x0000000000000000”-这是否意味着该文件不存在?如果您使用足够新的Apache POI版本(如3.14),最常见的触发方式是空的零字节文件,在零字节文件的情况下,您甚至可以得到一个更有用的异常