el文件时出现IndexOutOfBoundsException

el文件时出现IndexOutOfBoundsException

本文介绍了尝试使用Apache POI-HSSF读取MS Excel文件时出现IndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使用 POI-HSSF v3.2 解析MS Excel文件时正在获取IndexOutOfBoundsException.我尝试读取的电子表格不是空的,它是使用MS Excel 2003创建的,POI程序包中包含的BiffViewer解析它没有问题.

Whilst trying to parse MS Excel file using POI-HSSF v3.2 I am getting IndexOutOfBoundsException. The spreadsheet I am trying to read isn't empty it has been created using MS Excel 2003 and BiffViewer included with the POI package has no problem parsing it.

我的代码如下:

package src;

import java.io.*;
import org.apache.poi.hssf.record.*;
import org.apache.poi.hssf.eventusermodel.*;

class Excel implements HSSFListener
{
    public static void main (String[] args) throws Exception
    {
        FileInputStream stream = new FileInputStream("c:\\temp\\a.xls");


        HSSFEventFactory f = new HSSFEventFactory();

        HSSFRequest req = new HSSFRequest();

        req.addListenerForAllRecords(new Excel());

        f.processEvents(req,stream);

        stream.close();
    }

    public void processRecord (Record r)
    {
        System.out.println(r);
    }
}

这是我得到的堆栈跟踪:

And here is the stack trace I am getting:

非常感谢!我知道,我很懒惰,本来可以查看POI来源,但是希望,这里的人能够迅速指出我在代码中所做的任何愚蠢的事情.

Many thanks! I know, I am being plain lazy and could have looked at the POI source myself, but, hopefully, someone here will be able to point out swiftly whatever silly thing I've done within my code.

推荐答案

奥秘解决了,获取输入流的正确方法如下

Mystery solved, the correct way of getting an input stream is as follows

FileInputStream file   = new FileInputStream("c:\\temp\\a.xls");
POIFSFileSystem poifs  = new POIFSFileSystem(file);
InputStream     stream = poifs.createDocumentInputStream("Workbook");

这篇关于尝试使用Apache POI-HSSF读取MS Excel文件时出现IndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:28