java - 标题读取 xls 文件无效

标签 java excel apache-poi xls

我正在本地系统上读取一个 excel 文件。我正在使用 POI jar 版本 3.7,但出现错误 header 签名无效; 读取 -2300849302551019537 或十六进制 0xE011BDBFEFBDBFEF , 预期 -2226271756974174256 或十六进制 0xE11AB1A1E011CFD0。

用 Excel 打开 xls 文件可以正常工作。

它发生的代码块: 有人有想法吗?

/**
 * create a new HeaderBlockReader from an InputStream
 *
 * @param stream the source InputStream
 *
 * @exception IOException on errors or bad data
 */
public HeaderBlockReader(InputStream stream) throws IOException {
    // At this point, we don't know how big our
    //  block sizes are
    // So, read the first 32 bytes to check, then
    //  read the rest of the block
    byte[] blockStart = new byte[32];
    int bsCount = IOUtils.readFully(stream, blockStart);
    if(bsCount != 32) {
        throw alertShortRead(bsCount, 32);
    }

    // verify signature
    long signature = LittleEndian.getLong(blockStart, _signature_offset);

    if (signature != _signature) {
        // Is it one of the usual suspects?
        byte[] OOXML_FILE_HEADER = POIFSConstants.OOXML_FILE_HEADER;
        if(blockStart[0] == OOXML_FILE_HEADER[0] &&
            blockStart[1] == OOXML_FILE_HEADER[1] &&
            blockStart[2] == OOXML_FILE_HEADER[2] &&
            blockStart[3] == OOXML_FILE_HEADER[3]) {
            throw new OfficeXmlFileException("The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)");
        }
        if ((signature & 0xFF8FFFFFFFFFFFFFL) == 0x0010000200040009L) {
            // BIFF2 raw stream starts with BOF (sid=0x0009, size=0x0004, data=0x00t0)
            throw new IllegalArgumentException("The supplied data appears to be in BIFF2 format.  "
                    + "POI only supports BIFF8 format");
        }

        // Give a generic error
        throw new IOException("Invalid header signature; read "
                              + longToHex(signature) + ", expected "
                              + longToHex(_signature));
    }

最佳答案

只是一个想法,如果你使用 maven,请确保在资源标签中过滤设置为 false。 否则 maven 往往会在复制阶段损坏 xls 文件

关于java - 标题读取 xls 文件无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13949792/

相关文章:

java - 枚举输出错误

java - 使用 jena 列出合格基数限制的资源

java - 在运行时 eclipse 应用程序中建立 mysql/java/jdbc 连接

c# - 有没有办法以编程方式将数据列表从sharepoint下载到excel

excel - 根据条件复制范围

Java swing 示例 - 满世界跑的 Ant 从堆里获取食物?

python - 带有重复标题值的 Pandas read_excel

java - Apache poi - 在现有 .xlsx 文件中添加新页面

java - Apache POI xls 列删除

c# - 如何 IKVM Apache POI