音尘啊 2019-10-11 11:36 采纳率: 0%
浏览 781

JS导出有背景色的表格Excel如何去掉背景色

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <a download="统计报表" href="#"  id="excelOut" class="easyui-linkbutton l-btn l-btn-small" data-options="iconCls:'icon-undo'">导出为 Excel 文档</a>
    <table cellspacing="0" cellpadding="0" border="1" id="tableToExcel">
        <thead>
            <tr>
                <td colspan="4" align="center">html 表格导出道Excel</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td style="background-color:rgb(217, 217, 217);">列标题1</td>
                <td style='background-color:rgb(255, 242, 204);'>列标题2</td>
                <td style='background-color:rgb(226, 239, 218);'>类标题3</td>
                <td style='background-color:rgb(217, 225, 242);'>列标题4</td>
            </tr>
            <tr>
                <td style="background-color:rgb(217, 217, 217);">aaa</td>
                <td style='background-color:rgb(255, 242, 204);'>bbb</td>
                <td style='background-color:rgb(226, 239, 218);'>ccc</td>
                <td style='background-color:rgb(217, 225, 242);'>ddd</td>
            </tr>
            <tr>
                <td style="background-color:rgb(217, 217, 217);">AAA</td>
                <td style='background-color:rgb(255, 242, 204);'>BBB</td>
                <td style='background-color:rgb(226, 239, 218);'>CCC</td>
                <td style='background-color:rgb(217, 225, 242);'>DDD</td>
            </tr>
            <tr>
                <td style="background-color:rgb(217, 217, 217);">FFF</td>
                <td style='background-color:rgb(255, 242, 204);'>GGG</td>
                <td style='background-color:rgb(226, 239, 218);'>HHH</td>
                <td style='background-color:rgb(217, 225, 242);'>III</td>
            </tr>
        </tbody>
    </table>
    <script>
        window.onload = function () {
            tableToExcel('tableToExcel', '下载模板')
        };
        //base64转码
        var base64 = function (s) {
            return window.btoa(unescape(encodeURIComponent(s)));
        };
        //替换table数据和worksheet名字
        var format = function (s, c) {
            return s.replace(/{(\w+)}/g,
                function (m, p) {
                    return c[p];
                });
        }
        function tableToExcel(tableid, sheetName) {
            var uri = 'data:application/vnd.ms-excel;base64,';
            var template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"' +
                'xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet>'
                + '<x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets>'
                + '</x:ExcelWorkbook></xml><![endif]-->' +
                ' <style type="text/css">' +
                'table td {' +
                'border: 1px solid #000000;' +
                'width: 200px;' +
                'height: 30px;' +
                ' text-align: center;' +
                'background-color: #FFFFFF;' +    
                'color: #000000;' +   
                ' }' +
                '</style>' +
                '</head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><body ><table class="excelTable">{table}</table></body></html>';
            if (!tableid.nodeType) tableid = document.getElementById(tableid);
            var ctx = { worksheet: sheetName || 'Worksheet', table: tableid.innerHTML };
            document.getElementById("excelOut").href = uri + base64(format(template, ctx));
        }
    </script>
</body>
</html>

这个能实现导出表格,但是导出的表格的背景色改不了,求问如何解决?

----------------------------------------------------2019.10.17
问题已解决:
首先感谢@i_laolarou 的帮助
只需要将 'background-color: #FFFFFF;' 改为 'background-color: #FFFFFF !important;' 即可以解决导出的表格自定义背景颜色不生效的问题。

  • 写回答

2条回答 默认 最新

  • 阿阿阿都 2019-10-11 11:59
    关注
     <tbody>
                <tr>
                    <td style="background-color:rgb(217, 217, 217);">列标题1</td>
                    <td style='background-color:rgb(255, 242, 204);'>列标题2</td>
                    <td style='background-color:rgb(226, 239, 218);'>类标题3</td>
                    <td style='background-color:rgb(217, 225, 242);'>列标题4</td>
                </tr>
                <tr>
                    <td style="background-color:rgb(217, 217, 217);">aaa</td>
                    <td style='background-color:rgb(255, 242, 204);'>bbb</td>
                    <td style='background-color:rgb(226, 239, 218);'>ccc</td>
                    <td style='background-color:rgb(217, 225, 242);'>ddd</td>
                </tr>
                <tr>
                    <td style="background-color:rgb(217, 217, 217);">AAA</td>
                    <td style='background-color:rgb(255, 242, 204);'>BBB</td>
                    <td style='background-color:rgb(226, 239, 218);'>CCC</td>
                    <td style='background-color:rgb(217, 225, 242);'>DDD</td>
                </tr>
                <tr>
                    <td style="background-color:rgb(217, 217, 217);">FFF</td>
                    <td style='background-color:rgb(255, 242, 204);'>GGG</td>
                    <td style='background-color:rgb(226, 239, 218);'>HHH</td>
                    <td style='background-color:rgb(217, 225, 242);'>III</td>
                </tr>
            </tbody>
    
                把上面的改成
                 <tbody>
            <tr>
                <td >列标题1</td>
                <td>列标题2</td>
                <td >类标题3</td>
                <td>列标题4</td>
            </tr>
            <tr>
                <td >aaa</td>
                <td>bbb</td>
                <td >ccc</td>
                <td>ddd</td>
            </tr>
            <tr>
                <td>AAA</td>
                <td>BBB</td>
                <td >CCC</td>
                <td >DDD</td>
            </tr>
            <tr>
                <td>FFF</td>
                <td>GGG</td>
                <td >HHH</td>
                <td >III</td>
            </tr>
        </tbody>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题