我想扩展理查兹的答案。像 Richard 一样,我无法根据 Datatables 文档找出解决方案。我想要一个 excelHtml5 导出,所有字段都只导出为文本。
理查兹解决方案帮助我找到了我将在下面发布的解决方案。
对于 Datatables 1.10.12,html5 按钮代码出现在单独的文件buttons.html5.js 中。
正如 Richard 所指出的,搜索 DataTable.ext.buttons.excelHtml5 块。
我感兴趣的一段代码是:
// Detect numbers - don't match numbers with leading zeros or a negative
// anywhere but the start
if ( typeof row[i] === 'number' || (
row[i].match &&
$.trim(row[i]).match(/^-?\d+(\.\d+)?$/) &&
! $.trim(row[i]).match(/^0\d+/) )
) {
cell = _createNode( rels, 'c', {
attr: {
t: 'n',
r: cellId
},
children: [
_createNode( rels, 'v', { text: row[i] } )
]
} );
}
else {
// Replace non standard characters for text output
var text = ! row[i].replace ?
row[i] :
row[i]
.replace(/&(?!amp;)/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, '');
cell = _createNode( rels, 'c', {
attr: {
t: 'inlineStr',
r: cellId
},
children:{
row: _createNode( rels, 'is', {
children: {
row: _createNode( rels, 't', {
text: text
} )
}
} )
}
} );
}
为了使 excelHtml5 按钮仅导出文本,我删除了将字段标识为潜在数字的 IF 块。我们的客户还要求在任何空白字段中添加“<>”,因此我删除了 < 和 > 的两种替换方法。
// Replace non standard characters for text output
var text = ! row[i].replace ?
row[i] :
row[i]
.replace(/&(?!amp;)/g, '&')
.replace(/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, '');
cell = _createNode( rels, 'c', {
attr: {
t: 'inlineStr',
r: cellId
},
children:{
row: _createNode( rels, 'is', {
children: {
row: _createNode( rels, 't', {
text: text
} )
}
} )
}
} );
此更改允许 excel 按钮将所有值导出为文本。Excel 不再切换我的 < 和 > 并且我的数字都是文本,没有科学计数法。