Export to Excel issue when column has long integer strings
Export to Excel issue when column has long integer strings
Link to test case:
Unfortunately, it is an intranet site.
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem:
I have a datatable where first column is barcode labels and they are 24 characters long. They could be all numeric characters or mix of alpha and numeric. My problem is when exporting to Excel and all labels "look" numeric.
It exports fine when label is 1234ABCD5678901234567890 or 001234567890001234567890 but labels such as 123004590218842001720584 are displayed as 123004590218842000000000 and when clicking on that cell it shows as 1.23004590218842E+23, and right justified as if number.
I tried forcing it to use column A as string using
$('row c[r^="A"]', sheet).attr('s', '50'); //"A" is Label column
Didn't work; all it did was replace 123004590218842000000000 with 1.23004590218842E+23.
This is my Excel customization section:
buttons: [
{
extend: "collection",
text: "Export",
buttons: [
{
extend: 'excel',
orientation: 'landscape',
pageSize: 'LEGAL',
customize: function (xlsx) {
var sheet = xlsx.xl.worksheets['sheet1.xml'];
var sheet2 = xlsx.xl['styles.xml'];
// use font size 10
var tagName = sheet2.getElementsByTagName('sz');
for (i = 0; i < tagName.length; i++) {
tagName[i].setAttribute("val", "10")
}
$('c[r=A1] t', sheet).text('Label Outcomes');
$('row:first c', sheet).attr('s', '2').attr('s', '32'); // first row is bold
// This didn't help, it just made the header of this column non-bold
$('row c[r^="A"]', sheet).attr('s', '50'); //"A" is Label column
$(sheet.body)
.css('font-size', '10pt');
$(sheet.body).find('table')
.addClass('compact')
.css('font-size', 'inherit');
},
exportOptions: {
columns: [0, 1, 2, 3, 4, 5, 6, 7, 8, 10]
},
},
I posted to SOF site but no response in two days so I am hoping some guru here knows how to fix this.
This question has accepted answers - jump to:
Answers
Lots of gurus here, you can't walk anywhere without tripping over them
This example from this thread should get you going. It's specifically converting that column to use strings,
Colin
Thank you Colin. Grateful for all you gurus. Problem solved.
Are you aware of any documentation that explain excel formatting within the "customize" code; things such as $('row c[r^=A], sheet).attr('s', 50))?
How to access different cells or rows or columns; how to specify the attributes and what attributes 2, 32, 50, ... mean? I am assuming above means "all Axx cells".
https://datatables.net/reference/button/excelHtml5
Search for "Built in styles".
This is about manipulating the Excel XML using jQuery. Enjoy
If you search the forum you'll find posts on this from me and other people. Can become very cumbersome.
Like in here:
Thank you all for your help. Much appreciated.