PHP网站:导出<表>到Excel-并剥离<img>并且<a>标签

PHP Website: Export <table> to Excel - And strip <img> and <a> tags

本文关键字:lt gt img 剥离 并且 标签 Excel- 网站 PHP 导出      更新时间:2023-09-26

我目前有一个工作Excel导出,从导出到可下载的Excel文件。然而,在我的表格(在网站上)中,单元格的内容链接到一个单独的系统(在表格中列出客户,包裹在客户名称周围的链接将带您进入我们的CRM并找到客户)。同样在最后一列中,我有两个图像,它们充当按钮/链接来更改该行中的值。

然而,我不想把这些链接和图像导出到Excel中——我只想要单元格的内容。

下面是代码(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 );

更新1:

更换

无效e.tableRows++"+$(o)html()+"

e.tableRows++"+$(o)文本()+"

结果是,标签确实被剥离了,但所有列都被合并为一列。

您应该能够通过更改以下代码来去除有问题的元素:

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

到此:

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

另一种选择是使用display: none创建第二个表,但不包含不需要的功能,并在导出脚本中对该表执行操作。