zoukankan      html  css  js  c++  java
  • 将html导入到excel或word itprobie

    1>通过js实现,在页面的前台调用微软office的com组件,要求用户必须安装office,启动automation服务,并且在浏览器中启用未注册的activex选项.

    function importToExcel(tableid) {
        var curTbl = document.getElementById(tableid);
        try {
            var oXL = new ActiveXObject("Excel.Application");
        }
        catch (e) {
            alert("请安装微软Excel并且在浏览器的安全级别中启用‘对未标记为安全执行脚本的ActiveX控件初始化并执行脚本’的安全选项");
            return;
        }
        var oWB = oXL.Workbooks.Add();
        var oSheet = oWB.ActiveSheet;
    
        var sel = document.body.createTextRange();
        sel.moveToElementText(curTbl);
        sel.select();
        sel.execCommand("Copy");
    
        oSheet.Paste();
        oXL.Visible = true;
    }
    
    function importToWord(tableid) {
        var curTbl = document.getElementById(tableid);
        try {
            var oWD = new ActiveXObject("Word.Application");
        } catch (e) {
            alert("请安装微软Word并且在浏览器的安全级别中启用‘对未标记为安全执行脚本的ActiveX控件初始化并执行脚本’的安全选项");
            return;
        }
        var oDC = oWD.Documents.Add("", 0, 1);
        var oRange = oDC.Range(0, 1);
    
        var sel = document.body.createTextRange();
        sel.moveToElementText(curTbl);
        sel.select();
        sel.execCommand("Copy");
    
        oRange.Paste();
        oWD.Application.Visible = true;
    }  

    2>在服务器端实现,服务器端需要安装office,客户端不需要安装office,将需要保存的文本,以流的形式输出到页面,然后保存为指定的文件类型。输出的时候必须加上<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=gb2312\"/>,否则会乱码

            /// <summary>
            /// 将html导出Excel
            /// </summary>
            /// <param name="content">html内容</param>
            /// <param name="FileName">导出时的默认文件名</param>
            public void ExportToExcel(string content, string FileName)
            {
    

    _context.Response.AppendHeader("Content-Disposition",
    string.Format("attachment;filename={0}", HttpUtility.UrlPathEncode(filename)));
    _context.Response.ContentType = string.Format("application/vnd.ms-{0}", type);
    _context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    _context.Response.Charset = "gb2312";

    _context.Response.Write(string.Format("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=gb2312\"/>{0}", 

    Microsoft.JScript.GlobalObject.unescape(_context.Request["data"])));
    _context.Response.End();

            }
  • 相关阅读:
    vb 动态添加 timer
    sruts2 国际化 规范
    vb 创建xml
    robots
    android 桌面斗地主
    QT 平台
    云软件 Eucalyptus云
    android intentfilter
    android 布局相关
    Zend Studio PHP
  • 原文地址:https://www.cnblogs.com/guohu/p/2678813.html
Copyright © 2011-2022 走看看