Excel在workspace.xlsx中找到了不可读的内容(POI - java)

Rub*_*ran 6 java excel apache-poi

我正在尝试从java代码创建工作簿.我正在使用POI库,在执行程序后,工作簿在我的目录中成功创建,但是当我尝试打开我的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();
        }
Run Code Online (Sandbox Code Playgroud)

}

我正在使用excel 2010.

Gag*_*arr 5

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

要创建一个新的空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);
 }
Run Code Online (Sandbox Code Playgroud)