Excel export and newlines
Excel export and newlines
I found lots of posts on this but have not come up with a solution. My goal is to display the text with new lines. The closest I've come to making this work is to replace newlines like this: data.replace( /\n/g, '"&CHAR(10)&CHAR(13)&"' )
. The problem is that the cell needs to be a formula. Simply prepending =
to the line doesn't work. However if I edit that cell in Excel and replace the =
by typing =
its turned into a function and works. Although I need to also enable wrap text.
I'm returning something that looks like this:
="Line 1"&CHAR(10)&CHAR(13)&"Line 2"
Instead of returning this as a string I need to return it in a way that Excel interprets it as a function. Any ideas?
Bonus question.... How do I enable wrap text in the export function? Don't mind if I need to manually enable it in Excel.
I'm not tied to this solution if there are other working options.
Kevin
This question has an accepted answers - jump to answer
Answers
To wrap the text you can use style
55
from the style options built into theexcelHtml5
button type (this example shows how to apply styles).If I recall correctly you can use
50
to work with new line characters. However, I've not tried using formulas at all in the exported files since they don't have any meaning to DataTables itself.What to do is to create a seriously simply spreadsheet in Excel cell A1 as
=(1+1)
or something. Save it and then unzip the xlsx file. Have a look in the generated XML and there might be something "magic" about how it defines which cells contain formulas.I'll take a pop at that in the next few days.
Allan
Thanks for the info, I will check it out and respond with the results.
Kevin
This was a fun puzzle. I was able to get it working. Below is how I did it. Please let me know if there is a better or more efficient way.
I looked Office XML information and found this:
The inline string Datatables uses looks like this:
A formula looks like this. Apparently the
<v>
element is used for numbers so not needed for my purposes.First the data needs to be reformatted. Found that I can't use
&
as it results in aninnerHTML
error. Instead I need to build the string representing the Excel CONCATENATE function. Instead of returning"Line 1"&CHAR(13)&"Line 2"
I need to returnCONCATENATE("Line 1", CHAR(13), "Line 2")
. The Mac rendersCHAR(13)
as a new line. Other OS's may needCHAR(10)
. Might be a good idea to include both. This data already has\n
for newlines. If the data has<br>
and<p></p>
elements then the formatting function will need to change them to\n
for the split method to work.Also found that double quotes in the data need to be single quotes. Escaping them causes Excel file format errors. The double quotes around each line need to be escaped. I use the below to build the string.
The next step is to apply the wrap text attribute and change the cell type from
inlineStr
tostr
using thecustomize
function. Also need to add the<f>
element with the reformatted string and removal the<is> <t>
elements.I do need to change the format function to skip the first row. Otherwise the heading is seen as a formula and shows as a name error in Excel.
Let me know if improvements can be made.
Kevin
Wow - that's awesome analysis. I don't immediately see anything that I would change - it looks good to me!
Allan
I've seen some postings about setting the row height but no answers. I was able to use this in the
customize
function to set the row height:This shows five or so lines of my wrapped text starting in the middle. But I would like to set the vertical alignment to the top. I didn't see a builtin style for it in the
excelHtml5
docs. Does anyone know how to set the vertical alignment?I'll post a response if I find the answer.
Kevin
Got the vertical alignment working. One of the posts by @F12Magic pointed me in the right direction:
https://datatables.net/forums/discussion/comment/102413/#Comment_102413
Not sure this is worth all the hassle but was interesting to learn about
Kevin
I think you are more knowledgable in the xlsx format than myself now. I'm not sure if you should be proud of that or not ;-).
I've thought a few times about spinning off the xlsx part of Buttons into its own library as that would be quite an interesting project. But also potentially a lot of work since Excel has so many edge cases.
Allan
@kthorngren hi can you post an example working of a custom style with cell top aligned and wrapped text together? example from this post I get corrupted excel: https://datatables.net/forums/discussion/comment/102413/#Comment_102413
Here is what worked for me (no formula):
Note 1: depending on the OS use either '\n' or '\r' or both . I had to worry only about Windows, so using '\n'.
Note 2: I did not care about HTML, only export, so data was assigned as array of arrays
STEP 1: Format the data by replacing '\n' with some special character Excel export won't strip
if (str != null){ return str.replace(/(\r\n|\n|\r)/gm,"~"); } else { return null; }
STEP 2: In "customize: function( xlsx )" for "buttons:" in options of the table add special processing for column (can be also done for row or cell). In my case it was column "D"
$('row c[r^="D"]', sheet).each(WrapAndBreak);
Here is WrapAndBreak function:
https://stackoverflow.com/questions/35761577/export-value-with-linebreaks-into-single-cell-in-excel-with-jquery-datatables-20
However, you need to press the "wrap text" button when opening the excel. If someone knows a way to have it wrapped automatically, please let me know.
Have you looked at the code provided in this thread? It shows how to format the cell for wrapped text.
Kevin