I am using the Datatables TableTools plugin in order to provide an Export to Excel option for a table on my page.
In general everything works as intended. My only issue is that I would need all the data resp. the columns in the resulting Excel table being formatted as text as otherwise I am losing data in some columns.
Examples:
- I have a column that has leading zeros (e.g. 0022
) which only appears with the leading zeros cut off (e.g. 22
) in the Excel file if this is not formatted as text.
- Another column contains 19-digit account numbers (e.g. 1234567890123456789
) which appears with the last four digits being changed to zeros (e.g. 1234567890123450000
) in the Excel file if this is not formatted as text.
Is there any way I can set this in my Datatables / TableTools initialisation so that it always exports all data as text into the Excel file ?
Many thanks for any help with this, Tim.
Solution of datatable expord CSV with special characters. Find charset from https://cdn.datatables.net/buttons/1.1.2/js/buttons.html5.js or https://cdn.datatables.net/buttons/1.1.2/js/buttons.html5.min.js
and change it to
UTF-8-BOM
fromUTF-8
I have the solution to this problem.
It was broken my head very much time... So the explain is below this:
Search in the follow lines until take this code, and comment it:
Put this new code :
Save your datatables.js
This solution helps to maintain the number, date, and decimal format's.
I changed the code to force to write in text format all values from the HTML to the XLSX.
If anybody have a question about this solution, I will try to response all of them questions.
Thanks to all.
I would like to expand on Richards answer. Like Richard, I could not figure out the solution based on the Datatables documentation. I wanted an excelHtml5 export with all fields being exported as text only.
Richards solution helped me get to the solution that I will post below.
For Datatables 1.10.12 the html5 buttons code appears in a separate file buttons.html5.js.
As Richard noted, search for the DataTable.ext.buttons.excelHtml5 block.
The piece of code I was interested in was:
In order to make the excelHtml5 button export ONLY text, I removed the IF block that would identify a field as a potential number. Our customer also had a specific request to have '<>' in any field that was blank so I removed the two replace methods for < and >.
This change allows the excel button to export all values as text. Excel no longer switches my < and > and my numbers are all text, no scientific notation.
This specific problem has been answered elegantly in this post - https://stackoverflow.com/a/165052/6169225. Let's say that you have an integer
your_integer
that you want displayed as a string (i.e. the leading zeros displayed). Then you simply format it as such before you export to excel -="<your_integer>"
. When the excel doc auto-downloads, your integer will already display as a string using this method.For the numbers that are 19 digits, the issue is actually Excel rounding the numbers when it opens the data. Please see this thread for more info and possible workarounds: https://datatables.net/forums/discussion/comment/132633#Comment_132633
TableTools does not create a real
excel
file, it creates acsv
file instead. Those contain only raw data, no formatting. Although the leading zeros are there, Excel usually will not show them. You have several options here:csv
file from Excel's open dialog, from which you should be able to mark columns as text (you might need to change the file type totxt
)