Excel在workspace.xlsx中发现不可读的内容(POI-java)

我试图从Java代码创build一个工作簿。 我正在使用POI库,执行该程序后,工作簿正在我的目录中成功创build,但是当我试图打开我的Excel文件即时通讯错误,如“Excel在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(); } 

}

我正在使用Excel 2010。

你的代码犯了两个错误 – 没有工作表(无效)和错误的扩展名(XSSFWorkbook = .xlsx)

要创build一个新的空Excel XLSX文件,您的代码应该是这样的:

 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); }