2

I have a datatable in my application that I need to export to Excel but when the datatable is exported in excel, the format type for each cell is 'General'. However, I want all my cells to be displayed in 'Text' format in Excel.

I tried converting my data to String but it still displays the result in General. If I try to convert my data to Int then it does displays it in Number format in Excel but I want everything in Text format.

Here's the function I'm trying to change the data type:

{
   "targets": 13,
   "render": function (data, type, row){
       var d = data.toString();
       return d;
       }
}

Here I'm targeting to one column only. However, I want to target all my columns. Even targeting a single column won't work.

Any help would be appreciated.

1
  • I would appreciate you reviewing my answer to your question and if I was able to help you, marking answer as accepted by clicking on v under the answer's score. Thank you Apr 14, 2019 at 19:28

1 Answer 1

4

You could apply the following code to accomplish your task:

$('#example').DataTable( {
    dom: 'Bfrtip',
    columns: [
        { data: 'name' },
        { data: 'surname' },
        { data: 'position' },
        { data: 'office' },
        { data: 'salary' }
    ],
    buttons: [
        {
            extend: 'excelHtml5',
            customize: function(xlsx) {
                //Get the built-in styles
                //refer buttons.html5.js "xl/styles.xml" for the XML structure
                var styles = xlsx.xl['styles.xml'];

                //Create our own style to use the "Text" number format with id: 49
                var style = '<xf numFmtId="49" fontId="0" fillId="0" borderId="0" xfId="0" applyFont="1" applyFill="1" applyBorder="1" applyNumberFormat="1"/>';
                // Add new node and update counter
                el = $('cellXfs', styles);
                el.append(style).attr('count', parseInt(el.attr('count'))+1);
                // Index of our new style
                var styleIdx = $('xf', el).length - 1;

                //Apply new style to the first (A) column
                var sheet = xlsx.xl.worksheets['sheet1.xml'];
                //Set new style default for the column (optional)
                $('col:eq(0)', sheet).attr('style', styleIdx);
                //Apply new style to the existing rows of the first column ('A')
                //Skipping the header row
                $('row:gt(0) c[r^="A"]', sheet).attr('s', styleIdx);
            },
            exportOptions: {
                format: {
                    body: function(data, row, column, node) {
                        return column === 0 ? "\0" + data : data;
                    }
                }
            },
        },
    ]
} );

Here is JSFiddle

In this example we work with column 0 ('A').
For your column 13, use this code:

                //Set new style default for the column (optional)
                $('col:eq(13)', sheet).attr('style', styleIdx);
                //Apply new style to the existing rows of the 13th column ('N')
                //Skipping the header row
                $('row:gt(0) c[r^="N"]', sheet).attr('s', styleIdx);

Update Apr 4th, 2019

Added an additional trick in the code above to ensure very large numbers are still being treated as text:

exportOptions: {
    format: {
        body: function(data, row, column, node) {
            return column === 0 ? "\0" + data : data;
        }
    }
},

JSFiddle was updated

3
  • numFmtId="49" is a predefined ID for the Text cell format in Excel. You could get it by unzipping a test XLSX file and analyzing xl\styles.xml and xl\worksheets\sheet1.xml Aug 27, 2018 at 21:32
  • Came across this trying to find a solution for USPS Tracking Numbers - i.e. "92001902416755000004290987". This script doesn't work for this format. While it styles the cells as Number Format "Text", the values are still showing up incorrectly. For example, the above value ends up as "9.2001902416755E+25". I'm dying to find a way to make this work. Apr 4, 2019 at 23:07
  • Update Apr 4th, 2019 worked for me for data like "5210035002202001294983"
    – Mr.No
    May 18, 2021 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.