持续充电 2019-09-23 14:39 采纳率: 0%
浏览 263

求大佬帮忙看看,表格导出excel问题。

  1. 使用js导出excel时 如果页面的表格中数据较少时 excel会打不开。 下面是导出表格的js //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, fileName) { var table; 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 #d6d6d6;' + 'width: 200px;' + 'height: 30px;' + ' text-align: center;' + 'background-color: #ffffff;' + 'color: #000;' + ' }' + '</style>' + '</head><body ><table class="excelTable">{table}</table></body></html>'; if (!tableid.nodeType) { table = $("#" + tableid).clone(); table.find("th").each(function(){ if($(this).css("display")=="none"){ $(this).remove(); } }); table.find("td").each(function(){ if($(this).css("display")=="none"){ $(this).remove(); } }); } var ctx = { worksheet : sheetName || 'Worksheet', table : table[0].innerHTML }; document.getElementById("excelOut").href = uri + base64(format(template, ctx)); document.getElementById("excelOut").download = fileName; document.getElementById("excelOut").click(); }
  2. 下面是导出的表格的文本代码
<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>xxxx</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 #d6d6d6;width: 200px;height: 30px; text-align: center;background-color: #ffffff;color: #000; }</style></head><body ><table class="excelTable">
                <thead>
                <tr class="content_grid_title">          
                    <th height="40px;" style="font-size:14px;color:#404040;border:1px solid #ccc">柜面名称</th>               
                    <th style="font-size:14px;color:#404040; border:1px solid #ccc">回路名称</th>
                    <th style="font-size:14px;color:#404040;border:1px solid #ccc">相电压越上限</th>
                    <th style="font-size:14px;color:#404040;border:1px solid #ccc">相电压越下下限</th>
                    <th style="font-size:14px;color:#404040;border:1px solid #ccc">汇总</th></tr>
                <tr class="content_grid_title"></tr>
                </thead>
                <tbody><tr><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">AA1-AA4</td><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">3AA-1</td><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">1</td><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">0</td><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">1</td></tr><tr><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">3AA</td><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">3AA-4</td><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">2</td><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">1</td><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">3</td></tr><tr><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">AA8-AA12</td><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">11AA-1</td><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">0</td><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">1</td><td style="font-size:14px;color:#404040 ;border:1px solid #ccc;height:40px">1</td></tr></tbody>
            </table></body></html>

此时excel不能正常打开 当tbody'中的数据多复制几份 就可以正常打开了。

  • 写回答

1条回答 默认 最新

  • 关注
    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗