ag-grid export removes leading zeros

47 Views Asked by At

following is the function i have used for exporting file: excelStyles: [ { id: 'header', interior: { color: '#aaaaaa', pattern: 'Solid', }, }, { id: 'body', dataType : 'string', interior: { color: '#dddddd', pattern: 'Solid', }, }, ]

function exportToExcel (){ var sheetNameVal = "Btest";

    var params = {
            skipPinnedTop : false,
            sheetName :      sheetNameVal
    }
    params.processCellCallback = function(params) {
        var val = params.value ;
        if(/^\d+$/.test(val))
            val = " "+val;
        else if(params.column.colDef.dataType=="Date/Time"){
            if(val)
                val = val.length>10 ? val.substring(0,10) : val;
        }else if(!_.isEmpty(params.column.colDef.refData)){
            val = params.column.colDef.refData[val];
        }
        if((params.column.colDef.field).includes("_TRX_AMT")){
            var trxCurr = params.node.data.TRX_CURRENCY ;
            val = FormatCurrency2(val,trxCurr);
        }
        return val;
    };
    gridOptions.api.exportDataAsExcel(params);
}

now if suppose one of my cell has value = '000000180' when i export it to excel the value is changed to 180. i have tried converting it to string using val = " "+val but somehow it didn't work.

2

There are 2 best solutions below

0
Alexander Zbinden On

You can set the type of the excelStyle to String:

https://www.ag-grid.com/angular-data-grid/excel-export-data-types/

As an alternative, instead of adding a " ", you can add "'" in front of your value.

The character ' will not be visible in Excel.

0
ViqMontana On

Process the cell as a string, that way it won't get converted to a number which would lead to the leading 0's being removed.

You've got the right idea in trying val = " "+val but I what I think you need to do is:

val = "'" + val

to actuall convert it to a string.

Demo.