Javascript PHP网站:导出到Excel-并剥离和标记
我目前有一个工作Excel从导出到可下载Excel文件。 然而,在网站上的我的表格中,单元格的内容链接到一个单独的系统,该系统在表格中列出客户,围绕客户名称的链接将带您访问我们的CRM和客户。在最后一列中,我还有两个图像,它们充当按钮/链接来更改该行中的值 然而,我不希望这些链接和图像导出到Excel中-我只想要单元格的内容 下面是javascript/jquery.table2excel.js的代码:Javascript PHP网站:导出到Excel-并剥离和标记,javascript,php,Javascript,Php,我目前有一个工作Excel从导出到可下载Excel文件。 然而,在网站上的我的表格中,单元格的内容链接到一个单独的系统,该系统在表格中列出客户,围绕客户名称的链接将带您访问我们的CRM和客户。在最后一列中,我还有两个图像,它们充当按钮/链接来更改该行中的值 然而,我不希望这些链接和图像导出到Excel中-我只想要单元格的内容 下面是javascript/jquery.table2excel.js的代码: //table2excel.js (function ( $, window, docume
//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.text+;
结果是标记确实被剥离了,但所有列都合并到了一个列中。通过更改此代码,您应该能够剥离出有问题的元素:
e.tableRows += "<tr>" + $(o).html() + "</tr>";
为此:
e.tableRows += "<tr>" + $(o).html().replace(/<a.+?<\/a>|<img.+?>/g, '') + "<tr>";
另一个选项是创建第二个带有display的表:无,不包含您不需要的功能,并在导出脚本中对该表执行操作。关闭,但不显示!这只给了我文本,而不是html。这很好。但所有列都合并为一列。我现在得到的不是:第1列|第2列|第3列,而是:第1列第2列第3列。我改变了我的答案,去掉了所有的和标记,同时保留了行中剩余的html。Darnit-也不起作用。现在,我的第一行列标题在单独的列中导出。到现在为止,一直都还不错。但下载的excel文件中不包括其他750个表格行:/display:none;这个方法很有效,但它让我做了第二个SQL查询,而且在我看来有点混乱。我更喜欢striptags/replace方法。上面的代码不应该改变循环逻辑。你有没有检查你的控制台有没有错误?