Export to Excel with same line breaks

I'm using:

 pubLink: function(field, data, column){
 let link = "<a rel='nofollow' href='" + data["link"] + "' target='_blank'>" + data['title'] + "</a><br>" + data['authors'] + "<br>" +   data['publication'] ;
 return link
  }

To display multi line information in a grid column.  The data looks fine but I was wondering is there a way to show multi lines when exporting to Excel? When I do it now the <br>'s are removed but the data is all on one line.  I have also used 

excelQueryCellInfo: function (args) {
       if(args.column.field === "title"){
       args.style = { wrapText:true };
        }
    },

That will allow text wrapping but it still does not line break in the correct location.   Is there a way to replace the BR with something that Excel will read as a line feed? I've tried adding vbCrLf but it is showing as text


14 Replies

WM William Morgenweck February 24, 2022 07:15 PM UTC

I ended up doing

excelQueryCellInfo: function (args) {

       if(args.column.field === "title"){

           args.style = { wrapText:true };

           var regex = /<br\s*[\/]?>/gi;

           args.value = args.value.replace(regex, "\n");

        }

    },

Do you think this is the best way to do it?-- it seems to work




SK Sujith Kumar Rajkumar Syncfusion Team February 25, 2022 06:26 AM UTC

Hi William, 
 
Greetings from Syncfusion support. 
 
The mentioned approach is the proper way for applying line breaks in cells for excel export. The new line tag needs to be used in the cell value(using the excelQueryCellInfo event) for applying breaks in between cell content on excel export. 
 
Please get back to us if you require any further assistance. 
 
Regards, 
Sujith R 



WM William Morgenweck June 13, 2022 03:45 PM UTC

Has anyone come up with a way to replace the HTML
tag <br> with an Excel line feed that will work?


I read an article at CodeSnip:Export DataTable to Excel: ASP Alliance that is interesting, but I can't dig that far into the Excel output


I can remove the
's but that then just removes the line feed and it seems that I can't put chr(10) or vbClLf and still have the JavaScript to work.


Thanks

Bill



RS Rajapandiyan Settu Syncfusion Team June 14, 2022 12:03 PM UTC

Hi William,


Currently, we are validating your query at our end. We will provide further details on or before June 16th, 2022.


We appreciate your patience until then.


Regards,

Rajapandiyan S



JC Joseph Christ Nithin Issack Syncfusion Team June 16, 2022 01:33 PM UTC

Hi William,


   Thanks for your patience.


   By default Excel export does not support <br/> HTML element for adding new line. We suggest to replace the <br/> element with ‘\n’ and set the cell style's WrapText as TRUE in the `excelQueryCellInfo` event of the EJ2 Grid. This will achieve the new line in Excel worksheet.


Please refer the below code example.


 

function excelQueryCellInfo(args) {

  if (args.column.field == 'FirstName') {

    args.style = { wrapText: true };

    var regex = /<br\s*[\/]?>/gi;

    args.value = args.value.replace(regex, '\n');

  }

  debugger;

}

 

 


Sample: https://stackblitz.com/edit/zg2yyc-mlki9n?file=index.js,index.html


Please let us know if you need further assistance.


Regards,

Joseph I.



WM William Morgenweck June 16, 2022 01:56 PM UTC

I've using that code for the last year and a half and maybe I'm using it wrong.  Are you saying that if I have HTML code that is being created in code of:


"See the dog <br> jump"  or "See the dog <br/> jump" I should see in Excel that there is a line break between dog and jump.  Or only that the text will wrap.  Text wrapping is not the issue.  Creating a Professional document is the issue I need to produce the out put so it looks like the image


Excel10.PNG


It is possible because I can do it with another product.   This needs to be escalated 



JC Joseph Christ Nithin Issack Syncfusion Team June 19, 2022 07:29 PM UTC

Hi William,


Thanks for your update.


Currently we are working on your query, we will provide further details on or before 21st June, 2022. We appreciate your patience until then.


Regards,

Joseph I.



JC Joseph Christ Nithin Issack Syncfusion Team June 21, 2022 07:10 PM UTC

Hi William,


Sorry for the inconvenience caused.


  We are still validating your query, we will provide further details on or before 23rd June, 2022. We appreciate your patience until then.


Regards,

Joseph I.



WM William Morgenweck June 27, 2022 04:26 PM UTC

Just checking in-- any additional thoughts on line breaks?



JC Joseph Christ Nithin Issack Syncfusion Team June 29, 2022 12:33 AM UTC

Hi William,


  Sorry for the late response.


  We have analyzed this and it is not feasible to replace <br/> tag while excel exporting as it might break other users who are already using it. As we have updated earlier, we suggest you replace the <br/> text with “\n” by using the code “args.value = args.value.replace(regex, '\n');” in the excelQueryCellInfo event of the EJ2 Grid.


Please get back to us for further details.


Regards,

Joseph I.



WM William Morgenweck June 29, 2022 12:56 PM UTC

Let me be 100% clean with what you are saying.  You are saying replace a HTML tag of <br/> or <br> with an empty string?  

So I line that looks like this in the Datagrid


Tuesday, June 7th 2022

Schedule for:

Robert Smith

Meeting with XXX

Lunch with YYY


Will look like:

Tuesday, June 7th 2022 Schedule for: Robert Smith Meeting with XXX Lunch with YYY



If that's the answer, then honestly that is not a solution.


Are you saying that there is no way possible for a Syncfusion Excel Export to show separate lines unless it is the standard text wrap?  If this is the case, then I would say that this is a bug or a missing feature 





JC Joseph Christ Nithin Issack Syncfusion Team July 4, 2022 12:15 AM UTC

Hi William,


  Sorry for the inconvenience caused.


  In our last updates we were intending to say to replace the <br> or <br/> tag with the and not the empty string. Please use the following code line
in the excelQueryCellInfo event of the EJ2 Grid which will display the content in the new line. Due to some issues the new line symbol was not displayed in the update.


Sample: https://stackblitz.com/edit/zg2yyc-mlki9n?file=index.js,index.html


Please get back to us for further details.


Regards,

Joseph I.



WM William Morgenweck July 4, 2022 01:16 PM UTC

NOW THIS IS PERFECT.  Honestly from a business point of view this can now export beautiful Excel Reports. 



VB Vinitha Balasubramanian Syncfusion Team July 7, 2022 01:40 PM UTC

Hi William,


Thanks for your update.


We are happy to hear that issue has been resolved.


Please get back to us if you need further assistance on this.


Regards,

Vinitha Balasubramanian


Loader.
Up arrow icon