- 使用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(); }
- 下面是导出的表格的文本代码
<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'中的数据多复制几份 就可以正常打开了。