line break when exporting to excel

line break when exporting to excel

EufragioEufragio Posts: 19Questions: 2Answers: 0

I have my project in this way : https://jsfiddle.net/Eufragio/u342qgoz/1/

My problem is when exporting to excel, that the results bring me this way:

The result that I hope is this:

Answers

  • kthorngrenkthorngren Posts: 4,551Questions: 19Answers: 996

    Please don't duplicate your posts.

    This is not a simple thing to do with the Excel export. You will need to use the format object of the exportOptions as shown in this example.

    I have a discussion in this thread that shows how to replace newline characters to something that Excel will use to show line breaks. In your case you have <br> as your new line character. You will need to apply the same technique but to your footer instead of the body in my example.

    Kevin

  • EufragioEufragio Posts: 19Questions: 2Answers: 0

    Sorry, I still can not solve my problem.

    since the beginning was to export the group, since that is not possible. for less I want to export the results of those groupings

    You could make a demonstration of how it would be in my project: https://jsfiddle.net/Eufragio/u342qgoz/1/

  • EufragioEufragio Posts: 19Questions: 2Answers: 0

    @kthorngren Can you help me with this, since your result does not stay clear, the aim is to publicize the results of each group

    you think that it would be possible to export it in this way :

    or do you recommend to solve the problems

    Thank you for your time

  • kthorngrenkthorngren Posts: 4,551Questions: 19Answers: 996

    Like I said its not a simple solution. Did you try the code provided in the thread I linked to above?

    Kevin

  • EufragioEufragio Posts: 19Questions: 2Answers: 0
    edited January 14

    I understand that this is the code that would help me, but I do not understand how to attach it to my project

    exportOptions: {
        columns: [2, 3, 4, 5],
        format: {
            body: function ( data, row, column, node ) {
                   if (column === 3) {
                        //need to change double quotes to single
                        data = data.replace( /"/g, "'" );
                        //split at each new line
                        splitData = data.split('\n');
                        data = '';
                        for (i=0; i < splitData.length; i++) {
                            //add escaped double quotes around each line
                            data += '\"' + splitData[i] + '\"';
                            //if its not the last line add CHAR(13)
                            if (i + 1 < splitData.length) {
                                data += ', CHAR(13), ';
                            }
                        }
                        //Add concat function
                        data = 'CONCATENATE(' + data + ')';
                        return data;
                    }
                    return data;
                }
            }
        }
    },
    
  • kthorngrenkthorngren Posts: 4,551Questions: 19Answers: 996

    It took some time but I updated your example with the code for newlines in Excel:
    https://jsfiddle.net/m26nx9yg/

    For some reason char(13) didn't work so I changed to use char(10).

    When using footer: function ( data, row, column, node ) the column parameter is actually the node, not the column number. Make sense since this isn't a normal Datatables row. Also changed the if in line 5 to look for a class ('newline') assigned to the footer column to only format the desired columns.

    Changed the row selectors in the customize function to row:last to only affect the last row which is the footer. Also added the code to set the row height for the last row.

    This should get you started.

    Kevin

Sign In or Register to comment.