有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Apache poi创建Excel文件时出错

我正在使用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)
    {
    }
}  

共 (1) 个答案

  1. # 1 楼答案

    你忘了给你的Workbook打电话了

    在关闭流之前添加以下行:

          wb.write(output);