以下代码适用于 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();
}
}
}
您能否提一些建议?谢谢,