日期:2014-05-16  浏览次数:21015 次

Poi3.9 写入时org.apache.poi.hssf.record.RecordInputStream$LeftoverDataException:
POIFSFileSystem fs =new POIFSFileSystem(new FileInputStream(file));  
   
        HSSFWorkbook workBook= new HSSFWorkbook(fs);  
   
    
   
org.apache.poi.hssf.record.RecordInputStream$LeftoverDataException: Initialisation of record 0x894 left 7 bytes remaining still to be read.  
   
    at org.apache.poi.hssf.record.RecordInputStream.hasNextRecord(RecordInputStream.java:156)  
   
    at org.apache.poi.hssf.record.RecordFactoryInputStream.nextRecord(RecordFactoryInputStream.java:231)  
   
    at org.apache.poi.hssf.record.RecordFactory.createRecords(RecordFactory.java:443)  
   
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:285)  
   
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:248)  
   
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:192)  
   
    at org.apache.poi.hssf.usermodel.HSSFWorkbook.<init>(HSSFWorkbook.java:174) 



解决方案:重写POIjar包中RecordInputSream.java类的hasNextRecord()方法如下


public boolean hasNextRecord() throws LeftoverDataException {
  if (_currentDataLength != -1 && _currentDataLength != _currentDataOffset) {
readToEndOfRecord();
  }
  if (_currentDataLength != DATA_LEN_NEEDS_TO_BE_READ) {
_nextSid = readNextSid();
  }
  return _nextSid != INVALID_SID_VALUE;
}

private void readToEndOfRecord(){
  while(this._currentDataOffset<this._currentDataLength)
      readByte();
}