1 //导出excel
2 public String exportExcel(){
3 WritableWorkbook workbook = null;
4 HttpServletResponse response =ServletActionContext.getResponse();
5 response.setHeader("Content-Disposition", "attachment; filename="+
6 "excel0"+".xls");
7 try {
8 String hql = "from DistrictTest where isDele=0";
9 //workbook = Workbook.createWorkbook(new File("E:/yang.xls"));
10 districtTestList = (List<DistrictTest>) utilService.getHqlAll(hql);
11 outputStream = response.getOutputStream();
12 workbook = Workbook.createWorkbook(outputStream);
13 WritableSheet sheet = workbook.createSheet("sheet1", 0);
14 Label label = new Label(0, 0, "序号");
15 Label label1 = new Label(1, 0, "区县名称");
16 Label label2 = new Label(2, 0, "编码");
17 sheet.addCell(label);
18 sheet.addCell(label1);
19 sheet.addCell(label2);
20 for(int i=0;i<districtTestList.size();i++){
21 int j = i;
22 Label label3 = new Label(0, i+1, " "+(++j));
23 sheet.addCell(label3);
24 if(districtTestList.get(i).getDistrictName()!=null&&!" ".equals(districtTestList.get(i).getDistrictName())){
25
26 Label lable4 = new Label(1, i+1, districtTestList.get(i).getDistrictName().toString());
27 sheet.addCell(lable4);
28 }else{
29
30 Label lable4 = new Label(1, i+1, " ");
31 sheet.addCell(lable4);
32 }
33 if(districtTestList.get(i).getCode()!=null&&!" ".equals(districtTestList.get(i).getCode())){
34 Label lable5 = new Label(2, i+1, districtTestList.get(i).getCode().toString());
35 sheet.addCell(lable5);
36 }else{
37 Label lable5 = new Label(2, i+1, " ");
38 sheet.addCell(lable5);
39 }
40 }
41 workbook.write();
42 outputStream.flush();
43
44 System.out.println("下载excel成功");
45 flag="1";
46 return districtList();
47 } catch (Exception e) {
48 // TODO: handle exception
49 Loggers.error("----DistrictTestAction的exportExcel方法错误"+e.getMessage());
50 e.printStackTrace();
51 }finally{
52 if(workbook!=null){
53 try {
54 workbook.close();
55 } catch (Exception e) {
56 // TODO Auto-generated catch block
57 e.printStackTrace();
58 }
59 }
60 if(outputStream!=null){
61 try {
62 outputStream.close();
63 } catch (IOException e) {
64 // TODO Auto-generated catch block
65 e.printStackTrace();
66 }
67 }
68
69 }
70
71
72
73
74 return ERROR;
75 }