HSSFWorkbook.write(FileOutputStream fileOut)没有向响应流写入任何内容

时间:2014-07-21 07:42:11

标签: jsp apache-poi poi-hssf hssf

这是我的代码段

  try{

    response.setHeader("Content-Disposition", "attachment;filename=someName.xls");
    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("new sheet");

    HSSFRow row = sheet.createRow((short)0);

    HSSFCell cell = row.createCell((short)0);
    cell.setCellValue(1);


    row.createCell((short)1).setCellValue(1.2);
    row.createCell((short)2).setCellValue("Rajesh Kumar ");
    row.createCell((short)3).setCellValue(true);

    FileOutputStream fileOut = new FileOutputStream("C:/Excels/workbook.xls");
    wb.write(fileOut);

    fileOut.close();  
    response.getOutputStream().flush();
    } catch ( Exception ex ) {
          ex.printStackTrace();
    }

以上代码段在硬盘上成功创建文件。但是,只要打开Excel工作表,它就是空白的?

是什么原因?什么可以解决?

3 个答案:

答案 0 :(得分:2)

您是否尝试直接使用响应流:

wb.write(response.getOutputStream());
response.getOutputStream().flush();

答案 1 :(得分:-1)

该方法的documentation没有提及关于刷新流的信息。在flush()之前fileOut使用close()

答案 2 :(得分:-1)

  

HSSFWorkbook.write(FileOutputStream fileOut)没有向响应流写入任何内容

正确。它写入你给它的FileOutputStream。

如果您希望它写入响应输出流,请为其提供要写入的响应输出流。

相关问题