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>
  • 相关阅读:
    第3章 敏捷项目管理概述
    第2章 传统与敏捷方法论
    第1章 敏捷思维—“互联网+”知识工作者必备的DNA
    敏捷项目管理架构(APMF)
    敏捷宣言和准则
    研发工程师如何转型项目经理
    软件门外汉的入门进阶
    [摘录]第五部分 经验谈(2)
    [摘录]第五部分 经验谈(1)
    [摘录]第四部分 教训篇(2)
  • 原文地址:https://www.cnblogs.com/baron89/p/3369030.html
Copyright © 2011-2022 走看看