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

<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. 直播带货源码,flutter 顶部滚动栏+页面

    直播带货源码,flutter 顶部滚动栏+页面 tabPage.dart import 'package:flutter/cupertino.dart';import 'package:flutter ...

  2. Linux下安装MongoDB的Database Tools并配置

    安装tools以使用导入导出功能.解决mongodump: command not found #下载 wget https://fastdl.mongodb.org/tools/db/mongodb ...

  3. selenium最常用的基本方法

    1.打开,关闭浏览器 打开chrome浏览器:webdriver.Chorme() 打开Firefox浏览器:webdriver.Firefox() 关闭当前浏览器窗口:driver.close() ...

  4. maven静态资源过滤

    <resources> <resource> <directory>src/main/java</directory> <includes> ...

  5. 全链路压测SOP

    压测模型构建:人工 线上(大促)流量数据 (数据脱敏) 日常流量数据 业务方新的特性产生的变更数据 友商做过的事情 压测模型构建:自动 流程包括:录制-清洗-回放 (目前能做好的公司非常少) 压测标准 ...

  6. jupyter notebook相关笔记

    导出pdf时隐藏代码 参考:https://segmentfault.com/q/1010000043309446

  7. ubuntu添加新硬盘进行分区,并挂载到/home

    ubuntu添加新硬盘,进行分区,并挂载到/home目录. 1.查看已有的磁盘,可以看到sdb还没有分区. > sudo fdisk -l 可以看到各个硬盘的设备名,一般以sda.sdb.sdc ...

  8. SpringBoot配置阿里云https提示端口占用问题

    1.因为要配置https,所以去网上找了一些资料,然后按照步骤,依次完成了以下步骤 ①在application.yml中加入配置 http: port: 12000 #原本的端口号server: po ...

  9. VUE基础 · 绑定(1)

    前端三大框架:Angular.js.React.js.Vue.js,目前最热的是Vue,并且使用的热度还在递增中. Vue已经将操作页面的方法封装好,我们只需要对数据进行修改就可以完成页面的显示.Vu ...

  10. CompletableFuture的使用

    1 List<List<String>> subList = CommonUtils.splitList(pendingIds, 500);private static fin ...