Excel export and newlines replaced by a lot of spaces

Excel export and newlines replaced by a lot of spaces

itajackassitajackass Posts: 162Questions: 48Answers: 3

Hi I just create a test case here:

https://live.datatables.net/luzonuli/2/edit

The problem is that during export, the newlines found are replaced with leading spaces. I'd like to export removing these spaces? any solution?

Answers

  • kthorngrenkthorngren Posts: 21,299Questions: 26Answers: 4,945
    edited October 2023

    I suggest that you do some debugging of the format function to see the original data from the cell and the result of the regex statement. One way is with console.log statements like this:
    https://live.datatables.net/luzonuli/3/edit

    Note that I changed the regex replace line to reassign the result to the data variable. The result of the statement doesn't update the variable without reassigning it.

    The white space you see in the Excel file is found in the table data. You have this for example:

            <tbody>
              <tr>
                <td>Tiger Nixon<br>Second line not correct - it is glued to first line</td>
                <td>System Architect</td>
              </tr>
              <tr>
                <td>
                Garrett Winters
                Second line recognized but with leading spaces
                </td>
                <td>Director</td>
              </tr>
    

    There are leading spaces in the second line (line 9). HTML does not display multiple consecutive spaces to save space on the page which is why they don't show in the table. You will need to remove the consecutive spaces or a better option would be to use a <br> in the cell, like this:

                <td>
                Garrett Winters<br>Second line recognized but with leading spaces
                </td>
    

    Note that Excel doesn't use \r\n for new lines within the cell. This thread shows how I have handled new lines with Excel.

    Kevin

  • itajackassitajackass Posts: 162Questions: 48Answers: 3

    Ok i've found this workaround:

    https://live.datatables.net/gawusuwe/1/edit

Sign In or Register to comment.