zoukankan      html  css  js  c++  java
  • <转>HTML中的table转为excel

    转换html 中的table 为excel,firefox浏览器支持,代码如下

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    <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('testTable', 'W3C Example Table')" value="Export to Excel">
    
    <h4>两行三列:</h4>
    <table id = 'testTable' border="1">
    <tr>
      <td>100</td>
      <td>200</td>
      <td>300</td>
    </tr>
    <tr>
      <td>400</td>
      <td>500</td>
      <td>600</td>
    </tr>
    </table>
    
    </body>
    </html>
  • 相关阅读:
    django中的自定义标签与过滤器,静态文件配置,orm前戏
    JavaScript(js)运算符
    JavaScript(js)字面量,函数写法
    JavaScript(js)的4中输出方式
    JavaScript(js)的学习使用样式,核心语法,数据类型
    标签链接、表单及css部分知识
    学习html5 附代码
    automationOperationsWithPython
    0821 1336 模块与包的导入方法、常用模块介绍
    0816 1459 json & pickle ,目录导入,目录规范
  • 原文地址:https://www.cnblogs.com/baron89/p/3369030.html
Copyright © 2011-2022 走看看