JS导出gridview到excel

<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>

 

posted @ 2015-12-18 15:36  bobo-bobo  阅读(473)  评论(0编辑  收藏  举报