public ActionForward generateExcel(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
response.setContentType("application/ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "inline;filename=sheet1.xls");
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
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("This is a string");
row.createCell((short)3).setCellValue(true);
OutputStream fileOut = response.getOutputStream();
wb.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}
希望大家探讨一下原因
问题补充
后来终于知道原因了。
public ActionForward generateExcel(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
response.reset();//少了这一句
response.setContentType("application/ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "inline;filename=sheet1.xls");
try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
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("This is a string");
row.createCell((short)3).setCellValue(true);
OutputStream fileOut = response.getOutputStream();
wb.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return null;
}