drw85135 2015-03-03 14:55
浏览 95

PHP网站:将<table>导出到Excel - 并删除<img>和<a>标签

I currently have a working Excel export from a to a downloadable excel file. However, in my table (on the website) the contents of cells links to a seperate system (Listing customers in a table, and the link wrapped around the customer name will take you to our CRM and onto the customer). Also in the last column I have two images that acts as buttons/links to change a value in that row.

However I dont want these links and images exported into the Excel - I just want the contents of the cells.

Heres the code (javascript / jquery.table2excel.js):

//table2excel.js
(function ( $, window, document, undefined ) {
    var pluginName = "table2excel",
            defaults = {
            exclude: ".noExl",
            name: "Table2Excel"
    };

    // The actual plugin constructor
    function Plugin ( element, options ) {
            this.element = element;
            // jQuery has an extend method which merges the contents of two or
            // more objects, storing the result in the first object. The first object
            // is generally empty as we don't want to alter the default options for
            // future instances of the plugin
            this.settings = $.extend( {}, defaults, options );
            this._defaults = defaults;
            this._name = pluginName;
            this.init();
    }

    Plugin.prototype = {
        init: function () {
            var e = this;
            e.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>";
            e.template += "<x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions>";
            e.template += "<x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></body></html>";
            e.tableRows = "";

            // get contents of table except for exclude
            $(e.element).find("tr").not(this.settings.exclude).each(function (i,o) {
                e.tableRows += "<tr>" + $(o).html() + "</tr>";
            });
            this.tableToExcel(this.tableRows, this.settings.name);
        },
        tableToExcel: function (table, name) {
            var e = this;
            e.uri = "data:application/vnd.ms-excel;base64,";
            e.base64 = function (s) {
                return window.btoa(unescape(encodeURIComponent(s)));
            };
            e.format = function (s, c) {
                return s.replace(/{(\w+)}/g, function (m, p) {
                    return c[p];
                });
            };
            e.ctx = {
                worksheet: name || "Worksheet",
                table: table
            };
            window.location.href = e.uri + e.base64(e.format(e.template, e.ctx));
        }
    };

    $.fn[ pluginName ] = function ( options ) {
            this.each(function() {
                    if ( !$.data( this, "plugin_" + pluginName ) ) {
                            $.data( this, "plugin_" + pluginName, new Plugin( this, options ) );
                    }
            });

            // chain jQuery functions
            return this;
    };

})( jQuery, window, document );

Update 1:

Did not work replacing

e.tableRows += "" + $(o).html() + "";
with
e.tableRows += "" + $(o).text() + "";

The result was that the tags were indeed stripped, but all columns got merged into one single column.

  • 写回答

1条回答 默认 最新

  • doukuangxun5382 2015-03-03 15:09
    关注

    You should be able to strip out the offending elements by changing this code:

    e.tableRows += "<tr>" + $(o).html() + "</tr>";
    

    to this:

    e.tableRows += "<tr>" + $(o).html().replace(/<a.+?<\/a>|<img.+?>/g, '') + "<tr>";
    

    The other option would be to create a second table with display: none without the features you don't need, and act on that table in your export script.

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100