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

标签 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();
        }

我使用的是 excel 2010。

最佳答案

您的代码犯了两个错误 - 没有工作表(无效)和错误的扩展名 (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);
 }

关于java - Excel 在 workspace.xlsx 中发现不可读的内容(POI - java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29226960/

相关文章:

excel - 当函数名称位于单元格中时如何调用 Excel VBA 函数

c# - 加载 Office.dll 时出错 COMException : 'Errorloading type library/DLL'

Java POI : How to read Excel cell value and not the formula computing it?

java - 一个列表如何在 java 中包含不同的类型?

java - 递归的空间复杂度

excel - Excel VBA 中的长变量错误 6

java - 如何使用java api合并两个具有图表和表格的pptx文件?

excel-formula - poi 中的 IRR 返回 NaN 但 excel 中的值正确

java - Android 中线程如何与监听器交互?

java - 为什么我的循环在第一次之后会跳过 if/else 语句?