我正在使用 Apache_poi 制作一个 Excel 文件。该文件将根据我的 Swing 应用程序中按钮的操作事件创建。 Excel 工作表创建得很好,但是当我打开它时,出现此错误:
The file you are trying to open is in different format than specified by the file extension
当我打开文件时,它完全是空的。
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
try {
// String personalclass=jComboBox1.getSelectedItem().toString();
// String ac="academic";
// FileOutputStream output=new FileOutputStream("D:\\"+personalclass+ac+".xls");
FileOutputStream output=new FileOutputStream("D:\\workbook.xls");
Workbook wb=new HSSFWorkbook();
Sheet sheet =wb.createSheet("Class 1");
Cell cell=sheet.createRow(0).createCell(3);
cell.setCellValue("Thisisatestofmerging");
//sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
output.close();
}
catch(Exception e)
{
}
}
最佳答案
您忘记在您的Workbook
上调用write
。
在关闭流之前添加此行:
wb.write(output);
关于java - Apache poi 创建错误的 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24809803/