hhurz / tableExport.jquery.plugin

jQuery plugin to export a html table to JSON, XML, CSV, TSV, TXT, SQL, Word, Excel, PNG and PDF
MIT License
990 stars 716 forks source link

xlsx export: no decimal for integer columns #356

Closed astomas closed 1 year ago

astomas commented 1 year ago

After reading #352, I see you can remove decimals by adjusting "numbers" to 1 in tableExport.js , but il will work for all numeric columns (integer or not).

So how to remove, when exporting in xslx, the 2 decimals from integer columns only and keep the decimals for other numeric columns?

hhurz commented 1 year ago

There are two solutions

1st solution: add a xlsx format specifier to the regarding html table cells depending on their value types, like

      <tr>
        <td data-tableexport-xlsxformatid="1">3709</td>
      </tr>
      <tr>
        <td data-tableexport-xlsxformatid="2">10.25</td>
      </tr>

2nd solution: Apply a callback function to the mso.xlsx.formatId.numbers option:

$table.tableExport({
    type: "excel",
    mso: {
        fileFormat: 'xlsx',
        xlsx: {
            formatId: {
                numbers: onXlsxFormatId
            }
        }
    },
});
function onXlsxFormatId(cell, row, col){
    let formatId = 2;
    if (row > 0 && (col === 0 || col === 3)) {
        formatId = 1;  // Apply formatId 1 to the 1st and 4th column
    }
    return formatId;
}
astomas commented 1 year ago

1st solution works, thanks. You have to know beforehand which columns are integer only.