【问题标题】:Apache poi creates Excel file with errorApache poi 创建 Excel 文件时出错
【发布时间】:2014-07-17 17:22:14
【问题描述】:

我正在使用 Apache_poi 制作 Excel 文件。该文件将在我的 Swing 应用程序中按钮的操作事件上创建。 Excel 工作表创建得很好,但是当我打开它时,我得到了这个错误:

您尝试打开的文件与文件扩展名指定的格式不同

当我打开文件时,它完全是空的。

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)
    {
    }
}  

【问题讨论】:

    标签: java excel swing apache apache-poi


    【解决方案1】:

    您忘记在您的Workbook 上拨打write

    在关闭流之前添加此行:

          wb.write(output);
    

    【讨论】:

    • 感谢你一百万次。@Jonathan
    猜你喜欢
    • 1970-01-01
    • 2019-07-23
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-11
    相关资源
    最近更新 更多