我正在尝试从 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/