需求场景
业务部门需要将单个流程明细表单的数据复制出来!!!
最开始打算用报表展示所有数据,由用户筛选后导出到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);
}