JS Json生成Excel

306 阅读2分钟
var checkedData =  {
    "id":4058,
    "add_by":6273,
    "add_on":"2019-06-28 15:31:49",
    "deparment_id":5276,
    "crops_id":1,
    "breed_name":"品种名称",
    "breed_type":"品种类型",
    "breed_source":"品种来源",
    "group_id":4055,
    "apply_deparment":"申请单位(个人)",
    "apply_nationality":"中国",
    "apply_company_address":"地址",
    "apply_zip_code":"邮政编码",
    "apply_contact":"联系人",
    "apply_phone":"固定电话",
    "apply_mobile":"移动电话",
    "apply_Fax":"传真",
    "apply_email":"111@qq.com",
    "breed_deparment_id":"育种单位(个人)",
    "breed__nationality":"育种中国",
    "breed_company_address":"育种地址",
    "breed_Fax":"育种传真",
    "breed_zip_code":"育种邮政编码",
    "breed_contact":"育种联系人",
    "breed_phone":"育种固定电话",
    "breed_mobile":"育种移动电话",
    "breed_email":"YZ111@qq.com",
    "auth_status":0,
    "auth_name":"新品种保护名称",
    "auth_code":"品种权号",
    "parents_breed_auth":"亲本组和",
    "notice_name":"",
    "notice_code":"",
    "parents_breed_notice":"",
    "transgene_flag":0,
    "gene_name":"转基因生物名称",
    "gene_secruity_code":"转基因安全证书编号",
    "parents_breed_gene":"转基因亲本组合",
    "authorized_province":"审定省份",
    "authorized_number":"审定编号",
    "notice_breed_name":"公告品种名称",
    "breed_department_opinion":"育种单位意见",
    "parents_and_breeding_info":"",
    "characteristic":" 幼苗叶鞘",
    "breed_picture":"",
    "proposed_test_area":"",
    "techniques":"",
    "risk":"",
    "report":"",
    "gene_file":null,
    "gene_picture":"63128074_52.png",
    "gene_picture_2":"63128080_94.png",
    "audit_flag":3,
    "audit_date":"2019-06-28 15:31:49",
    "audit_flag2":3,
    "audit_date2":null,
    "encrypted_id":"A4A8FB406B42B02E",
    "word_flag":1,
    "return_opinion":null,
    "gene_text":"",
    "gene_text_2":"",
    "crops_name":"玉米",
    "text1":"2017 2017春玉米 玉米"
}

let formatArray = checkedData.map((item) => {
    item.auth_status = item.auth_status == 0 ? '已获授权' : '未授权';
    item.transgene_flag = item.transgene_flag == 0 ? '是' : '否';

    if (item.audit_flag == 0) {
        item.audit_flag = '待审核';
    } else if (item.audit_flag == 1) {
        item.audit_flag = '通过';
    } else if (item.audit_flag == 2) {
        item.audit_flag = '已退回';
    } else if (item.audit_flag == 3) {
        item.audit_flag = '未提交';
    }

    return item;
})
       

let title = ['品种名称', '品种类型', '品种来源', '申请单位(个人)', '申请单位国籍', '申请单位地址', '申请单位邮政编码',
'申请单位联系人', '申请单位固定电话', '申请单位移动电话', '申请单位传真', '申请单位邮箱', '育种单位(个人)',
'育种单位国籍', '育种单位地址', '育种单位传真', '育种单位邮政编码', '育种单位联系人', '育种单位固定电话', '育种单位移动电话',
'育种单位邮箱', '品种保护情况', '新品种保护名称', '品种权号', '亲本组和', '是否转基因', '转基因生物名称', '转基因安全证书编号',
'转基因亲本组合', '审定省份', '审定编号', '公告品种名称', '育种单位意见', '审核状态', '作物', '申请组别'];

let filter = 'id,add_by,add_on,deparment_id,crops_id,group_id,notice_name,notice_code,'+
'parents_breed_notice,parents_and_breeding_info,characteristic,breed_picture,proposed_test_area,' + 
'techniques,risk,report,gene_file,gene_picture,gene_picture_2,audit_date,audit_flag2,'+
'audit_date2,encrypted_id,word_flag,return_opinion,gene_text,gene_text_2,';  //不导出的字段
        
var timestamp = (new Date()).valueOf();
exportExcel(formatArray, '品种信息汇总_' + timestamp, title, filter);

function exportExcel(JSONData, FileName, title, filter) {

    if (!JSONData) return;
    //转化json为object
    var arrData = typeof JSONData != "object" ? JSON.parse(JSONData) : JSONData;

    var excel = "<table>";
    //设置表头
    var row = "<tr>";
    if (title) { //使用标题项
       
        for (var i = 0; i < title.length; i++) {
            console.log(i);
            row += "<th align='center'>" + title[i] + "</th>";
        }
    } else {//不使用标题项
        for (var i in arrData[0]) {
            row += "<th align='center'>" + i + "</th>";
        }
    }
    excel += row + "</tr>";
    //设置数据
    for (var i = 0; i < arrData.length; i++) {
        var row = "<tr>";
        for (var index in arrData[i]) {
            //判断是否有过滤行
            if (filter) {
                if (filter.indexOf(index+',') == -1) {
                    var value = arrData[i][index] == null ? "" : arrData[i][index];
                    row += "<td>" + value + "</td>";
                }
            } else {
                var value = arrData[i][index] == null ? "" : arrData[i][index];
                row += "<td align='center'>" + value + "</td>";
            }
        }
        excel += row + "</tr>";
    }
    excel += "</table>";
    var 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>";
    var uri =
        "data:application/vnd.ms-excel;charset=utf-8," +
        encodeURIComponent(excelFile);
    var 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);
}