首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >HTML表到Excel,浏览器阻塞

HTML表到Excel,浏览器阻塞
EN

Stack Overflow用户
提问于 2013-07-25 09:43:02
回答 1查看 775关注 0票数 0

我下载了Javascript函数,用于将HTML表转换为excel。它将将页面中的所有表转换为excel,实际上,我只需要将一个特定的表转换为也可以在Button中单击的excel。但是下载的javascript代码执行Page本身。有人能指导我如何使它成为函数,也能为特定的表(给定的表id )提供指导吗?

代码语言:javascript
运行
复制
    <table id="testTable">
        <thead>
            <tr>
                <th>Name</th>
                <th>ACP</th>
                <th>OEMCP</th>
                <th>Unix<br>
                    NT 3.1</th>
                <th>Unix<br>
                    NT 3.51</th>
                <th>Unix<br>
                    95</th>
            </tr>
        </thead>
    </table>
    <table id="testTable1">
        <thead>
            <tr>
                <th>Name</th>
                <th>ACP</th>
                <th>OEMCP</th>
                <th>Windows<br>
                    NT 3.1</th>
                <th>Windows<br>
                    NT 3.51</th>
                <th>Windows<br>
                    95</th>
            </tr>
        </thead>
    </table>
    <script type="text/javascript">
    (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>{{=worksheet}}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><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))
        }
    })();        
    tableToExcel(document.getElementsByTagName("table"),"one");
    </script> 

从上面的代码,我只需要"testTable“excel导出。同样,当我执行这个javascript时,浏览器会得到stucks。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-25 09:46:06

tableToExcel(document.getElementsByTagName("table"),"one");

这是你的关键路线!

给它一个表列表和新工作表的名称。

如果你只想要一个:

代码语言:javascript
运行
复制
function expTable(tbl)
{
     var tables = [];
     tables.push(tbl);
     tableToExcel(tables,"myTableExcelWorkbook");
}

使用expTable(document.getElementById(tableId));运行它

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17854415

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档