Adsense
Popular Posts
- Install APXS in Redhat Linux
- MySQL workbench -"Could not decrypt password cache"
- Transfer modules between sites
- JavaScript, remove trailing insignificant zeros after toFixed function
- Set Windows path command line
- datatable order by nunmeric
- MySQL date created and date modified
- super(props) in React
- PHP, dump varailbes in a format way
- React: connect(mapStateToProps, mapDispatchToProps)
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