纯js导出Excel文件(无需引入插件)

纯js导出Excel文件(无需引入插件)

提示:代码没有来得及整理,大致代码都在这里

let thlist = [
    [
        {text: '序号', rowspan: '2'},
        {text: '姓名', rowspan: '2'},
        {text: '手机号', rowspan: '2'},
        {text: '考勤课时', rowspan: '2'},
        {text: '学习课时', rowspan: '2'},
        {text: '直播课时', rowspan: '2'},
        {text: '录播课时', rowspan: '2'}
    ]
]
let tdlist = []
let objInfo = {
    fileName: '课时记录表',
    theadList: thlist,
    tbodyList: tdlist
}
toExcel(objInfo)

function toExcel(objInfo) {
    let obj = {
        fileName: objInfo.fileName || '文件名',
        FootName: objInfo.FootName || 'sheet1',
        theadList: objInfo.theadList || [],
        tbodyList: objInfo.tbodyList || []
    }
let excel = '<table style="font-size:15px;font-family:Microsoft YaHei;vnd.ms-excel.numberformat:@">';
    for (let i = 0; i < obj.theadList.length; i++) {
        excel += "<tr align='center'>"
        let th = obj.theadList[i]
        for (let h = 0; h < th.length; h++) {
            excel += "<th colspan=" + th[h].colspan + " rowspan=" + th[h].rowspan + " style=" + th[h].style + " align=" + th[h].align + ">" + th[h].text + "</th>"
        }
        excel += "</tr>";
    }
    for (let i = 0; i < obj.tbodyList.length; i++) {
        excel += "<tr align='center'>"
        let td = obj.tbodyList[i]
        for (let d = 0; d < td.length; d++) {
            excel += "<td colspan=" + td[d].colspan + " rowspan=" + td[d].rowspan + " style=" + td[d].style + " align=" + td[d].align + ">" + td[d].text + "</td>"
        }
        excel += "</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 += obj.FootName||"sheet1";
    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 url = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);
    var link = document.createElement("a");
    link.href = url;
    link.style = "visibility:hidden";
    link.download = obj.fileName + ".xlsx";
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

后来我给封装成了插件p-export-excel,可以在npm中安装npm i p-export-excel

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

初辰ge

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值