我希望我的解决方案能够帮助某人将 Excel 导出中的链接扩展到已经非常
有用的图书馆。
经过几个小时的搜索,我发现很多人在此处和 Datatables 论坛中寻找 Excel 导出链接的解决方案。
主要问题是默认导出仅考虑两种不同的格式。数字和内联字符串。
链接既不是内联字符串也不是数字,它是一个函数,需要输入 str。
在寻找解决方案的过程中,我发现了许多有用的部分。
您必须调整导出,已经为此提供了“自定义”选项。https://datatables.net/extensions/buttons/examples/html5/excelTextBold.html https://datatables.net/extensions/buttons/examples/html5/excelTextBold.html在此示例中,考虑了 C 列中的所有单元格。我们想要循环所有单元格并在那里找到可能的 URL。
我们想用公式替换链接。默认情况下,它具有内联单元格类型,必须将其替换为 str 类型和用作值的公式。
感谢 Dzyann,他展示了它是如何工作的。https://datatables.net/forums/discussion/42097/can-you-export-a-table-and-format-a-cell-to-use-a-formula-using-orthogonal-data https://datatables.net/forums/discussion/42097/can-you-export-a-table-and-format-a-cell-to-use-a-formula-using-orthogonal-data
要在链接下划线,应采用格式 [4]。
可用格式列表:https://datatables.net/reference/button/excelHtml5#Built-in-styles https://datatables.net/reference/button/excelHtml5#Built-in-styles
我的解决方案适合我的要求:
// (1.) customize export
customize: function( xlsx ) {
var sheet = xlsx.xl.worksheets['sheet1.xml'];
// Loop over all cells in sheet
$('row c', sheet).each( function () {
// if cell starts with http
if ( $('is t', this).text().indexOf("http") === 0 ) {
// (2.) change the type to `str` which is a formula
$(this).attr('t', 'str');
//append the formula
$(this).append('<f>' + 'HYPERLINK("'+$('is t', this).text()+'","'+$('is t', this).text()+'")'+ '</f>');
//remove the inlineStr
$('is', this).remove();
// (3.) underline
$(this).attr( 's', '4' );
}
});
}
更新!! IE11
在 neirda 发现 IE11 在向 $(this) 添加非 HTML 对象时出现问题后,必须找到另一个解决方案。相同的基础:<f> HYPERLINK
文件:buttons.html5.js
线路:1098
插入了一个开关,可为 URL 内容创建不同的 Celle。 (作为公式,带有超链接)
// Formula HYPERLINK for http-content,
// is a URL if: a) started first char of cell content and
// b) without blanks
// s:4 use to unterline
if ( (row[i].indexOf("http") === 0)
&&
(row[i].indexOf(" ") < 0 ) ) {
cell = _createNode( rels, 'c', {
attr: {
t: 'str',
r: cellId,
s: 4
},
children:{
row: _createNode( rels, 'f', { text: 'HYPERLINK(\"'+text+'\",\"'+text+'\")' } )
}
} );
} else {
// String output - replace non standard characters for text output
cell = _createNode( rels, 'c', {
attr: {
t: 'inlineStr',
r: cellId
},
children:{
row: _createNode( rels, 'is', {
children: {
row: _createNode( rels, 't', {
text: text
} )
}
} )
}
} );
}