Question

I am new to Java and am having an issue importing my Excel sheet. I am trying to print out an Excel spreadsheet into the Java console using the Apache Poi library. When I run my code, I get the following exception:

java.io.IOException: Invalid header signature; read 0x656D614E2C234449, expected 0xE11AB1A1E011CFD0

How can i fix it so it matches my header? Which is like this...

ID#, Name, Age, Street Address, City, State, Zip, Employer, Prescription

Below is my code:

//imports, blah blah, etc...

    public static void main( String [] args ) {

        try {

            InputStream input = new BufferedInputStream(new FileInputStream(
                "c:/Users/sgoetz/Desktop/Week3ProgrammingData.csv"));
            POIFSFileSystem fs = new POIFSFileSystem( input );
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            HSSFSheet sheet = wb.getSheetAt(0);

            Iterator rows = sheet.rowIterator();

            while( rows.hasNext() ) {  
                HSSFRow row = (HSSFRow) rows.next();
                System.out.println("\n");
                Iterator cells = row.cellIterator();

                while( cells.hasNext() ) {
                    HSSFCell cell = (HSSFCell) cells.next();

                    if(HSSFCell.CELL_TYPE_NUMERIC==cell.getCellType())
                        System.out.print( cell.getNumericCellValue()+"     " );
                    else
                        if(HSSFCell.CELL_TYPE_STRING==cell.getCellType())
                            System.out.print( cell.getStringCellValue()+"     " );
                        else
                            if(HSSFCell.CELL_TYPE_BOOLEAN==cell.getCellType())
                                System.out.print( cell.getBooleanCellValue()+"     " );
                            else
                                if(HSSFCell.CELL_TYPE_BLANK==cell.getCellType())
                                    System.out.print( "BLANK     " );
                                else
                                    System.out.print("Unknown cell type");
                }
            }
        } catch ( IOException ex ) {
            ex.printStackTrace();
        }
    }
}
Était-ce utile?

La solution 2

"Invalid header signature" is about the header that identifies the version of Excel that was used to create the Excel file. If the file is not an Excel file or is a pre-97 Excel file, you'll get this error.

Autres conseils

POI - the library you are using with all the HSSF classes etc - is used to import and export Excel documents (.xls, .xlsx). You are trying to use it on a CSV document, which will not work.

I'd recommend instead a library like SuperCSV for that.

Is this right ?

   InputStream input = new BufferedInputStream(
               new FileInputStream("c:/Users/sgoetz/Desktop/Week3ProgrammingData.csv"));

I'd expect POI to actually read an Excel-format (Microsoft's OLE 2 Compound document) file, not a .csv file.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top