javascript - 如何将html正文表格导出到Excel?

标签 javascript jquery html excel html-table

我想导出除页脚之外的整个表格。无论如何也不导出 <tfoot>表的?

这是我的表格的骨架结构:

  <div id="table">
        <table data-filter="#filter" class="footable" style="background-color:white;">
            <thead>
                <tr>
                    <th>Employee ID</th>
                    <th>Branch Code</th>
                    <th>Client ID</th>
                    <th>Amount</th>
                    <th>Report Timestamp</th>
                </tr>
            </thead>
            <tbody>
                <?php 
                if(count($result)==0)
                { echo "<tr><td colspan='5' style='text-align:center; font-weight:bold;'>No data available</td></tr>"; }
                else
                {
                for($i=0; $i<count($result); $i++){?>
                <tr>
                    <td>1</td>
                    <td>2</td>
                    <td>3</td>
                    <td>4</td>
                    <td>5</td>
                </tr>

                <?php }
                }
                ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="8">
                        <div class="pagination pagination-centered"></div>
                    </td>
                </tr>
            </tfoot>
        </table>
        </div>

这是我将表格导出到 Excel 时使用的脚本:

<script>
(function () {
    var cache = {};

    this.tmpl = function tmpl(str, data) {
        // Figure out if we're getting a template, or if we need to
        // load the template - and be sure to cache the result.
        var fn = !/\W/.test(str) ? cache[str] = cache[str] || tmpl(document.getElementById(str).innerHTML) :

        // Generate a reusable function that will serve as a template
        // generator (and which will be cached).
        new Function("obj",
            "var p=[],print=function(){p.push.apply(p,arguments);};" +

        // Introduce the data as local variables using with(){}
        "with(obj){p.push('" +

        // Convert the template into pure JavaScript
        str.replace(/[\r\t\n]/g, " ")
            .split("{{").join("\t")
            .replace(/((^|}})[^\t]*)'/g, "$1\r")
            .replace(/\t=(.*?)}}/g, "',$1,'")
            .split("\t").join("');")
            .split("}}").join("p.push('")
            .split("\r").join("\\'") + "');}return p.join('');");

        // Provide some basic currying to the user
        return data ? fn(data) : fn;
    };
})();

var tableToExcel = (function () {
    var uri = 'data:application/vnd.ms-excel;base64,',
        template = '<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"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>Sheet 1</x:Name><x:WorksheetOptions><x:Print><x:ValidPrinterInfo/></x:Print></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--><body>{{for(var i=0; i<tables.length;i++){ }}<table>{{=tables[i]}}</table>{{ } }}</body></html>',
        base64 = function (s) {
            return window.btoa(unescape(encodeURIComponent(s)))
        },
        format = function (s, c) {
            return s.replace(/{(\w+)}/g, function (m, p) {
                return c[p];
            })
        }
    return function (tableList, name) {
        if (!tableList.length > 0 && !tableList[0].nodeType) table = document.getElementById(table)
        var tables = [];
        for (var i = 0; i < tableList.length; i++) {
            tables.push(tableList[i].innerHTML);
        }
        var ctx = {
            worksheet: name || 'Worksheet',
            tables: tables
        };
        window.location.href = uri + base64(tmpl(template, ctx))
    }
})();
function download(){
    tableToExcel(document.getElementsByTagName("table"), "Sheet 1");
}
var btn = document.getElementById("btn");
btn.addEventListener("click",download);
</script>

编辑

好的,我已经尝试了上述脚本,结果如​​下: enter image description here 知道为什么会这样吗?我需要网格线。我还需要删除页脚。我尝试了 unix 的第二个答案,但失败了。

这是代码:

    <script type="text/javascript">
        $(document).ready(function () {
            var something = '<table><tfoot></tfoot></table>'.replace(/<\/?tfoot>/g, '');
            $(something).appendTo('#table');
            $("#btn").click(function () {
                $("#table").btechco_excelexport({
                    containerid: "table"
                   , datatype: $datatype.Table
                });
            });
        });
    </script>

最佳答案

你可能想尝试这样的事情

var something = '<table><tfoot></tfoot></table>'.replace(/<\/?tfoot>/g, '');
$(something).appendTo('something');

并以某种方式将其合并到您的代码中。这将删除 excel 正文中的所有 html 内容。

关于javascript - 如何将html正文表格导出到Excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25903992/

相关文章:

javascript - JavaScript 和 onclick 事件的问题

javascript - 将动态 HTML 表值发布到 jQuery AJAX

javascript - jQuery Instagram Bootstrap3 动态轮播

jquery - 为什么我的选择下拉列表之一会在内容加载到其下方后消失?

MySQL 表导出为 HTML

javascript - 使用 .attr() 更新 Textarea 在输入和按键时增加++ 一行

javascript - 在 iOS 上长按按钮(Javascript)

javascript - 无法解析要发送到 View 的 JSON 值

sql - Web SQL 数据库或索引数据库 API ... 或两者兼而有之?

javascript - WebDriverJS 控制流程