欢迎与我联系   

Java 导出Excel

前台代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<button class="btn btn-sm btn-success" type="submit" id="detailEp" onclick="return exportCheck(true);" forbid="yes">
    <i class="icon-arrow-right bigger-110"></i> 导出
</button>
<br><script>
$(function() {
    var timer = "";
});
 
//点击导出按钮时禁用导出按钮
$("#myForm").submit(function() {
    $("button[type=submit]",this).attr("disabled","disabled");
    //提交导出后定时去查看有没有导出成功
    timer = setInterval(refrashPg,1000);
})
 
//导出成功后会放开导出按钮的禁用
function refrashPg() {
    if (getCk() =="1") {
        clearInterval(timer);
        $("#detailEp").removeAttr("disabled");
    }
    delCk();
}
//js获取到cookie
function getCk() {
    debugger
    var ck = document.cookie.split(";");
    var ckname = "";
    for (var i = 0;i<ck.length;i++) {
        var arr = ck[i].split("=");
        if (arr[0] =="updtstatus") {
            ckname = arr[1];
            break;
        }
    }
    return ckname;
}
//js删除掉cookie
function delCk() {
    var exp = new Date();
    var name = "updtstatus";
    exp.setTime(exp.getTime()-1000);
    var cval = getCk();
    document.cookie = name  + "=" + cval + "; expires=" + exp.toGMTString();
}
}
1
</script>

  后台导出代码就是普通的Java POI代码:

复制代码
 1 public void buildExcelDocument(Map<String, Object> obj,String fileName,String type, 
 2              HttpServletRequest request, HttpServletResponse response)  
 3             throws Exception {  
 4         HSSFWorkbook workbook = new HSSFWorkbook();  
 5         if ("1".equals(type)) {
 6             List<Map<String, Object>> contentList =  (List<Map<String, Object>>) obj.get("content");  
 7             HSSFSheet sheet = workbook.createSheet("sheet1");//创建Excel的版本是2003-2007(xls) 如果需要2010的话,用 XSSFSheet   
 8             Map<String,String>  titleList = (Map<String, String>) obj.get("title");
 9             //表头的key 对应内容listMap中的map的key
10             List<String> mkey = new ArrayList<String>();
11             //表头的value 表头
12             List<String> mvalue = new ArrayList<String>();
13             Iterator<Entry<String, String>> it = titleList.entrySet().iterator();
14             while(it.hasNext()){
15                 @SuppressWarnings("rawtypes")
16                 java.util.Map.Entry entry = (java.util.Map.Entry)it.next();
17                 mkey.add((String) entry.getKey());
18                 mvalue.add((String) entry.getValue());
19             }
20             sheet.setDefaultColumnWidth((short) 12);    
21             HSSFCell cell = null;  
22             for (int i = 0;i<mvalue.size();i++) {
23                 cell = getCell(sheet, 0, i);
24                 setText(cell, mvalue.get(i));
25             }
26             
27             for (short i = 0; i < contentList.size(); i++) {    
28                 HSSFRow sheetRow = sheet.createRow(i+1);   
29                 Map<String, Object> entity = contentList.get(i);  
30                 for (int j = 0;j< mkey.size();j++) {
31                         if (entity.get(mkey.get(j)) instanceof String) {
32                             sheetRow.createCell(j).setCellValue((String)entity.get(mkey.get(j)));
33                         } else if (entity.get(mkey.get(j)) instanceof Double) {
34                             sheetRow.createCell(j).setCellValue((Double)entity.get(mkey.get(j)));
35                         } else if (entity.get(mkey.get(j)) instanceof Date){
36                             String date = (entity.get(mkey.get(j))).toString();
37                             sheetRow.createCell(j).setCellValue(date.substring(0, 10));
38                         } else if (entity.get(mkey.get(j)) instanceof BigDecimal){
39                             sheetRow.createCell(j).setCellValue((entity.get(mkey.get(j))).toString());
40                         }
41                     }
42                 }
43             }
44         } else if ("2".equals(type)) {
45             List<String[]> contentList =  (List<String[]>) obj.get("content");  
46             HSSFSheet sheet = workbook.createSheet("sheet1");    
47             String[]  titleList = (String[]) obj.get("title");
48             HSSFCell cell = null;  
49             for (int i = 0;i<titleList.length;i++) {
50                 cell = getCell(sheet, 0, i);
51                 setText(cell, titleList[i]);
52             }
53             for (short i = 0; i < contentList.size(); i++) {    
54                 HSSFRow sheetRow = sheet.createRow(i+1); 
55                 String[] detail = contentList.get(i);
56                 for (int j = 0;j< detail.length;j++) {
57                     sheetRow.createCell(j).setCellValue((String)contentList.get(i)[j]);
58                 }
59             }
60         }
61         
62        //设置下载时客户端Excel的名称     
63         String filename = fileName + ".xls";  
64       //处理中文文件名  
65         filename = Chineseutil.encodeFilename(filename, request);  
66         response.setContentType("application/Vnd.ms-excel;charset=UTF-8");     
67         response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gb2312"), "iso8859-1"));     
68         OutputStream ouputStream = response.getOutputStream();
69         workbook.write(ouputStream);     
70         
71         //导出数据后将信息存入cookie
72      Cookie status = new Cookie("updtstatus", "1");
73       status.setMaxAge(3600);//设置cookie存活时间1个小时
74       response.addCookie(status);
75         
76         ouputStream.flush();     
77         ouputStream.close();  
78     }
复制代码
posted @ 2017-03-07 10:45  小珍珠在河里敲代码  阅读(143)  评论(0编辑  收藏  举报