To prevent Datatables from removing the first zeros on Excel export, you can use the customizeData option in the excelHtml5 button configuration. This option allows you to modify the data before it is exported to Excel. You can use a regular expression to check if the data starts with a zero and add an apostrophe before it to force Excel to treat it as text. Here's an example:
$('#example').DataTable( {
buttons: [
{
extend: 'excelHtml5',
customizeData: function(data) {
for (var i = 0; i < data.body.length; i++) {
for (var j = 0; j < data.body[i].length; j++) {
var cell = data.body[i][j];
if (/^0/.test(cell)) {
data.body[i][j] = "'" + cell;
}
}
}
}
}
]
} );
This code checks each cell in the table data and adds an apostrophe before it if it starts with a zero. This will force Excel to treat the cell as text and preserve the leading zeros.