I am getting date columns as general type in excel
I am getting date columns as general type in excel
{
extend: 'excelHtml5',
title: pg_name,
exportOptions: {
columns: ':not(.no-print):visible',
orthogonal: null,
format: {
body: function (data, row, column, node) {
var $node = $(node);
if ($node.find("span.clsColsDateStr").length) {
$node.find("span.clsColsDateStr").remove();
}
var text = $node.text().trim();
var m = moment(text, ['DD-MMM-YYYY'], true);
if (m.isValid()) {
return m.format('YYYY-MM-DD');
}
return text || "";
}
}
},
customize: function (xlsx) {
}
}
Answers
Can you link to a test case showing the issue please? ISO8061 formatted date strings should be dates in Excel after the export.
Allan
Inserting date values into Excel can be tricky. The internal storage format is number of days since
1899-12-30T00:00:00Z.Based on that, my solution looks like this:
I am using Luxon rather than outdated moments.js. But I think, it should be no big deal to convert it.
Many thanks. Interesting to see how you've done it.
For interest, this is how Buttons does it. It only supports YYYY-MM-DD at the moment.
Allan
Looks like there is a problem in version 3. In version this worked fine, now I see in Excel the numeric value, e.g. 40991.541666666664
My custom render function is this:
I suspect the dt export-button exports a string, despite
d.diff(DateTime.fromISO('1899-12-30', { zone: 'utc' }), 'days').daysis a number.In XML data, a numeric column looks like this:
<row r="3"><c t="n" r="A3"><v>62201</v></c>whereas my date field is:
<c t="inlineStr" r="H3" s="68"><is><t xml:space="preserve">40991.541666666664</t></is></c>I would rather expect
<c r="H3" s="68"><v>40991.541666666664</t></c>or something similar.
Any ideas?
Hi,
Can you link to a test case showing the issue so I can look into it in detail please?
Thanks,
Allan
I tried this workaround:
It works in principle, i.e when I open the sheet, then Excel complains about "a problem with some content" and is able to correct it. But certainly not the final solution.
I did not manage it to make it running on https://live.datatables.net but this code should work:
I've just put your code into this bin and it will generate a file okay. I've managed to open it in both LibreOffice and Excel (web version) without any issue:
I don't have Excel native on this machine I'm afraid (Linux / KDE), so I can't test that. However, if you are getting "a problem with some content" from your custom XML, it would be a case of inspecting what Excel actually needs and seeing what is different.
Normally I would do that by allowing it to correct the error, then change the .xlsx file to a .zip, unzip and inspect the XML it created. That might also need to be cross referenced to the open spreadsheet spec. I'm afraid that for custom exports, that is something that is outside of the free support that I can provide.
Allan
Yes, on my computer at home it displays
But it is still a string, not a date. When you try
=A3+1then it should return the next day, but I get a#VALUE!error.No worry, I will find it out.
Wernfried