Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting to excel with proper cell formatting on ag-grid

When I export a table to excel using ag-grid's own exportDataAsExcel() the resulting excel contains dates as General data type instead of Date.

[excel pic]

I have used this:

exportDataAsExcel({
   processCellCallback: ({col, val}) => {( /*date value formatting here*/ )}
})

to format both Date, string with proper date formatting (DD/MM/YYYY) but I can't make excel properly recognize these cells as Date instead of General

This is reproducible with the excel export examples on their website: https://www.ag-grid.com/javascript-grid-excel/?framework=all#gsc.tab=0

like image 426
Albert Vàzquez Avatar asked Dec 10 '22 10:12

Albert Vàzquez


1 Answers

I've used this code to apply value formatter in excell as well:

this.gridOptions.api.exportDataAsExcel({
  processCellCallback: (params) => {
    const colDef = params.column.getColDef()
    // try to reuse valueFormatter from the colDef
    if (colDef.valueFormatter) {
      const valueFormatterParams: ValueFormatterParams = {
        ...params,
        data: params.node.data,
        node: params.node!,
        colDef: params.column.getColDef()
      };
      return colDef.valueFormatter(valueFormatterParams);
    }
    return params.value;
  },
});
like image 117
Liero Avatar answered Dec 28 '22 06:12

Liero