PHP网站:导出<表>到Excel-并剥离<img>并且<a>标签
PHP Website: Export <table> to Excel - And strip <img> and <a> tags
我目前有一个工作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
创建第二个表,但不包含不需要的功能,并在导出脚本中对该表执行操作。
相关文章:
- 在<页眉>标签
- 如何更改<svg>标记为<img>用js标记
- 如何逃离<>在javascript下划线模板中
- 有没有一种方法可以检测ios<>使用jquery和触发器操作形成导航按钮
- 正则表达式,它允许除“”之外的所有字符<>_;{}[]”;
- ajax调用:响应<->数据类型
- 如果所有三个文本框<>然后是100%
- CORS的问题.烧瓶<->AngularJS
- 有没有一种方法可以创建与Java<->JSON映射对象,如JSON.stringfy(jsObject)创建的
- 什么是%<>%调用了语法
- JavaScript返回值:What dos<>意思是
- JavaScript中哪种类型的值对应于Java`List<>`
- 如何发送类似“<>"在参数中使用$.ajax-to-server
- 不等于<>操作员不工作
- jquery附加函数转换<>到实体名称
- 只有当超过3个元素时才显示更多内容按钮,否则不显示.使用jquery's:lt:gt
- 为什么在<>ckeditor中不支持
- url有效性的Java脚本regexp,带有<>
- 设计Javascript前端<->C++后端通信
- 比较查询运算符ObjectId<->日期