使用单元格颜色导出jQuery数据表数据

时间:2015-06-22 09:02:23

标签: jquery excel pdf datatables tabletools

我正在使用不同格式的TableTools从jQuery数据表导出数据,如excel,pdf。某些单元格在数据表中具有不同的背景颜色。但是这些颜色不会在导出的excel或pdf中显示。谁能告诉我,如何解决这个问题?

以下是数据表的代码:

var wdm_without_groupcode_attendee_table = jQuery("#wdm_without_groupcode_attendee_table").DataTable({
    "dom": 'CT<"clear">lfrtip',
            "tableTools": {
                        "sSwfPath": wdm_new_reports_localize_data.wdm_swf_path , //Getting path from localize script
                        "aButtons": [ 
                                    {
                                        "sExtends": "copy",         
                                        "oSelectorOpts": { filter: "applied", order: "current" }
                                    },
                                    {
                                        'sExtends': 'xls',         
                                        "oSelectorOpts": { filter: 'applied', order: 'current' }
                                    },
                                    {
                                        'sExtends': 'print',               
                                        "oSelectorOpts": { filter: 'applied', order: 'current' }
                                    },
                                    {
                                        'sExtends': 'pdf',               
                                        "oSelectorOpts": { filter: 'applied', order: 'current' }
                                    },
                                    {
                                        'sExtends': 'csv',     
                                        "oSelectorOpts": { filter: 'applied', order: 'current' }
                                    }]
                        },
            "aoColumns": [
                    {sClass: "alignLeft"},
                    {sClass: "alignLeft"},
                    {sClass: "alignCenter"},
                    {sClass: "alignCenter"},
                    {sClass: "alignCenter"},
                    {sClass: "alignCenter"},
                    {sClass: "alignCenter"},
                    {sClass: "alignCenter"},
                    {sClass: "alignLeft"},
                    {sClass: "alignCenter"},
                    {sClass: "alignCenter"}
                    ],
            "createdRow": function ( row, data, index ) {


                jQuery('td',row).eq(4).addClass('DARK_BLUE');
                jQuery('td',row).eq(5).addClass('ORANGE');
                jQuery('td',row).eq(6).addClass('RED');

                //to decide color of cells
                if ( data[2].toLowerCase().indexOf("yes") >= 0 ) {
                    jQuery('td', row).eq(2).addClass('SKY_BLUE');
                }
                if ( data[3] >= 1 ) {
                    jQuery('td', row).eq(3).addClass('DARK_BLUE');
                }
                if ( data[7].toLowerCase().indexOf("yes") >= 0 ) {
                    jQuery('td', row).eq(7).addClass('DARK_BLUE');
                }
            }
});

2 个答案:

答案 0 :(得分:0)

我不确定,但看起来你在完全创建TDs后对TR进行着色 所以请尝试使用此createdCell

"aoColumns": [
    {sClass: "alignLeft"},
    {sClass: "alignLeft", createdCell: function (td, cellData, rowData, row, col) {
        if (cellData >= 1 )
            $(td).addClass('DARK_BLUE');
    }},
    {sClass: "alignCenter", createdCell: function (td, cellData, rowData, row, col) {
        if (cellData.toLowerCase().indexOf("yes") >= 0 )
            $(td).addClass('SKY_BLUE');
    }},
    {sClass: "alignCenter DARK_BLUE"},
    {sClass: "alignCenter ORANGE"},
    {sClass: "alignCenter RED"},
    {sClass: "alignCenter", createdCell: function (td, cellData, rowData, row, col) {
        if (cellData >= 1 )
            $(td).addClass('DARK_BLUE');
    }},
    {sClass: "alignCenter"},
    {sClass: "alignLeft"},
    {sClass: "alignCenter"},
    {sClass: "alignCenter"}
],

答案 1 :(得分:0)

要在导出的文件中应用单元格颜色,您需要在自定义方法中添加着色逻辑。

customize: function(xlsx) {
    var sheet = xlsx.xl.worksheets['sheet1.xml'];
    //color headers
    $('row:first c', sheet).attr('s', '42');
}

Adding header cell color