页面生成打开excel(运用poi)

 

 

    把excel作为流在页面输出

 

 

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;
 }

你可能感兴趣的:(Excel)