导出也做了很多遍了,还是发现好记性不如烂笔头,还是记下来。

  1     public void exportLog(HttpServletRequest request,HttpServletResponse response,
  2             @ModelAttribute(CommonUtils.SESSION_USER) UserContext uc,ModelMap model,String logId
  3             )  {
  4 //获得信息列表
  5 List<GTL> lgtgl = logManagerImpl.findLogByKspcAndDwbhAndAreaIdAndLogId(batchId, code, areaId,Id);//id是为了能够在导出以下选项时候用的,导出全部可以穿  //  一个空参数 areaid是为了权限验证用的
  6 //将结果集保存到excel
  7          //取文件存放路径并新建文件
  8          String path = this.getClass().getClassLoader().getResource("").getPath();
  9          PropertieFile cf = new PropertieFile(path + "/spf.properties");
 10          String filePath=cf.getValue("global.export.student");
 11          String tempName=GetRandomName.ConfirmId(32)+".xls";
 12          File file = new File(filePath);//导出文件存放的位置
 13          if(!file.exists()) {
 14              file.mkdirs();
 15          }
 16          WritableWorkbook wwb;
 17         try {
 18             wwb = Workbook.createWorkbook(new File(filePath+tempName));
 19             WritableFont  wf = new WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD , false); //对文字style设置
 20             WritableCellFormat wff = new WritableCellFormat(wf); 
 21             Color color = Color.decode("#87CEFF"); // 自定义的颜色
 22             wwb.setColourRGB(Colour.ORANGE, color.getRed(),color.getGreen(), color.getBlue());
 23             wff.setBackground(Colour.ORANGE);//设置单元格颜色
 24             wff.setAlignment(Alignment.CENTRE); // 设置对齐方式
 25             wff.setVerticalAlignment(VerticalAlignment.CENTRE);
 26             WritableSheet ws = wwb.createSheet("导出信息", 0);// 建立工作簿
 27             ws.setColumnView(0, 20); // 设置列的宽度
 28             ws.setRowView(0, 600); // 设置行的高度
 29             ws.setColumnView(1, 20); // 设置列的宽度
 30             ws.setRowView(1, 600); // 设置行的高度

 41             
 42             Label label1 = new Label(0, 0, "第一列表头",wff);
         Label label1 = new Label(0, 0, "第二列表头",wff);
 49 ws.addCell(label1);// 放入工作簿  56 for(int k=0;k<lgtgl.size();k++){  57 WritableFont wf_1 = new WritableFont(WritableFont.TIMES, 12, WritableFont.NO_BOLD , false); //对文字style设置  58 WritableCellFormat wff_1 = new WritableCellFormat(wf_1);  59 wff_1.setAlignment(Alignment.CENTRE); // 设置对齐方式  60  wff_1.setVerticalAlignment(VerticalAlignment.CENTRE);  61  wff_1.setBorder(Border.ALL, BorderLineStyle.THIN,Colour.BLACK);  62 label1 = new Label(0, k + 1, lgtgl.get(k).getd1l()().toString().trim(),wff_1);// 建立第一列              
            	 String type=lgtgl.get(k).getd2l().toString().trim();
            	 if(("0").equals(type)){
            		 type="采集";
            	 }
            	 if(("1").equals(type)){
            		 type="认证";
            	 }
            	 if(("2").equals(type)){
            		 type="考务";
            	 }
            	 if(("3").equals(type)){
            		 type="押运";
            	 }
            	 label2 = new Label(1, k + 1, type,wff_1);// 建立第二列
102                  ws.addCell(label1);// 放入工作簿
103 109            }
110             // 写入Exel工作表
111             wwb.write();
112             // 关闭Excel工作薄对象
113             wwb.close();
114         } catch (IOException e) {
115             e.printStackTrace();
116         } catch (RowsExceededException e) {
117             e.printStackTrace();
118         } catch (WriteException e) {
119             e.printStackTrace();
120         }
121         //返回文件流
122         response.setCharacterEncoding("UTF-8");
123         try {
124             response.setHeader("Content-Disposition", "attachment;filename=\""
125                     + new String("信息导出.xls".getBytes("gb2312"), "ISO8859-1")
126                     + "\"");
127             OutputStream out = response.getOutputStream();
128             response.setContentType("xls");
129             FileOperation fileOperation=new FileOperation();
130             IOUtils.copy(fileOperation.readFile(filePath+tempName), out);
131             out.flush();
132             out.close();
133         } catch (UnsupportedEncodingException e) {
134             e.printStackTrace();
135         } catch (IOException e) {
136             e.printStackTrace();
137         }
138     }
139 }