当前位置: 首页 > 知识库问答 >
问题:

NotOle2FileException:无效的标头签名;读取0x000000000000000000000000,应为0xE11AB1A1E011CFD0

赏逸春
2023-03-14

我要做的是询问用户是否要创建新的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();
        }

    }
}

后来呢:

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)
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
    File newFile = null;

    if(columnsQuery != null) {
        try {
            excelFileName = eUtil.getFile(FileExtensions.XLS);

            if(excelFileName != null) {

                newFile = new File(excelFileName);

                if(newFile.exists()) {
                    try {
                        workbook =  WorkbookFactory.create(newFile);
                    } catch (EncryptedDocumentException | InvalidFormatException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                else {
                   if (newFile.getName().endsWith(".xls")) {
                       workbook = new HSSFWorkbook();
                   }
                   else if (newFile.getName().endsWith(".xlsx")) {
                       workbook = new XSSFWorkbook();
                   }
                   else {
                       throw new IllegalArgumentException("Must be .xls or .xlsx");
                   }
                }

                excelFileIn = new FileInputStream(newFile);
                exportColsToWorkbook(columnsQuery);

                excelFileOut = new FileOutputStream(newFile);
                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)

共有1个答案

太叔永新
2023-03-14

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

File newFile = new File("output.xlsx");
if (!newFile.exists) { newFile.createNewFile(); }
Workbook wb = WorkbookFactory.create(newFile);

那这就不行了!

只能使用WorkbookFactory加载预先存在的Excel文件。不能使用WorkbookFactory创建全新的空工作簿。与此非常相关的是,如果从InputStream文件创建*SSFWorkbook,则您也不能执行New HSSFWorkbook(emptyStream)New HSSFWorkbook(emptyFile)操作,而这些文件必须存在并填充。

相反,如果您想创建一个全新的空工作簿,那么您需要做的更像是:

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实例,不传入流/文件

 类似资料:
  • 我在Maven构建期间收到此错误。 无法执行目标组织。阿帕奇。专家插件:maven shade插件:2.4.3:项目dl4j上的shade(默认)示例:创建着色jar时出错:无效的LOC头(错误签名)- 这是我的pom。xml文件。 我多次尝试删除jar文件,但似乎都不起作用。

  • 问题内容: 我正在本地系统上读取一个Excel文件。我正在使用POI jar版本3.7,但收到错误无效的标题签名;读取-2300849302551019537或十六进制0xE011BDBFEFBDBFEF,预期为-2226271756974174256或十六进制0xE11AB1A1E011CFD0。 用Excel打开xls文件可以正常工作。 它发生的代码块:有人有想法吗? 问题答案: 只是一个想法

  • 问题内容: 在执行这段(相对简单的)代码时,我得到了这个错误: 其实。有趣的是,我在java-sun-6u32和java-1.6.0-openjdk-amd64上得到它,但是java- sun-7u4成功了。解压缩本身似乎可以解决问题。我猜这意味着这些zipfile可能是由Java 7可以理解的某些较新版本的zip创建的,而以前的版本则不是。但是,任何见识将不胜感激。另外,由于在生产中我真的没有使

  • 我试图使用Apache POI 3.9将XLS文件转换为java中的CSV文件,但是我遇到了一些问题。当尝试转换我需要的文件时,它显示了以下错误: 我认为我使用的代码是完全正确的(它也适用于其他文件)。我认为问题出在XLS文件上,因为当我试图使用微软Excel打开它时,它也向我显示了一个关于文件类型的警告(它说它是微软Excel 3工作表)。有没有办法使用POI打开这些文件?

  • 问题内容: 我已经经历过类似的帖子。我已经尝试了大多数解决方案,但无法摆脱我遇到的错误。希望对此有所帮助。 在将Maven依赖项添加到部署程序集之后,出现了此错误。我将STS与Pivotal tc Server Development Edition v3.1一起使用。我已经检查了tc服务器和java home的版本。两者都使用Java 8。 我不明白问题是什么。 以下是我的pom.xml。当我尝

  • 我正在尝试将OpenAM Saml SSO集成到我的。net 5应用程序。ITfoxtec。Saml2用于处理SP上的身份验证。尝试登录时,会发送有效的post samlResponse。但在验证响应时,我得到以下异常。 确认签名算法正确(http://www.w3.org/2000/09/xmldsig#rsa-sha1) 当手动验证saml响应时(通过浏览器插件saml Chrome面板),我