简体   繁体   English

在apache poi 4.0.1中读取xls文件时出现NotOLE2FileException

[英]NotOLE2FileException while reading xls file in apache poi 4.0.1

I am trying to read xls file in java using apache poi 4.0.1 我正在尝试使用apache poi 4.0.1java读取xls文件

Problem 问题

When i try to read the file, i fet following exception 当我尝试读取文件时,出现以下异常

org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature;

Complete stacktrace 完整的堆栈跟踪

org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0x756F4E2C65707954, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:151)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:117)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:294)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:401)
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:382)
    at com.wild.animal.identifier.DataLoader.loadData(DataLoader.java:27)
    at com.wild.animal.identifier.WildAnimalSearch.main(WildAnimalSearch.java:6)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

When i try to open the same file manually in excel i get the following dialog 当我尝试在excel中手动打开同一文件时,出现以下对话框

在此输入图像描述

Question

What am i doing wrong and how can i fix it? 我在做什么错,我该如何解决?

Code

public static void loadData() {

    try {

        File dataFile = new File("Attachment_1551892122.xls");
        FileInputStream fins = new FileInputStream(dataFile);

        HSSFWorkbook workbook = new HSSFWorkbook(fins);
        HSSFSheet sheet = workbook.getSheetAt(0);

        Iterator<Row> rowIterator = sheet.iterator();

        Iterator<Cell> columnIterator;
        Row row;
        Cell column;

        while (rowIterator.hasNext()) {
            row = rowIterator.next();

            columnIterator = row.cellIterator();

            while (columnIterator.hasNext()) {
                column = columnIterator.next();

                switch (column.getCellType()) {
                    case STRING:
                        System.out.print(column.getStringCellValue());
                        break;
                    case BOOLEAN:
                        System.out.print(column.getBooleanCellValue());
                        break;
                    case NUMERIC:
                        System.out.print(column.getNumericCellValue());
                        break;
                }
                System.out.print(" - ");
            }
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

As pointed out in a comment by @AxcelRichter, the file was not a excel file, so i created a new excel file, copied all the data from old file in to new one. 正如@AxcelRichter在评论中指出的那样,该文件不是excel文件,因此我创建了一个新的excel文件,将所有数据从旧文件复制到新文件中。

Tried reading the new file and it worked!! 尝试读取新文件,它起作用了!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.apache.poi.poifs.filesystem.NotOLE2FileException - org.apache.poi.poifs.filesystem.NotOLE2FileException NotOLE2FileException: 无效的标头签名; 读取 0x0000000000000000,预期为 0xE11AB1A1E011CFD0 - 文件似乎不是有效的 OLE2 文档 - NotOLE2FileException: Invalid header signature; read 0x0000000000000000, expected 0xE11AB1A1E011CFD0 - file appears not to be a valid OLE2 document Apache POI获取NPE读取xls文件 - Apache POI gets NPE reading xls file NotOLE2FileException:无效的标头签名; 读取0x0000000000000000,应为0xE11AB1A1E011CFD0 - NotOLE2FileException: Invalid header signature; read 0x0000000000000000, expected 0xE11AB1A1E011CFD0 将 apache poi 从版本 4.0.1 升级到最新版本(版本 4.1.2 和版本 5.0.0)后 xls 文件损坏 - xls file is corrupted after upgrading apache poi from version 4.0.1 to the latest versions ( version 4.1.2 and version 5.0.0) 使用Java和Apache poi从.xls或.xlsx文件中读取内容 - Reading contents from .xls or .xlsx file using Java and Apache poi 使用Apache POI读取xls文件后,逗号转换为点 - Comma convert to dot after reading xls file with Apache POI 使用Apache POI将XLS文件读取为Java中的2D数组 - Reading XLS file using Apache POI into 2D array in Java 使用Apache POI编写xls文件时出现堆错误 - Heap error while writing xls file with Apache POI Apache Poi更新xls文件 - Apache Poi update xls file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM