Quantcast
Channel: Recent Discussions — DataTables forums
Viewing all articles
Browse latest Browse all 82408

Export to excel with Custom styles causes excel to show error when opening

$
0
0

I am trying to add custom styles to my excel export but no matter what I do I get errors.
Following this post I tried to add a custom style like this:

 dataTableConfig.buttons = [
    {
        extend: 'excel',
        text: '<span class="glyphicon glyphicon-download-alt"></span> Excel',
        titleAttr: 'Excel',
        className: 'btn btn-default',
        exportOptions: {
            orthogonal: 'export',
            modifier: {
                page: 'current'
            },
            columns: columnsToExport,
        },
        customize: function (xlsx) {
            var sheet = xlsx.xl.worksheets['sheet1.xml'];
            var styles = xlsx.xl["styles.xml"];
            var fillsCount = styles.childNodes[0].childNodes[1].childNodes.length;
            var fills = styles.childNodes[0].childNodes[1];
            var xfs = styles.childNodes[0].childNodes[4];
            var newFill = '<fill><patternFill patternType="solid"><fgColor rgb="ffa779c3"/><bgColor indexed="64"/></patternFill></fill>';
            fills.innerHTML = fills.innerHTML + newFill;
            var newXf = '<xf borderId="0" fillId="' + fillsCount + '" fontId="1" numFmtId="0" applyBorder="1" applyFill="1" applyFont="1" xfId="0"/>';
            xfs.innerHTML = xfs.innerHTML + newXf;

            var xfsLength = xfs.childNodes.length;
            $(xfs).attr('count', xfsLength + 1);
            $('row c[r="A1"]', sheet).attr('s', xfsLength);
            $('row c[r="C3"]', sheet).attr('s', '42');
        }
    }
];

But that doesn't seem to work, I also tried manipulating the styles with xml nodes like this:

customize: function (xlsx) {
    var sheet = xlsx.xl.worksheets['sheet1.xml'];
    var styles = xlsx.xl["styles.xml"];
    var fillsCount = styles.childNodes[0].childNodes[1].childNodes.length;
    var fills = styles.childNodes[0].childNodes[1];
    var xfs = styles.childNodes[0].childNodes[4];
    var newFill = '<fill><patternFill patternType="solid"><fgColor rgb="ffa779c3"/><bgColor indexed="64"/></patternFill></fill>';
    var newFillXml = $($.parseXML(newFill)).find("fill");
    fills.appendChild(newFillXml[0]);
    $(fills).attr('count', fillsCount + 1);

    var newXf = '<xf borderId="0" fillId="' + fillsCount + '" fontId="1" numFmtId="0" applyBorder="1" applyFill="1" applyFont="1" xfId="0"/>';
    var newXfXml = $($.parseXML(newXf)).find("xf");
    xfs.appendChild(newXfXml[0]);

    var xfsLength = xfs.childNodes.length;
    $(xfs).attr('count', xfsLength + 1);
    $('row c[r="A1"]', sheet).attr('s', xfsLength);
    $('row c[r="C3"]', sheet).attr('s', '42');
}

No matter what I always get:
We found a problem with some content in 'File'. Do you want us to try to recover us much as we can? If you trust the source of this workbook, click Yes.
I don't understand what I am doing wrong? I updated by hand an excel file with this fill and it works fine. Then indexes were different but that's all.
Thanks!


Viewing all articles
Browse latest Browse all 82408


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>