将 apache poi 从 4.0.1 版升级到最新版本4.1.2 版和 5.0.0 版后,xls 文件损坏

问题描述

以下代码适用于 apache poi 4.0.1 版,但在将 apache poi 升级到最新版本(4.1.2 版或 5.0.0 版)后,生成的 xls 文件已损坏,当我尝试打开它时无法打开找到里面的任何数据。 (升级poi.jar后xls文件大小从4KO更新为0KO。)

import java.io.FileOutputStream;
import org.apache.poi.hssf.usermodel.hssfWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public abstract class ExcelExporter
{ 
    public static void exportPanel(String account,JTable table) {
        FileOutputStream excel = null;
        try {   
            Workbook wb = new hssfWorkbook();
            Sheet sh = wb.createSheet("hello");
            Row row = sh.createRow(0);
            Cell cell = row.createCell(0);
            cell.setCellValue(1);
            excel = new FileOutputStream("WORKBOOK.xls");
            wb.write(excel);
            wb.close();
            excel.flush();
            excel.close();
        } catch (Exception ex) {
            ex.printstacktrace();
        }   
    }
}

你能指点一下吗? 谢谢,

enter image description here enter image description here

解决方法

通过将 commons-math3.jar 添加到我的项目中解决了问题。