Formating data when exporting to excel returns data with html code

Formating data when exporting to excel returns data with html code

bmacuerbmacuer Posts: 2Questions: 1Answers: 0

I have a table which I couldnt export to excel with formatted numbers, because they get messed up on excel. What I did, was to clean the number so it just returned a number withouth symbol, commas, or dots. This fix the issue now I can modified the numbers and apply operations to them in excel. The problem now its on the other columns it showing full html instead of just the text. I made a code to clean html tags on the data, but it still showing some html entities, I just want it to show clear text.

        {
        extend: 'excel',
        exportOptions: {
            columns: ':visible',
            format: {
                body: function(data, row, column, node) {               
                       return column >= 7 && column <= 9 ? data.replace( /[$,.]/g, '' ) : data;

                }
            }
        }

Here is a picture showing html code on a column
https://i.stack.imgur.com/Gw8s3.png

When I apply strip tags code, the html tags disappears but I still have html code left. I wonder if there is a function to just display data text, since without formatting, it just display text normally without any html.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,117Questions: 26Answers: 4,916
    Answer ✓

    By default the Excel export will strip HTML tags. My understanding is that this not the case when using format. AFAIK this is still the case with the latest versions of buttons. You can try adding another regex to strip the HTML. Maybe this example will work for you:

    return column >= 7 && column <= 9 ? data.replace( /[$,.]/g, '' ) : data.replace(/(&nbsp;|<([^>]+)>)/ig, "");
    

    This regex seems to work with the couple examples I tried. You may need to modify it to fit your needs.

    Kevin

  • bmacuerbmacuer Posts: 2Questions: 1Answers: 0

    Thanks Kevin, it works perfectly!!

This discussion has been closed.