表单代码块JS---明细表数据导出

需求场景

业务部门需要将单个流程明细表单的数据复制出来!!!

最开始打算用报表展示所有数据,由用户筛选后导出到excel,结果用户闲麻烦...

沟通无效只能JS大法了 = =

整体思路

  • 1.表单单元格设置id值
  • 2.通过js根据id值插入按钮
  • 3.实现导出方法
  • 4.给按钮绑定导出方法

注意:代码仅供参考

$(document).ready(function(){
    $('#btn').append('<button id = "export" >导出</button>');
    $('#export').click(function(){
                const arr = [];
                const rowArr = WfForm.getDetailAllRowIndexStr("detail_1").split(",");
                for(let i=0; i<rowArr.length; i++){
                    const json = {};
                    let rowIndex = rowArr[i];
                    if(rowIndex !== ""){
                        json.field1 = WfForm.getFieldValue("field1_"+rowIndex);
                        json.field2 = WfForm.getFieldValue("field2_"+rowIndex);
                        json.field3 = WfForm.getFieldValue("field3_"+rowIndex);
                        json.field4 = WfForm.getFieldValue("field4_"+rowIndex);
                        json.field5 = WfForm.getFieldValue("field5_"+rowIndex);
                        arr.push(json);
                    }
                }
                //自定义标题栏
                const title=['明细字段1','明细字段2','明细字段3','明细字段4','明细字段5'];
                //过滤器
                const filter=[]               
                jsonToExcel(arr,"流程明细数据",title,filter);
    });
    
  });

  const jsonToExcel = (data, fileName ,title,filter) => {  
    if(!data)  return;
    const arrData = typeof data != 'object' ? JSON.parse(data) : data;  
    let excel = "<table>";      
    let row = "<tr>";  
    if(title)
    {
        //使用标题项
        for (let index in title) {  
            if(index < title.length){
              row += "<th align='center'>" + title[index] + '</th>';
            }
        }
    }
    else{
        //不使用标题项
        for (let i in arrData[0]) {  
            row += "<th align='center'>" + i + '</th>';
        } 
    }
        excel += row + "</tr>";  
    //设置数据  
    for (let i = 0; i < arrData.length; i++) {
        let row = "<tr>";  
        for (let index in arrData[i]) {
            //判断是否有过滤行
            if(filter)
            {
                if(filter.indexOf(index)==-1)
                {
                    const value = arrData[i][index] == null ? "" : arrData[i][index];  
                    row += '<td>' + value + '</td>'; 
                } 
            }
            else
            {
                const value = arrData[i][index] == null ? "" : arrData[i][index];  
                row += "<td align='center'>" + value + "</td>"; 
            }    
        }  
        excel += row + "</tr>";  
            }  
            excel += "</table>";  
            let excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";  
                excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';  
                excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';  
                excelFile += '; charset=UTF-8">';  
                excelFile += "<head>";  
                excelFile += "<!--[if gte mso 9]>";  
                excelFile += "<xml>";  
                excelFile += "<x:ExcelWorkbook>";  
                excelFile += "<x:ExcelWorksheets>";  
                excelFile += "<x:ExcelWorksheet>";  
                excelFile += "<x:Name>";  
                excelFile += "{worksheet}";  
                excelFile += "</x:Name>";  
                excelFile += "<x:WorksheetOptions>";  
                excelFile += "<x:DisplayGridlines/>";  
                excelFile += "</x:WorksheetOptions>";  
                excelFile += "</x:ExcelWorksheet>";  
                excelFile += "</x:ExcelWorksheets>";  
                excelFile += "</x:ExcelWorkbook>";  
                excelFile += "</xml>";  
                excelFile += "<![endif]-->";  
                excelFile += "</head>";  
                excelFile += "<body>";  
                excelFile += excel;  
                excelFile += "</body>";  
                excelFile += "</html>";  
                const uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);  
    const link = document.createElement("a");      
    link.href = uri;  
    link.style = "visibility:hidden";  
    link.download = fileName + ".xls";  
    document.body.appendChild(link);  
    link.click();  
    document.body.removeChild(link);  
}  


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值