惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园_首页
量子位
阮一峰的网络日志
阮一峰的网络日志
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG
D
DataBreaches.Net
B
Blog
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
博客园 - 三生石上(FineUI控件)
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
aimingoo的专栏
aimingoo的专栏
B
Blog RSS Feed
H
Help Net Security
The Cloudflare Blog
U
Unit 42

Java不加糖的Blog

假设, AI 把我代替的那一刻真的到来 | szhshp 的第三边境研究所 小傻瓜都能懂的 AstrBot QQ 机器人集成 MCP 功能实战指南 | szhshp 的第三边境研究所 《智人之上: 从石器时代到 AI 时代的信息网络简史》阅读笔记 | szhshp 的第三边境研究所 iPadOS 26 无法设置空间场景图片壁纸的解决方法 | szhshp 的第三边境研究所 《一路云海》(终) | szhshp 的第三边境研究所 《一路云海》(四): 如何不按套路旅行 | szhshp 的第三边境研究所 《一路云海》(三): In Ya Mellow Tone | szhshp 的第三边境研究所 《一路云海》(二): 关西世博参观纪实 | szhshp 的第三边境研究所 《一路云海》(一): 新的征程 | szhshp 的第三边境研究所 2025 大阪世博会 [ 3 天前-先到先得 ] 阶段 场馆预约必中独家攻略 | szhshp 的第三边境研究所 Hackathon 随想 | szhshp 的第三边境研究所 一杯双皮奶 | szhshp 的第三边境研究所 Armbian + CasaOS + NAS 配置指南 | szhshp 的第三边境研究所 Docker 构建镜像报错: error getting credentials - err: exit status 1, out: `` | szhshp 的第三边境研究所 Disqus RIP! 论过高的维护成本如何治疗固执的坏习惯 | szhshp 的第三边境研究所 炸弹猫桌游变体规则 | szhshp 的第三边境研究所 《小岛经济学》阅读笔记 | szhshp 的第三边境研究所 《金钱心理学》阅读笔记 | szhshp 的第三边境研究所 为知笔记 RIP: 迁移剩余的笔记 | szhshp 的第三边境研究所 2025 博客第十年展望 - 再见我的过去 | szhshp 的第三边境研究所 我在独立游戏里面致敬的作品 | szhshp 的第三边境研究所 《How to make thing faster》阅读笔记 | szhshp 的第三边境研究所 《The Art of Clean Code》阅读笔记 | szhshp 的第三边境研究所 《Clean Architecture: A Craftsman Guide to Software Structure and Design》阅读笔记 | szhshp 的第三边境研究所 《How AI Works》阅读笔记 | szhshp 的第三边境研究所 游戏策划废案 - Project Uranus | szhshp 的第三边境研究所 游戏策划废案 - Project X | szhshp 的第三边境研究所 人生第一款独立游戏开发复盘 | szhshp 的第三边境研究所 Trap of Life | szhshp 的第三边境研究所 wireguard折腾记录
DataTable-输出Excel添加额外行的实现 | szhshp 的第三边境研...
2017-06-15 · via Java不加糖的Blog

Meta

目录

Datatable Excel输出

这个方法对主流浏览器适用,特别是IE Edge

有个需求需要在Datatable输出的Excel顶端添加几行数据, 看了下Datatable官方的实现, 作者似乎也没啥好主意, 不过一些用户提供了方法。

基于Button.Customize参数实现:

jQuery(document).ready(function($) {
  $('table#datatable').dataTable({
    buttons: [{
      extend: 'excelHtml5',
      render: function ( data, type, full, meta ) {
            return '<a href="'+data+'">Download</a>'; //change the button text here
      },
      customize: function(xlsx) {
        var sheet = xlsx.xl.worksheets['sheet1.xml'];
        var numrows = 4;

        // add styles for the column header, these row will be moved down
        var clRow = $('row', sheet);
        $(clRow[0]).find('c').attr('s', 32);
          
        //update Row
        clRow.each(function () {
          var attr = $(this).attr('r');
          var ind = parseInt(attr);
          ind = ind + numrows;
          $(this).attr("r", ind);
        });
          
        // Create row before data
        $('row c ', sheet).each(function(index) {
            var attr = $(this).attr('r');

            var pre = attr.substring(0, 1);
            var ind = parseInt(attr.substring(1, attr.length));
            ind = ind + numrows;
            $(this).attr("r", pre + ind);
        });

        function addRow(index, data) {
          var row = sheet.createElement('row');
          row.setAttribute("r", index);              
          for (i = 0; i < data.length; i++) {
            var key = data[i].k;
            var value = data[i].v;

            var c  = sheet.createElement('c');
            c.setAttribute("t", "inlineStr");
            c.setAttribute("s", "2");  /*set specific cell style here*/
            c.setAttribute("r", key + index);

            var is = sheet.createElement('is');
            var t = sheet.createElement('t');
            var text = sheet.createTextNode(value)

            t.appendChild(text);                                      
            is.appendChild(t);
            c.appendChild(is);

            row.appendChild(c);
            debugger;
          }

          return row;
        }

        //add data to extra rows
        var countryStateList = 'asd';
        var agencyValue = 'asd';
        var reportGroupList = 'asd';

        var r1 = addRow(1, [{ k: 'A', v: 'Report Filter Criteria:' }, { k: 'B', v: '' }]);   //add one cell for row 1 
        var r2 = addRow(2, [{ k: 'A', v: 'Country/State:' }, { k: 'B', v: countryStateList }]); //add two cells for row 2-4
        var r3 = addRow(3, [{ k: 'A', v: 'Agency:' }, { k: 'B', v: agencyValue }]);
        var r4 = addRow(4, [{ k: 'A', v: 'Report Group:' }, { k: 'B', v: reportGroupList }]);

        var sheetData = sheet.getElementsByTagName('sheetData')[0];
        sheetData.insertBefore(r4,sheetData.childNodes[0]);
        sheetData.insertBefore(r3,sheetData.childNodes[0]);
        sheetData.insertBefore(r2,sheetData.childNodes[0]);
        sheetData.insertBefore(r1,sheetData.childNodes[0]);

      }
    }]
  });
});

参考文献

Excel Export Add Rows and Data