js导出gridview到excel

本文介绍了一种将HTML表格转换为Excel文件的方法。通过JavaScript实现,支持导出带有样式的表格,并提供了示例代码和使用效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<html>
<head>
<script type="text/javascript">
    var tableToExcel = (function() {
        var uri = 'data:application/vnd.ms-excel;base64,'
    , 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]--></head><body><table>{table}</table></body></html>'
    , base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) }
        return function(table, name) {
            if (!table.nodeType) table = document.getElementById(table)
            var ctx = { worksheet: name || 'Worksheet', table: table.innerHTML }
            window.location.href = uri + base64(format(template, ctx))
        }
    })()
</script>
</head>
<body>
<h1>tableToExcel Demo</h1>
<p>Exporting the W3C Example Table</p>


<input type="button" onclick="tableToExcel('GridViewBoiler', 'W3C Example Table')" value="Export to Excel">


<table cellspacing="0" cellpadding="4" rules="all" border="1" id="Table1" style="color:#333333;width:100%;border-collapse:collapse;">
<tbody><tr style="color:White;background-color:#507CD1;font-weight:bold;">
<th scope="col">序号</th><th scope="col">参数描述</th><th scope="col">--望亭发电有限公司--#11机组--锅炉本体试验--2015-02-09 14:40:53--</th>
</tr><tr style="background-color:#EFF3FB;">
<td align="center">1</td><td align="left">入炉煤收到基低位发热量(KJ/Kg)</td><td>19600.0000</td>
</tr><tr style="background-color:White;">
<td align="center">2</td><td align="left">入炉煤收到基灰分(%)</td><td>23.6000</td>
</tr><tr style="background-color:#EFF3FB;">
<td align="center">3</td><td align="left">入炉煤收到基水分(%)</td><td>4.1800</td>
</tr><tr style="background-color:White;">
<td align="center">4</td><td align="left">入炉煤收到基碳含量(%)</td><td>57.4900</td>
</tr><tr style="background-color:#EFF3FB;">
<td align="center">5</td><td align="left">入炉煤收到基氢含量(%)</td><td>3.6500</td>
</tr><tr style="background-color:White;">
<td align="center">6</td><td align="left">入炉煤收到基氧含量(%)</td><td>9.9500</td>
</tr><tr style="background-color:White;">
<td align="center">20</td><td align="left">换算至设计送风温度时的排烟温度(%)</td><td>122.0239</td>
</tr><tr style="background-color:#EFF3FB;">
<td align="center">21</td><td align="left">修正后锅炉热效率(%)</td><td>91.2669</td>
</tr>
</tbody></table>


</body>

</html>



效果图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值