首页 > 其他分享 >js 导出excle文件(多页)

js 导出excle文件(多页)

时间:2022-10-27 09:44:41浏览次数:36  
标签:function 导出 ctx js workbookXML link var 多页 excle

  --前提:页面生成相应的表格数据    例如:     

<table id="a">
  <tr>
    <th></th>
  </tr>
  <tr>
    <td></td>
  </tr>
</table>
<table id="b">
  <tr>
    <th></th>
  </tr>
  <tr>
    <td></td>
  </tr>
</table>

(表格列与列、行与行之间可合并)     var tables = ['a', 'b'], wsnames = ['a页', 'b页'], wbname = 'test.xls', appname = 'Excel'; 注:(多页)   tables为每个表的id选择器
  wsnames为每页名称   wbname为总表名称 /* 以下为导出处理函数 */ function toExcle(tables){     var uri = 'data:application/vnd.ms-excel;base64,'         , tmplWorkbookXML = '<?xml version="1.0"?><?mso-application progid="Excel.Sheet"?><Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">'             + '<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office"><Author>Axel Richter</Author><Created>{created}</Created></DocumentProperties>'             + '<Styles>'             + '<Style ss:ID="Currency"><NumberFormat ss:Format="Currency"></NumberFormat></Style>'             + '<Style ss:ID="Date"><NumberFormat ss:Format="Medium Date"></NumberFormat></Style>'             + '</Styles>'             + '{worksheets}</Workbook>'         , tmplWorksheetXML = '<Worksheet ss:Name="{nameWS}"><Table>{rows}</Table></Worksheet>'         , tmplCellXML = '<Cell{attributeStyleID}{attributeFormula}><Data ss:Type="{nameType}">{data}</Data></Cell>'         , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }         , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }
    var ctx = "";     var workbookXML = "";     var worksheetsXML = "";     var rowsXML = "";
    for (var i = 0; i < tables.length; i++) {         if (!tables[i].nodeType) tables[i] = document.getElementById(tables[i]);
        //           控制要导出的行数         for (var j = 0; j < tables[i].rows.length; j++) {             rowsXML += '<Row>';
            for (var k = 0; k < tables[i].rows[j].cells.length; k++) {                 var dataType = tables[i].rows[j].cells[k].getAttribute("data-type");                 var dataStyle = tables[i].rows[j].cells[k].getAttribute("data-style");                 var dataValue = tables[i].rows[j].cells[k].getAttribute("data-value");                 dataValue = (dataValue) ? dataValue : tables[i].rows[j].cells[k].innerHTML;                 var dataFormula = tables[i].rows[j].cells[k].getAttribute("data-formula");                 dataFormula = (dataFormula) ? dataFormula : (appname == 'Calc' && dataType == 'DateTime') ? dataValue : null;                 ctx = {                     attributeStyleID: (dataStyle == 'Currency' || dataStyle == 'Date') ? ' ss:StyleID="' + dataStyle + '"' : ''                     , nameType: (dataType == 'Number' || dataType == 'DateTime' || dataType == 'Boolean' || dataType == 'Error') ? dataType : 'String'                     , data: (dataFormula) ? '' : dataValue                     , attributeFormula: (dataFormula) ? ' ss:Formula="' + dataFormula + '"' : ''                 };                 rowsXML += format(tmplCellXML, ctx);             }             rowsXML += '</Row>'         }         ctx = { rows: rowsXML, nameWS: wsnames[i] || 'Sheet' + i };         worksheetsXML += format(tmplWorksheetXML, ctx);         rowsXML = "";     }
    ctx = { created: (new Date()).getTime(), worksheets: worksheetsXML };     workbookXML = format(tmplWorkbookXML, ctx);
    //       查看后台的打印输出     //console.log(workbookXML);
    var link = document.createElement("A");     link.href = uri + base64(workbookXML);     link.download = wbname || 'Workbook.xls';     link.target = '_blank';     document.body.appendChild(link);     link.click();     document.body.removeChild(link); }   导出结果:

 

 

 

标签:function,导出,ctx,js,workbookXML,link,var,多页,excle
From: https://www.cnblogs.com/yuqz/p/16831039.html

相关文章

  • 删除 设置 获取 cookies 的第三方包 js-cookies
    //第三方包js-cookies是一个操作cookies的包importCookiesfrom'js-cookie'//Cookies.set(key,value)存值//Cookies.get(key)取值//Cookies.remove(key)......
  • ReactJS单页面应用之项目搭建
    初衷因接手的项目前端采用reactjs+antd,为把控项目中的各个细节,所以想做一些整理,以免后期遗忘。创建及启动项目#全局安装create-react-app#如果曾经安装过,可先移除:n......
  • js 预解析
    预解析是js中一个很重要的知识,为了防止忘记和便于后续查找,本博客记录了预解析的重要概念和一些例子。一、什么是预解析。js引擎运行js会分成两个步骤,一......
  • b站 bilibili 哔哩哔哩 js小脚本 油猴 查找搜索某年的视频 自动翻页
    varccount=0;varnumnum=5constsleep=(delay)=>newPromise((resolve)=>setTimeout(resolve,delay))while(numnum--){  document.querySelect......
  • js 的页面切换
    <!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content......
  • TypeError: Object of type 'ndarray' is not JSON serializabl;TypeError: Object of
    1.python使用​​json.jsonify​​​将结果转为json格式时,爆出如上​​TypeError:Objectoftype'ndarray'isnotJSONserializable​​​错误。(flask)代码如下:@app......
  • hyperf/go/springboot通过jsonrpc通信
    一、背景随着用户的增长和业务的增多,单节点服务已经满足不了需求,用hyperf对主业务进行了重构。hyperf是一个后现代的php框架,基于php+swoole,支持协程,解决了php让人诟病的......
  • .Net内置JSON序列化中文问题
    今天在用System.Text.Json序列化的时候遇到了中文序列化的一个问题,示例如下:JsonSerializer.Serialize(new{Name="你好"});预期结果是:{"Name":"你好"},但得到结果如下......
  • js原型和继承
    原型prototype概述:prototype是属于函数的一个空间,它是一个对象.因为构造函数也是函数所以他也具备.而这个prototype属性我们称为显式原型函数的prototypefunctionf......
  • JS之dom元素和位置有关的属性计算方式
    以下全部属性皆为横向(因为竖向的话只需要把x改为y即可,就不在本文列出) 1.clientWidth:元素内部宽度=width+padding2.offsetWidth:元素内部宽度=width+padding+border+scr......