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

<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);
}
 
导出结果:

 

js 导出excle文件(多页)的更多相关文章

  1. EasyUI 如何结合JS导出Excel文件

    出处:http://blog.csdn.net/jumtre/article/details/41119991 EasyUI 如何结合JS导出Excel文件 分类: 技术 Javascript jQu ...

  2. FileSaver.js导出json文件和文本

    最近刚刚写了个json数据导出生成Excel文件的,顺便总结下利用FileSaver.js导出其他文件的,这里要注意的一个点就是,当导出的是json文件或是txt文件时,导出的内容要是字符串,特别当时 ...

  3. Web 端 js 导出csv文件(使用a标签)

    前言 导出文件,使用最多的方式还是服务器端来处理.比如jsp 中使用response 的方式. 但是,有时候可能就想使用web 前端是否也可以把页面上的内容导出来呢? 比如说,导出页面的一个表格. 这 ...

  4. 【转载】JS导出CSV文件

    转自:http://www.cnblogs.com/dengnan/p/3990211.html 通过自己实际测试有以下几种方法 方法一通过a标签实现,把要导出的数据用“\n”和“,”拼接成一个字符串 ...

  5. js导出excel文件

    <div id="tablesDiv"> <table id="tabDiv1"> <tbody><tr> &l ...

  6. Web 端 js 导出csv文件

    http://www.qdfuns.com/notes/35821/2ab249182734d1f5c66da6b5cf395db9.html

  7. JAVA 导出 Excel, JS 导出 Excel

    本介绍两种Excle导出方法: JAVA 导出 Excle, JS 导出 Excle 1, js 根据 html 页面的 table > tr > td 标签导出 js代码: //导出 v ...

  8. SpringMVC 实现POI读取Excle文件中数据导入数据库(上传)、导出数据库中数据到Excle文件中(下载)

    读取Excale表返回一个集合: package com.shiliu.game.utils; import java.io.File; import java.io.FileInputStream; ...

  9. FileSaver.js 实现浏览器文件导出

    FileSaver.js 实现浏览器文件导出 在浏览器中用 FileSaver.js 可以下载文件,不会造成文件直接打开等情况

  10. excle 文件的导入和导出

    //excle 文件导出 public function excel(){ try{ include(BASE_PATH."Excel/PHPExcel.php"); // ech ...

随机推荐

  1. EBS的配置文件

    默认账套 select fnd_profile.value('GL_SET_OF_BKS_ID') FROM DUAL; >> 2026 这样就能通过2026去获取一些账套上的配置 比如c ...

  2. WCF的实现(方式二)

    参考他人实现文件传输 [WCF]利用WCF实现上传下载文件服务 服务端: 1.首先新建一个名为FileService的WCF服务库项目,如下图: 2.将Service,IService重命名为File ...

  3. c#笔记(四)——switch

    ---恢复内容开始--- using UnityEngine; using System.Collections;   public class Script1 : MonoBehaviour {   ...

  4. (转载)史上最详细的docker学习手册

    原文链接:https://my.oschina.net/u/1388595/blog/5078146 一.docker入门 1.docker的安装及入门示例 环境准备:docker需要安装在cento ...

  5. hyperledger explorer 环境搭建

    ### nodejs 下载nodejs二进制包: wget https://nodejs.org/dist/v12.16.2/node-v12.16.2-linux-x64.tar.xz 解压xz数据 ...

  6. vm虚拟机和主机之间互传文件

    u盘大家都有吧,用u盘吧,超方便! vmtools 从主机传文件到虚拟机 可以通过之间复制粘贴/拖拽 或者共享文件夹的方式 从虚拟机传文件到主机,查到了说说是要在虚拟机里面改一个什么映射设置,改完之后 ...

  7. 更换CentOS的下载源为阿里云

    阿里Linux镜像地址:http://mirrors.aliyun.com/ 1.备份mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cen ...

  8. 点击dgv某列的单元格时触发事件的方法

    private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { if (e.Colu ...

  9. 38.Ribbon

    Ribbon默认是懒加载,所以初次请求时间最长,后续请求会变快,可以通过修改为饥饿加载 ribbon.eager-load.enabled=true ribbon.eager-load.clients ...

  10. JQuery电梯导航

    // .zjong .dag_id 内容区// .zuoyou .dao_hang a 电梯按钮 $(function() { $(".zjong .dag_id").each(( ...