在Java脚本中将HTML表导出到EXCEL [英] Export HTML Table to EXCEL in Java script

查看:60
本文介绍了在Java脚本中将HTML表导出到EXCEL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉Java script.i需要将表从 HTML 导出到 Excel



我的表格包含名称,年龄,日期,金额和图像列,图片小。

代码在这里。

I am new to Java script.i need to export table from HTML to Excel.

My table consists of Name, Age, Date, Amount and Image column with small image.
Code goes here.

function downloadsalesreport () {
    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("#tablesalesentry")
                    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");

        }



它完美无缺,但问题出在 Excel 图像没有正确对齐。

Excel 中,由 Images 组成的单元格已折叠但剩余数据(名称,年龄,日期,金额 )完美安排。如果不是,请向我建议任何关于导出表到 Excel 的想法。


It works perfectly but my problem is in Excel, Images are not aligned properly.
In Excel cells, which consists of Images are collapsed but remaining data(Name, Age, Date, Amount) are perfectly arranged. if not please suggest me any idea regarding export table to Excel.

推荐答案

1 \ r)
.replace( /\t=(.*?)}} / g,',


1,'
.split(\ t)。join('
);
.split(
}} )。join( p.push('
.split(\\ \\ r)。join(\\'

+
' );} return p.join(' ' ););

//为用户提供一些基本的currying
返回数据?f n(数据):fn;
};
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("#tablesalesentry")
                    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");

        }



它完美无缺,但问题出在 Excel 图像没有正确对齐。

Excel 中,由 Images 组成的单元格已折叠但剩余数据(名称,年龄,日期,金额 )完美安排。如果没有,请告诉我有关出口表到 Excel 的任何想法。


It works perfectly but my problem is in Excel, Images are not aligned properly.
In Excel cells, which consists of Images are collapsed but remaining data(Name, Age, Date, Amount) are perfectly arranged. if not please suggest me any idea regarding export table to Excel.


这篇关于在Java脚本中将HTML表导出到EXCEL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆