Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel found unreadable content in workspace.xlsx (POI - java)

I'm trying to create a workbook from java code. I'm using POI library for this, after executing the program, workbook is successfully creating in my directory, But when i'm trying open my excel file im getting error like "Excel found unreadable content in workspace.xlsx".

public static void main(String args[]) throws InterruptedException{
        Workbook wb = new XSSFWorkbook();
        FileOutputStream fileOut;
        try {
            fileOut = new FileOutputStream("workbook.xls");
            wb.write(fileOut);
            fileOut.close();
            System.out.println("success");
        } catch (Exception e) {
            // TODO Auto-generated catch block
              System.out.println("failure");
            e.printStackTrace();
        }

}

I'm using excel 2010.

like image 975
Rubanraj Ravichandran Avatar asked Mar 24 '15 07:03

Rubanraj Ravichandran


People also ask

Does Apache POI support Xlsx?

Apache POI provides excellent support for working with Microsoft Excel documents. Apache POI is able to handle both XLS and XLSX formats of spreadsheets. Some important points about Apache POI API are: Apache POI contains HSSF implementation for Excel '97(-2007) file format i.e XLS.


1 Answers

Your code is making two mistakes - no sheets (not valid), and wrong extension (XSSFWorkbook = .xlsx)

To create a new empty Excel xlsx file, your code should instead be something like:

Workbook wb = new XSSFWorkbook();
wb.createSheet();
FileOutputStream fileOut;
try {
     fileOut = new FileOutputStream("workbook.xlsx");
     wb.write(fileOut);
     fileOut.close();
     System.out.println("success");
 } catch (Exception e) {
     throw new RuntimeException("failure", e);
 }
like image 162
Gagravarr Avatar answered Oct 01 '22 11:10

Gagravarr