You can use the build in styles. for example, 42 is Bold, green background, thin black border.
$('row:first c', sheet).attr( 's', '42');
The list of all the build in styles are here:
https://datatables.net/reference/button/excelHtml5#Built-in-styles
Answer from Candice on Stack OverflowYou can use the build in styles. for example, 42 is Bold, green background, thin black border.
$('row:first c', sheet).attr( 's', '42');
The list of all the build in styles are here:
https://datatables.net/reference/button/excelHtml5#Built-in-styles
I had to create my own custom styles by scoping out the datatables.js file export button file in my project.
Here's my script:
buttons: [{
//"colvis",
extend: 'excelHtml5',
text: 'Export to Excel',
autoFilter: true,
title: '',
customize: function (xlsx) {
var styles = xlsx.xl['styles.xml'];
//custom font
f1 = '<font><sz val="11" /><name val="Calibri" /><color rgb="FFFFFFFF" /><b />
</font>';
//custom colors
//green 67
s1 = '<fill><patternFill patternType="solid"><fgColor rgb="C6E0B4" /><bgColor indexed="64" /></patternFill></fill>';
s2 = '<xf numFmtId="168" fontId="0" fillId="6" borderId="1" applyFont="1" applyFill="1" applyBorder="1" xfId="0" applyAlignment="1"><alignment horizontal="center"/></xf>';
styles.childNodes[0].childNodes[2].innerHTML = styles.childNodes[0].childNodes[2].innerHTML + s1;
styles.childNodes[0].childNodes[5].innerHTML = styles.childNodes[0].childNodes[5].innerHTML + s2;
$('row c[r^="Z"]', sheet).attr('s', '67');
}]
Another reference can be found at this site: https://datatables.net/forums/discussion/45846/datatable-excel-export-how-can-we-apply-multiple-styles-to-same-cell
css - DataTables Excel export with styles - Stack Overflow
Datatables export Excel - Stack Overflow
Modifying datatable function to use export to excel functionality โ DataTables forums
Table cells format when exporting jQuery Datatable to Excel
Videos
I think the best thing to do is to remove the paging, then do the export, then turn the paging back on once it's done.
I made a couple minor changes:
$(function ()
{
var table = $('#example').DataTable();
$("#btnExport").click(function(e)
{
table.page.len( -1 ).draw();
window.open('data:application/vnd.ms-excel,' +
encodeURIComponent($('#example').parent().html()));
setTimeout(function(){
table.page.len(10).draw();
}, 1000)
});
});
Updated fiddle: http://jsfiddle.net/jzdjdo3z/176/
Page Length docs: https://datatables.net/reference/api/page.len()
Paging option docs: https://datatables.net/reference/option/paging
I'm not sure why initializing with dataTables vs DataTables made a difference, but it did. So keep an eye out for that.
If you want download as .xlsx format, use following code. Here you can define the name of the file also. Scripts:
<script src="https://unpkg.com/xlsx/dist/shim.min.js"></script>
<script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"></script>
<script src="https://unpkg.com/[email protected]/Blob.js"></script>
<script src="https://unpkg.com/[email protected]/FileSaver.js"></script>
Javacript:
var table = $('#example').DataTable();
$("#btnExport").click(function(e)
{
var fileName = "test";
var fileType = "xlsx";
var table_obj = document.getElementById("example");
var wb = XLSX.utils.table_to_book(table_obj, {sheet: "Sheet JS"});
return XLSX.writeFile(wb, null || fileName + "." + (fileType || "xlsx"));
});
But issue is, you can download first page only. If any one found to full table, update here. Thanks in Advance.
Use "createEmptyCells" : true,
Example { extend: 'excelHtml5', text: 'Export to Excel', "createEmptyCells" : true, exportOptions: { title:null, orthogonal: 'export' } },
To style the third cell for each row excluding the header, you need this selector:
$('row[r!=1] c:nth-child(3)', sheet).attr('s', '20');
The styling will apply even the cell is empty. Check this example I created.
ยป npm install datatables-buttons-excel-styles



