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.
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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With