Excel / Copy buttons - Keep formatting in some cells

Excel / Copy buttons - Keep formatting in some cells

guidolsguidols Posts: 38Questions: 14Answers: 1
edited August 2022 in Buttons

Hi,
I'm using the Excel / Copy buttons, which are working fine for most situations.

However I have a situation where a particular column contains html data, which is then rendered, please see these simple cells:

If I export that table, the Excel (or copy) button will create a cell containing:

line1line2line3

Is ok that only a cell is created, but how can I tell the button to insert in the cell the new line break character?

This question has an accepted answers - jump to answer

Answers

  • rf1234rf1234 Posts: 2,991Questions: 87Answers: 421

    This thread might help you out.
    https://datatables.net/forums/discussion/comment/167672/
    Warning: Customizing the client side Excel export can be very cumbersome and complex! Data tables does have some styles built in but you may end up having to create your own, for example

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    The export gets it's data from the buttons.exportData() method. Which by default will strip HTML and new lines.

    What you want to do is set the stripNewlines option to false for the exportOptions object in your export button:

    {
      extend: 'copy',
      exportOptions: {
        stripNewlines: false
      }
    },
    

    Allan

  • guidolsguidols Posts: 38Questions: 14Answers: 1
    edited August 2022

    I added, in my html data, the character that Excel uses to add a new line in a cell, i.e. char(10).

    But there is still a problem: the user creates the Excel using the buttons and, after opening it, he needs to press the "wrap text" button in the column containing the wrapped data:

    Without doing so, the data is not wrapped.

    Is there a way to create the Excel and "forcing" wrapping data?

    Please see this similar problem and solution (non related to DataTables):

    "After you supply text you should set the cell's IsTextWrapped style to true".

    Thanks!

  • kthorngrenkthorngren Posts: 21,336Questions: 26Answers: 4,951
    edited August 2022 Answer ✓

    This thread discusses one option.

    Kevin

  • guidolsguidols Posts: 38Questions: 14Answers: 1

    Thanks Kevin!

    This will do the trick (where "F" is the desired column):

    $('row c[r^="F"]', sheet).attr('s', '55');

Sign In or Register to comment.