半盏清茶

导航

s-HR导出excel方式二

js代码:

exportToExcelAction:function(){
        var billId = _self.getCurrentModelID();
        billId = encodeURIComponent(billId);
        var url = shr.getContextPath() + "/dynamic.do?handler=com.kingdee.eas.custom.dfxw.pssf.handler.AFOwnBaseAdjustEditHandler&method=exportToExcel&billId=" + billId;
        var sbjndw = $("#sbjndw").text();
        var rq = new Date().format("yyyyMMdd");
        this.download(url, sbjndw+"公积金个人基数调整"+rq+".xls");
    },
    /*文件字节流下载*/
    download: function (url, fileName) {
        var xhr = new XMLHttpRequest();
        xhr.open('post', url, true); // 也可以使用POST方式,根据接口
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.responseType = "blob"; // 返回类型blob
        // 定义请求完成的处理函数,请求前也可以增加加载框/禁用下载按钮逻辑
        xhr.onload = function () {
            // 请求完成
            if (this.status === 200) {
                // 返回200
                var blob = this.response;
                
                var reader = new FileReader();
                if(this.response.type == 'application/json'){
                    reader.readAsText(blob); // 转换为base64,可以直接放入a表情href
                    reader.onload = function (e) {
                        var data = JSON.parse(e.target.result);
                        var result = data.result;
                        var a = JSON.parse(data.data);
                        if(result == "success"){
                            shr.showError({
                                            message: JSON.stringify(a.errmsg)
                                        });
                        }
                    }
                }else{
                    const ie = navigator.userAgent.match(/MSIE\s([\d.]+)/),
                    ie11 = navigator.userAgent.match(/Trident\/7.0/) && navigator.userAgent.match(/rv:11/),
                    ieEDGE = navigator.userAgent.match(/Edge/g),
                    ieVer = (ie ? ie[1] : (ie11 ? 11 : (ieEDGE ? 12 : -1)));
                console.log('ie:' + ie);
                console.log('ieVer:' + ieVer);
                if (ieVer > -1) {
                    window.navigator.msSaveBlob(blob, fileName);
                }else{
                    reader.readAsDataURL(blob); // 转换为base64,可以直接放入a表情href
                    reader.onload = function (e) {
                        // 转换完成,创建一个a标签用于下载
                        var a = document.createElement('a');
                        a.download = fileName;
                        a.href = e.target.result;
                        $("body").append(a); // 修复firefox中无法触发click
                        a.click();
                        $(a).remove();
                    }
                }
                    
                }
                
            }
        };
        // 发送ajax请求
        xhr.send(null);
    },

 

handler代码:

 1 public void exportToExcelAction(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap)
 2       throws SHRWebException{
 3         System.out.println("exportToExcel");
 4         Context ctx = SHRContext.getInstance().getContext();
 5         String id = request.getParameter("billId");
 6         
 7         String[] head = {"序号","员工","公积金缴纳单位","编码","工资","入集团时间","原公积金规则-原基数规则名称","原公积金规则-原基数种类名称","原公积金规则-原基数","现申请公积金规则-拟调基数","申请原因","生效期间"};
 8         
 9         String[] fields = {"seq","person","ncUnit","personnumber","salary","joinDate","oldBaseName","oldBaseType","oldBase","newBase","description","usePeriod"};
10         StringBuffer sql = new StringBuffer();
11 
12         sql.append("/*dialect*/select entry.fseq seq,zy.fname_l2 person,nc.fname_l2 ncUnit,zy.fnumber personnumber,cfsalary salary,to_char(cfjoinDate,'yyyy-MM-dd') joinDate, ");
13         sql.append("brule.fname_l2 oldBaseName,case when cfoldBaseType=1 then '固定' when cfoldBaseType=2 then '分档' when cfoldBaseType=2 then '去年平均工资' else '上月应发工资' end oldBaseType, ");
14         sql.append("cfoldBase oldBase,cfnewBase newBase,entry.fdescription description,to_char(cfusePeriod,'yyyy-MM-dd') usePeriod ");
15         sql.append("from CT_PSS_AFOwnBaseAdjustentry entry ");
16         sql.append("left join CT_BD_NCywdyjczl nc on entry.cfncunitid=nc.fid  ");
17         sql.append("left join CT_PSS_FundBaseRule brule on entry.cfoldbasenameid=brule.fid ");
18         sql.append("left join T_BD_Person zy on entry.fpersonid=zy.fid  ");
19         sql.append("where fbillid ='"+id+"' order by entry.fseq ");
20         
21         //遍历数据,生成一张分录excel
22         HSSFWorkbook wb = new HSSFWorkbook();
23         HSSFCellStyle cellStyle = wb.createCellStyle();
24         DataFormat format = wb.createDataFormat();
25         cellStyle.setDataFormat(format.getFormat("#,##0.00"));//金额字段格式
26         HSSFSheet sheet = wb.createSheet("数据表");
27         Row row = null;
28         Cell cell = null;
29         row = sheet.createRow(0);
30         for(int i=0;i<head.length;i++){
31             cell = row.createCell(i);
32             cell.setCellValue(head[i]);
33         }
34         int rowIndex = 1;
35         try {
36             IRowSet rs = DbUtil.executeQuery(ctx, sql.toString());
37             while (rs.next()) {
38                 Row tempRow = sheet.createRow(rowIndex);
39                 for(int i=0;i<fields.length;i++){
40                     Cell tempCell = tempRow.createCell(i);
41                     if(i==0){
42                         tempCell.setCellValue(rs.getInt(fields[i]));
43                     }else if((i==4 || i==8 && i==9)){
44                         tempCell.setCellStyle(cellStyle);
45                         tempCell.setCellValue(rs.getDouble(fields[i]));
46                     }else{
47                         tempCell.setCellValue(rs.getString(fields[i]));
48                     }
49                 }
50                 rowIndex++;
51 
52             }
53             String fileName = "数据表.xls";
54             response.setContentType("application/vnd.ms-excel");
55             response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName+"\"");
56             OutputStream out = response.getOutputStream();
57             wb.write(out);
58             out.flush();
59             out.close();
60         } catch (BOSException e) {
61             System.out.println(e.getMessage());
62         } catch (SQLException e) {
63             System.out.println(e.getMessage());
64         } catch (IOException e) {
65             System.out.println(e.getMessage());
66         }
67     }

 

posted on 2020-12-05 01:39  半盏清茶  阅读(168)  评论(0编辑  收藏  举报