Exception when reading excel file using apache poi

0
I'm trying to read an Excel (XLS) file using the Apache Poi API, and I'm taking the exception :

java.io.IOException: Invalid header signature; read 0x0020000A000DFEFF, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:140)
    at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
    at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:128)
    at br.com.sfera.utility.LerXLS.lerArquivoXls(LerXLS.java:32)
    at br.com.sfera.utility.LerXLS.main(LerXLS.java:21)

Below is the java method that reads the xls file.

public void lerArquivoXls(String arquivoXls)
    {
        try {
            InputStream input = new BufferedInputStream(new FileInputStream(arquivoXls));

            POIFSFileSystem fileSystem = new POIFSFileSystem(input);
            HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
            HSSFSheet sheet = workbook.getSheetAt(0);

            Iterator linhas = sheet.rowIterator();
            while(linhas.hasNext()){
                HSSFRow linha = (HSSFRow) linhas.next();
                Iterator celulas = linha.cellIterator();
                while(celulas.hasNext()){
                    HSSFCell celula = (HSSFCell) celulas.next();
                    if(HSSFCell.CELL_TYPE_NUMERIC==celula.getCellType()){
                        //celula numerica
                        System.out.println(celula.getNumericCellValue()+" - ");
                    }
                    else if(HSSFCell.CELL_TYPE_STRING==celula.getCellType()){
                        //celula de string
                        System.out.println(celula.getStringCellValue() + " - ");
                    }else if(HSSFCell.CELL_TYPE_BOOLEAN==celula.getCellType()){
                        //celula booleana
                        System.out.println(celula.getBooleanCellValue() + " - ");
                    }else if(HSSFCell.CELL_TYPE_BLANK==celula.getCellType()){
                        //celula em branco
                        System.out.println(" == VAZIO == ");
                    }else{
                        //celula desconhecida
                        System.out.println(" == FORMATO DESCONHECIDO == ");
                    }
                }
            }



        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Can anyone help me, how can I avoid this problem, or what is the cause of avoiding this exception and can read the XLS file without problems?

    
asked by anonymous 11.07.2014 / 14:57

1 answer

1

Use the method below to select the file. Here it worked:

JFileChooser chooser = new JFileChooser();
        chooser.showDialog(chooser, "Selecionar");
        File arquivo = chooser.getSelectedFile();
        BufferedInputStream buf = new BufferedInputStream(new FileInputStream(arquivo));
    
01.12.2014 / 19:30