• js 导出到word,excel


    1>js导出,原理:复制页面中的table(也可以是其他内容)然后粘贴到excle中

    
    

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>导出表格</title>
    </head>
    <body>
    <input type="button" value="导出Word" onclick="JavaScript:exportToWord('tableId')" />
    <input type="button" value="导出Excel" onclick="JavaScript:exportToExcel('tableId')" />
    <table id="tableId">
    <thead>
    <tr>
    <th>序列</th>
    <th>名字</th>
    <th>年龄</th>
    <th>性别</th>
    </tr>
    </thead>
    <tbody>
    <tr>
    <td>01</td>
    <td>张三</td>
    <td>18</td>
    <td>女</td>
    </tr>
    <tr>
    <td>02</td>
    <td>李四</td>
    <td>20</td>
    <td>女</td>
    </tr>
    <tr>
    <td>03</td>
    <td>王五</td>
    <td>22</td>
    <td>男</td>
    </tr>
    <tr>
    <td>04</td>
    <td>张飞</td>
    <td>100</td>
    <td>男</td>
    </tr>

    
    

    </tbody>
    </table>
    </body>
    <script type="text/javascript">
    function exportToWord(id) {
    //Scripting.FileSystemObject (FSO 文本文件读写)被关闭了,
    //开启FSO功能即可,在“运行”中执行regsvr32 scrrun.dll即可
    try {
    var oElement = document.getElementById(id);
    var word = new ActiveXObject("Word.Application");
    var doc = word.Documents.Add("", 0, 1); //不打开模版直接加入内容
    var Range = doc.Range();

    
    

    var sel = document.body.createTextRange();
    sel.moveToElementText(oElement);
    sel.select();
    sel.execCommand("Copy");
    Range.Paste();
    word.Application.Visible = true;
    }
    catch (e) {
    alert("无法启动Excel! " + e.message +
    " 如果您确信您的电脑中已经安装了Excel," +
    "那么请调整IE的安全级别。 具体操作: " +
    "工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用");
    }
    }
    function exportToExcel(id) {
    //Scripting.FileSystemObject (FSO 文本文件读写)被关闭了,
    //开启FSO功能即可,在“运行”中执行regsvr32 scrrun.dll即可
    try {
    var oElement = document.getElementById(id);
    var oRangeRef = document.body.createTextRange();
    oRangeRef.moveToElementText(oElement);
    oRangeRef.execCommand("Copy");

    
    

    var oXL = new ActiveXObject("Excel.Application")
    var oWB = oXL.Workbooks.Add;
    var oSheet = oWB.ActiveSheet;
    oSheet.Paste();
    oSheet.Cells.NumberFormatLocal = "@";
    oXL.Selection.ColumnWidth = 8;

    
    

    oXL.Visible = true;
    oSheet = null;
    oWB = null;
    appExcel = null;
    } catch (e) {
    alert("无法启动Excel! " + e.message +
    " 如果您确信您的电脑中已经安装了Excel," +
    "那么请调整IE的安全级别。 具体操作: " +
    "工具 → Internet选项 → 安全 → 自定义级别 → 对没有标记为安全的ActiveX进行初始化和脚本运行 → 启用");
    }
    }
    </script>
    </html>

  • 相关阅读:
    idou老师教你学Istio 19 : Istio 流量治理功能原理与实战
    面对runc逃逸漏洞,华为云容器为您保驾护航
    idou老师教你学Istio 18 : 如何用istio实现应用的灰度发布
    idou老师教你学Istio 17 : 通过HTTPS进行双向TLS传输
    idou老师教你学Istio 16:如何用 Istio 实现微服务间的访问控制
    idou老师教你学Istio 15:Istio实现双向TLS的迁移
    极简容器化交付 | 部署组件分析
    idou老师教你学Istio 14:如何用K8S对Istio Service进行流量健康检查
    Hibernate5笔记9--Hibernate注解式开发
    Hibernate5笔记8--Hibernate事务相关内容
  • 原文地址:https://www.cnblogs.com/zhj-Acmen/p/7010335.html
Copyright © 2020-2023  润新知