尝试使用Apache POI-HSSF读取MS Excel文件时IndexOutOfBoundsException

时间:2021-12-18 20:25:22

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.

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

My code is as follows:

我的代码如下:

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:

这是我得到的堆栈跟踪:

Exception in thread "main" java.lang.IndexOutOfBoundsException at java.io.FileInputStream.readBytes(Native Method) at java.io.FileInputStream.read(FileInputStream.java:199) at org.apache.poi.hssf.record.RecordInputStream.nextRecord(RecordInputStream.java:106) at org.apache.poi.hssf.eventusermodel.HSSFRecordStream.getNextRecord(HSSFRecordStream.java:128) at org.apache.poi.hssf.eventusermodel.HSSFRecordStream.nextRecord(HSSFRecordStream.java:93) at org.apache.poi.hssf.eventusermodel.HSSFEventFactory.genericProcessEvents(HSSFEventFactory.java:141) at org.apache.poi.hssf.eventusermodel.HSSFEventFactory.processEvents(HSSFEventFactory.java:98) at src.Excel.main(Excel.java:21)

位于org.apache.poi.hssf.record.RecordInputStream的java.io.FileInputStream.readBytes(本地方法)中的线程“main”java.lang.IndexOutOfBoundsException中的异常,位于java.io.FileInputStream.read(FileInputStream.java:199) .nextRecord(RecordInputStream.java:106)org.apache.poi.hssf.eventusermodel.HSSFRecordStream.getNextRecord(HSSFRecordStream.java:128)org.apache.poi.hssf.eventusermodel.HSSFRecordStream.nextRecord(HSSFRecordStream.java:93 )org.apache.poi.hssf.eventusermodel.HSSFEventFactory.genericProcessEvents(HSSFEventFactory.java:141)位于src.Excel.main的org.apache.poi.hssf.eventusermodel.HSSFEventFactory.processEvents(HSSFEventFactory.java:98)( Excel.java:21)

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.

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

2 个解决方案

#1


2  

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");

#2


2  

FileInputStream stream =
   new FileInputStream("abcd.xls");  
  HSSFEventFactory f = new HSSFEventFactory(); 
  HSSFRequest req = new HSSFRequest(); 
  req.addListenerForAllRecords(new Excel());  
  Workbook wb;
  wb = new HSSFWorkbook(stream); 
  Sheet sheet=wb.getSheet("abcd11042009");
  int rows=sheet.getPhysicalNumberOfRows();
  Row headerRow;
  Cell cell;
  for(int i=0;i<rows;i++)
  {
    headerRow= sheet.getRow(i);

    cell = headerRow.getCell(1);
    System.out.println("Doing..."+ cell.getStringCellValue());
  }

#1


2  

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");

#2


2  

FileInputStream stream =
   new FileInputStream("abcd.xls");  
  HSSFEventFactory f = new HSSFEventFactory(); 
  HSSFRequest req = new HSSFRequest(); 
  req.addListenerForAllRecords(new Excel());  
  Workbook wb;
  wb = new HSSFWorkbook(stream); 
  Sheet sheet=wb.getSheet("abcd11042009");
  int rows=sheet.getPhysicalNumberOfRows();
  Row headerRow;
  Cell cell;
  for(int i=0;i<rows;i++)
  {
    headerRow= sheet.getRow(i);

    cell = headerRow.getCell(1);
    System.out.println("Doing..."+ cell.getStringCellValue());
  }