[Java] JXL导出Excel文件 →→→→→进入此内容的聊天室

来自 , 2019-02-16, 写在 Java, 查看 137 次.
URL http://www.code666.cn/view/f8f122d5
  1. /**
  2.      * 导出为Excel
  3.      * @param cdosCategoryKeyWords
  4.      * @return
  5.      */
  6.     public boolean exportExcel(CDO[] cdosCategoryKeyWords)
  7.         {
  8.                 HttpServletResponse response=ServletActionContext.getResponse();
  9.                 OutputStream os=null;
  10.                 try
  11.                 {
  12.                         os = response.getOutputStream();
  13.                 }
  14.                 catch (IOException e1)
  15.                 {
  16.                         mallLog.error("获取response输出流出错");
  17.                 }
  18.                 Calendar cal = Calendar.getInstance();
  19.                 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
  20.                 String curDatetime = sdf.format(cal.getTime());
  21.             response.reset();
  22.         response.setHeader("Content-disposition", "attachment; filename=CategoryKeyWords"+curDatetime+".xls");
  23.             response.setContentType("application/msexcel");
  24.                 WritableWorkbook wwb=null;
  25.                 WritableSheet ws=null;
  26.                 try
  27.                 {
  28.                         wwb = Workbook.createWorkbook(os);
  29.                         ws=wwb.createSheet("关键词类目匹配列表",0);
  30.                         ws.getSettings().setDefaultColumnWidth(15);
  31.                         //创建表头
  32.                         WritableFont wfc = new WritableFont(WritableFont.ARIAL,10,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.RED);
  33.             WritableCellFormat wcfFC = new WritableCellFormat(wfc);
  34.             Label lId2HeadLabel = new Label(0,0,"编号ID",wcfFC);
  35.                         Label strKeyWordHeadLabel = new Label(1,0,"关键词",wcfFC);
  36.                         Label strCategoryConfigHeadLabel = new Label(2,0,"类目",wcfFC);
  37.                         Label dtModifyTimeHeadLabel = new Label(3,0,"更新时间",wcfFC);
  38.                        
  39.                         ws.addCell(lId2HeadLabel);
  40.                         ws.addCell(strKeyWordHeadLabel);
  41.                         ws.setColumnView(2, 50);
  42.                         ws.addCell(strCategoryConfigHeadLabel);
  43.                         ws.addCell(dtModifyTimeHeadLabel);
  44.                        
  45.                 Label lId2Label = null;
  46.                         Label strKeyWordLabel = null;
  47.                         Label strCategoryConfigLabel = null;
  48.                         Label dtModifyTimeLabel = null;
  49.                         for(int i=1;i<=cdosCategoryKeyWords.length;i++)
  50.                         {
  51.                                 String lId2 = cdosCategoryKeyWords[i-1].getStringValue("lId"); //ID
  52.                                 String strKeyWord = cdosCategoryKeyWords[i-1].getStringValue("strKeyWord"); //关键词
  53.                                 String strCategoryConfig = cdosCategoryKeyWords[i-1].getStringValue("strCategoryConfig");//类目
  54.                                 String dtModifyTime = cdosCategoryKeyWords[i-1].getStringValue("dtModifyTime");//更新时间
  55.                                 lId2Label = new Label(0,i,lId2);
  56.                                 strKeyWordLabel = new Label(1,i,strKeyWord);
  57.                                 strCategoryConfigLabel = new Label(2,i,strCategoryConfig);
  58.                                 dtModifyTimeLabel = new Label(3,i,dtModifyTime);
  59.                                 ws.addCell(lId2Label);
  60.                                 ws.addCell(strKeyWordLabel);
  61.                                 ws.addCell(strCategoryConfigLabel);
  62.                                 ws.addCell(dtModifyTimeLabel);
  63.                         }
  64.                 }
  65.                 catch (Exception e)
  66.                 {
  67.                         mallLog.error("输出Excel失败");
  68.                         return false;
  69.                 }
  70.                 finally
  71.                 {
  72.                         try
  73.                         {
  74.                                 wwb.write();
  75.                                 wwb.close();
  76.                                 os.close();
  77.                         }
  78.                         catch (WriteException e)
  79.                         {
  80.                                 mallLog.error("关闭WritableWorkbook出错");
  81.                         }
  82.                         catch (IOException e)
  83.                         {
  84.                                 mallLog.error("关闭WritableWorkbook出错");
  85.                         }
  86.                 }
  87.                 return true;
  88.         }
  89. //java/1339

回复 "JXL导出Excel文件"

这儿你可以回复上面这条便签

captcha