英文:
Datatable.net Built in styles for word wrap
问题
我正在使用Datatable.net,并希望我的Excel导出能够自动换行。我已经尝试过使用 $(this).attr('s', 55);
,就像这个链接中所示:https://datatables.net/reference/button/excelHtml5#Built-in-styles。我还尝试过设置宽度,看看是否有帮助。我还尝试过添加 <th class='wrap'>"
,但都没有起作用。
JS 代码
$(tableId).DataTable({
"lengthMenu": [
[10, 15, 20, -1],
[10, 15, 20, "All"]
],
"pageLength": -1,
scrollX: true,
buttons: [
{
extend: 'excelHtml5',
title: "Individual Report - " + moment().format('MM-DD-YYYY'),
customize: function (xlsx) {
var sheet = xlsx.xl.worksheets["sheet1.xml"];
var col = $("col", sheet);
col.each(function () {
$(this).attr('s', 55);
$(this).attr('width', 33);
});
}
}
]
});
(Note: The code is provided in its original form with proper formatting, and I have not made any translations for code-related terms.)
英文:
I am using Datatable.net and wanted my excel export to word wrap. I have tried using $(this).attr('s', 55); like this https://datatables.net/reference/button/excelHtml5#Built-in-styles. I have also tried setting the width and seeing if that helps. I have also tried added <th class='wrap'>" but nothing has been working.
JS Code
$(tableId).DataTable({
"lengthMenu": [
[10, 15, 20, -1],
[10, 15, 20, "All"]
],
"pageLength": -1,
scrollX: true,
buttons: [
{
extend: 'excelHtml5',
title: "Individual Report - " + moment().format('MM-DD-YYYY'),
customize: function (xlsx) {
var sheet = xlsx.xl.worksheets["sheet1.xml"];
var col = $("col", sheet);
col.each(function () {
$(this).attr('s', 55);
$(this).attr('width', 33);
});
}
}
]
);
答案1
得分: 1
$(this).attr('s', '55');
$('<c>', sheet).attr('s', '55');
英文:
The selector you are using for the built-in style needs to be adjusted from this:
$(this).attr('s', 55);
to this:
$('row c', sheet).attr( 's', '55' );
The $("col", sheet)
selector is for the <cols>
tags in the worksheet XML - for example:
<cols>
<col min="1" max="1" width="18.85546875" customWidth="1"/>
<col min="2" max="2" width="24.28515625" customWidth="1"/>
</cols>
That is correct for setting widths.
But for the style s
attribute, you need to target the <c>
tag - for example:
<row r="2" spans="1:2">
<c r="A2" t="s">
<v>2</v>
</c>
<c r="B2" t="s">
<v>3</v>
</c>
</row>
Here is some demo code (it doesn't generate an Excel in a Stack Snippet, so here is the core code):
$(document).ready(function() {
var table = $('#example').DataTable( {
dom: 'Bfrtip',
buttons: [
{
extend: 'excelHtml5',
text: 'Save as Excel',
title: '',
customize: function( xlsx ) {
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row c', sheet).attr( 's', '55' );
var col = $("col", sheet);
col.each(function () {
$(this).attr('width', 15);
});
}
}
]
} );
} );
The end result (with my test data):
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论