Adsense
Popular Posts
- Code update from PHP 7.4 to PHP 8.1 - PhpSpreadsheet
- MySQL workbench -"Could not decrypt password cache"
- Code update from PHP 7.4 to PHP 8.1 - Worksheet/Iterator.php
- Rendering HTML tags inside textarea
- axios handle blob type data
- Unix Utils and wget in Windows
- increase mysql query speed
- Setup vi syntax for PHP
- EXCEL return to the next line in the same cell
- Get WAMP running on EC2
Tuesday, February 13, 2018
DataTable FAQ - Set text align and cell width when exporting to EXCEL
1) To set A column text left aligned when exporting to EXCEL using DataTable
$('row c[r^="A"]', sheet).attr( 's', '50' );
2) To set All column width 20
var col = $('col', sheet);
col.each(function () {
$(this).attr('width', 20);
});
3) To set first column width 10
$(col[0]).attr('width', 10);
4) Example JS code is as following:
var buttonCommon = {
exportOptions: {
format: {
body: function(data, row, column) {
data = data.replace(/<br\s*\/?>/ig, "\r\n");//should be with wrapped text
return data;
},
header: function(data, column, row) {
data = data.replace(/<br\s*\/?>/ig, "\r\n");//should be with wrapped text
return data;
}
}
}
};
$.extend(true, $.fn.dataTable.defaults, {
"lengthChange": false,
"pageLength": 100,
"orderClasses": false,
"stripeClasses": [],
"destroy": true,
dom: 'Bfrtip',
buttons: [
$.extend(true, {}, buttonCommon, {
extend: 'excel',
exportOptions: {
columns: ':visible'
},
customize: function(xlsx) {
var sheet = xlsx.xl.worksheets['sheet1.xml'];
$('row c[r^="A"]', sheet).attr( 's', '50' ); //<-- left aligned text for A column
var col = $('col', sheet);
col.each(function () {
$(this).attr('width', 20);
});
$(col[0]).attr('width', 10);
$(col[1]).attr('width', 40);
$(col[2]).attr('width', 50);
}
})
]
}
);
Labels:
datatable
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment