You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by "Srinivas Araru (sararu)" <sa...@cisco.com> on 2004/10/23 03:47:30 UTC

IOException when POI reading Excel File Invalid header signature

Hi,
 
I am trying to read the attached Excel file using the following sample code:
 
If something in the Excel file is changed and saved, I do not see the
exception. It looks like the Excel file or the format of the file is
corrupted.
 
/**
* Start of Sample Program. EventExample.java
**/
import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFRequest;
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
import org.apache.poi.hssf.record.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.hssf.record.RowRecord;
 
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStream;
 
/**
 * This example shows how to use the event API for reading a file.
 */
public class EventExample
        implements HSSFListener
{
    private SSTRecord sstrec;
    public static int count;
    /**
     * This method listens for incoming records and handles them as
required.
     * @param record    The record that was found while reading.
     */
    public void processRecord(Record record)
    {
        count++;
        //System.out.println ("Record size " );
        switch (record.getSid())
        {
            // the BOFRecord can represent either the beginning of a sheet
or the workbook
 
            case BOFRecord.sid:
                BOFRecord bof = (BOFRecord) record;
                if (bof.getType() == bof.TYPE_WORKBOOK)
                {
                    System.out.println("Encountered workbook");
                    // assigned to the class level member
                } else if (bof.getType() == bof.TYPE_WORKSHEET)
                {
                    System.out.println("Encountered sheet reference");
                }
                break;
            case RowRecord.sid:
                RowRecord rowrec = (RowRecord) record;
                System.out.println("Row " + rowrec.getRowNumber() + " found,
first column at "
                        + rowrec.getFirstCol() + " last column at " +
rowrec.getLastCol());
                break;
           /* case NumberRecord.sid:
                NumberRecord numrec = (NumberRecord) record;
                System.out.println("Cell found with value " +
numrec.getValue()
                        + " at row " + numrec.getRow() + " and column " +
numrec.getColumn());
                break;
                // SSTRecords store a array of unique strings used in
Excel.*/
            case SSTRecord.sid:
                sstrec = (SSTRecord) record;
                for (int k = 0; k < sstrec.getNumUniqueStrings(); k++)
                {
                    System.out.println("String table value in row "  + k + "
= " + sstrec.getString(k));
                }
                break;
            case LabelSSTRecord.sid:
                LabelSSTRecord lrec = (LabelSSTRecord) record;
                System.out.println("String cell found with value "
                        + sstrec.getString(lrec.getSSTIndex()));
                break;
            case BlankRecord.sid:
                BlankRecord brec = (BlankRecord) record;
                System.out.println ("Blank Record " + brec.getRow());
                break;
           default:
                //System.out.println ("Default Setting " +
record.toString());
                break;
        }
    }
 
    /**
     * Read an excel file and spit out what we find.
     *
     * @param args      Expect one argument that is the file to read.
     * @throws java.io.IOException  When there is an error processing the
file.
     */
    public static void main(String[] args) throws IOException
    {
        // create a new file input stream with the input file specified
        // at the command line
        FileInputStream fin = new FileInputStream(args[0]);
        // create a new org.apache.poi.poifs.filesystem.Filesystem
        POIFSFileSystem poifs = new POIFSFileSystem(fin);
        // get the Workbook (excel part) stream in a InputStream
        InputStream din = poifs.createDocumentInputStream("Workbook");
        // construct out HSSFRequest object
        HSSFRequest req = new HSSFRequest();
        // lazy listen for ALL records with the listener shown above
        req.addListenerForAllRecords(new EventExample());
        // create our event factory
        HSSFEventFactory factory = new HSSFEventFactory();
        // process our events based on the document input stream
        factory.processEvents(req, din);
        // once all the events are processed close our file input stream
        fin.close();
        // and our document input stream (don't want to leak these!)
        din.close();
        System.out.println("done." + count);
    }
}
 
/**
* End of Program
**/
 
I get the following error:
 
java.io.IOException: Invalid header signature; read 4503599627764745,
expected -2226271756974174256
 at
org.apache.poi.poifs.storage.HeaderBlockReader.<init>(HeaderBlockReader.java
:88)
 at
org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:
83)
 at EventExample.main(EventExample.java:89)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.intellij.rt.execution.application.AppMain.main(Unknown Source)
Exception in thread "main" Process terminated with exit code 1
 
I am trying to find if a solution exists for this bug.