zhongjiezhe168 发表于 2013-1-28 18:12:19

导出excell

public ActionForward downWords(ActionMapping mapping, ActionForm form,
      HttpServletRequest request, HttpServletResponse response)
    {
      Long timestamp =
            Long.parseLong(request.getAttribute("timestamp").toString());
      response.setHeader("Expires", "Mon, 26 Jul 1997 05:00:00 GMT");
      response.setHeader("Last-Modified",
            Common.gmdate("EEE, d MMM yyyy HH:mm:ss", timestamp, "0") + " GMT");
      response.setHeader("Cache-Control", "no-cache, must-revalidate");
      response.setHeader("Pragma", "no-cache");
      response.setHeader("Content-Disposition",
            "attachment; filename=CensorWords.xls");
      response.setHeader("Content-Type", "application/octet-stream");
      List<Words> wordslist = wordsService.getAllWords();
      if (wordslist != null && wordslist.size() > 0)
      {
            ExportExcel excel = new ExportExcel();
            try
            {
                excel.wordsExport(wordslist, response.getOutputStream());
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
      }
      return null;
    }







public boolean wordsExport(List<Words> list, OutputStream os)
    {
      // 创建一个EXCEL
      Workbook wb = new HSSFWorkbook();
      
      // 创建一个SHEET
      Sheet sheet = wb.createSheet("sheet1");
      
      // 创建一行 标题栏
      Row row = sheet.createRow((short)0);
      
      // 填充标题栏
      Cell titleCell = row.createCell(0);
      titleCell.setCellValue("关键字");
      
      Cell titleCell1 = row.createCell(1);
      titleCell1.setCellValue("信息类型");
      
      Cell titleCell2 = row.createCell(2);
      titleCell2.setCellValue("背景颜色");
      
      Cell titleCell3 = row.createCell(3);
      titleCell3.setCellValue("替换字");
      
      for (int i = 1; i <= list.size(); i++)
      {
            // 创建一行 内容栏
            Row row1 = sheet.createRow((short)i);
            
            // 填充不良信息关键字
            Cell contentCell = row1.createCell(0);
            contentCell.setCellValue(list.get(i - 1).getFind());
            
            // 填充不良信息类型
            Cell contentCell1 = row1.createCell(1);
            contentCell1.setCellValue(list.get(i - 1).getType());
            
            // 填充背景颜色
            Cell contentCell2 = row1.createCell(2);
            contentCell2.setCellValue(list.get(i - 1).getColor());
            
            // 填充不良信息替换字
            Cell contentCell3 = row1.createCell(3);
            contentCell3.setCellValue(list.get(i - 1).getReplacement());
            
            // 填充站位单元格
            Cell contentCell4 = row1.createCell(4);
            contentCell4.setCellValue("");
      }
      try
      {
            wb.write(os);
            os.close();
            return true;
      }
      catch (IOException e)
      {
            logger.error(e.getMessage(), e);
            return false;
      }
    }
页: [1]
查看完整版本: 导出excell