NotOLE2FileException: Invalid header signature; read 0x0000000000000000, expected 0xE11AB1A1E011CFD0 - file appears not to be a valid OLE2 document

Viewed 26630
try {
        File file = new File("file4.xls");
        if (!file.exists()) file.createNewFile();
        FileInputStream fis = new FileInputStream(file);
        POIFSFileSystem fileSystem = new POIFSFileSystem(fis);
        //   FileInputStream fis = new FileInputStream(file);
        HSSFWorkbook workbook = new HSSFWorkbook(fileSystem);
        //Sheet sheet = workbook.createSheet("sheet0");
        Sheet sheet = workbook.getSheet("sheet1");
        sheet.createRow(0).createCell(0).setCellValue("HelloWorld");
        Cell cell = sheet.createRow(1).createCell(0);
        cell.setCellValue("Value_1_1");
        fis.close();
        FileOutputStream fos = new FileOutputStream(file);
        workbook.write(fos);
        workbook.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

I am trying to create simple excel file using java. I am getting above mentioned error.

2 Answers

Open the file once. Check your file once on click save as. If it is generated from HTML it will show as Web page(*.htm , .html) below drop down to the file. You can save as Excel 97-2003 Workbook (.xls) and retry to read from code. It worked in my case.

Related