java - Apache poi 创建错误的 Excel 文件

标签 java excel swing apache apache-poi

我正在使用 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/

相关文章:

vba - 隐藏空单元格

excel - Excel 函数中对单个单词进行拼写检查

java - 功能: Image in a JFrame

java - rxJava buffer() 与尊重背压的时间

java - SQLite - 数据库在 Java 中被锁定

java - JAR 内的 Apache Camel JSch privateKeyFile

java - log4.xml 中用于访问 java 系统属性的表达式语言是什么?

excel - 将 VBS 脚本转换为 Excel VBA

java - 在 Java Swing 应用程序中保留对象值

Java JPanel 重绘问题