JS JQuery 操作: Json转 Excel 下载文件

方法的调用

var json =
'[' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"},' +
'{"申请流水号":"123456","保险公司":"测试数据","发票抬头":"测试数据","发票金额":4,"联系人":"小明","联系人手机号":"1234563333","申请状态":"开票成功"}' +
']';
 var dataJson = JSON.parse(json);
 
 /调用方法
 SONToExcelConvertor("我的excel",dataJson);

json转excel 下载 方法#

  
  function JSONToExcelConvertor(fileName, jsonData) {
        ///json转excel下载
        ///文件名
        ///数据        

        //json
        var arrData = typeof jsonData != 'object' ? JSON.parse(jsonData) : jsonData;

        // #region 拼接数据

        var excel = '';//设置表头var row ="";for(var name in arrData[0]){//每个单元格都可以指定样式. eg color:red   生成出来的就是 红色的字体了.
            row +="';}//列头结束
        excel += row +"";//设置数据for(var i =0; i < arrData.length; i++){var row ="";for(var index in arrData[i]){var value = arrData[i][index]==="."?"": arrData[i][index];

                row +='';//将值放入td}//将td 放入tr,将tr放入table
            excel += row +"";}//table结束
        excel +="
" + name + '
' + value + '
"
; // #endregion // #region 拼接html var excelFile = ""; excelFile += ''; excelFile += '; excelFile += '; charset=UTF-8">'; excelFile += ""; excelFile += ""; excelFile += ""; excelFile += ""; excelFile += excel;//将table 拼接 excelFile += ""; excelFile += ""; // #endregion var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile); //创建a标签 var link = document.createElement("a"); //指定url link.href = uri; //设置为隐藏 link.style = "visibility:hidden"; //指定文件名和文件后缀格式 link.download = fileName + ".xls"; //追加a标签 document.body.appendChild(link); //触发点击事件 link.click(); //移除a标签 document.body.removeChild(link); }

JS JQuery 操作: Json转 Excel 下载文件_第1张图片

你可能感兴趣的:(JavaScript,JQuery,JS,操作:,Json转Excel,下载文件)