zoukankan      html  css  js  c++  java
  • ASP.NET2.0导出Word文档(C#导出DOC)

    ASP.NET2.0导出Word文档(C#导出DOC)

     方式一:

    protected void applyLetter_Click(object sender, EventArgs e)
        {
            string strFileName = Server.MapPath("../InvitationLetterApplicationFormForVisa.doc");
            FileInfo DownloadFile = new FileInfo(strFileName);
            Response.Clear();
            Response.ClearHeaders();
            Response.Buffer = false;
            Response.ContentType = "application/octet-stream";
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8));
            Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
            Response.WriteFile(DownloadFile.FullName);
            Response.Flush();
            Response.End();
            string strWhere = "UserName='" + Session["UserNameEN"].ToString() + "'";
        }

    方式二:

    在网络上看到很多关于ASP.NET导出DOC文档的例子,有的干脆就直接将html页面不做任何处理直接导出为DOC文件,但是那样会有很多错误,例如将某些控件显示为图片。我还曾经见过微软为中国某个大公司制作的一个XX系统,导出的DOC文件实际上是某种特殊格式的XML,但是对于这个技术我还不是很了解。于是我在网络上收集资料,找到很多种实现方法,一一实验,最后总结出以下经验。

    一、首先配置系统环境:

    1、在命令行(work 的sdk命令行提示)中输入:dcomcnfg,会显示出“组件服务”管理器
    2、打开“组件服务-》计算机-》我的电脑-》DCOM 配置”,找到“Microsoft Word文档”,单击右键,选择“属性”
    3、在“属性”对话框中单击“安全”选项卡,在“启动和激活权限”处选择“自定义”,再单击右边的”编辑“,在弹出的对话框中添加”ASPNET
    “(在IIS6中是NETWORD SERVICE)用户,给予”本地启动“和”本地激活“的权限,单击”确定“,关闭”组件服务“管理器。
    这样就能在Asp.net页面中访问Word对象了。

    二、修改WEB.CONFIG,在<system.web>区段内加入:<identity impersonate="true"/>

    三、添加引用:《网站》——>《添加引用》——>《COM》——Microsoft Word 11.0 Object Library

    四、添加命名空间引用,
    using Microsoft.Office.Interop.Word;

    五:代码
    Object Nothing = System.Reflection.Missing.Value;
    //取得Word文件保存路径
    object filename = "c:\aa.doc";
    //创建一个名为WordApp的组件对象
    Microsoft.Office.Interop.Word.Application WordApp = new ApplicationClass();
    //网络上有写代码中直接使用Word.Application WordApp = new ApplicationClass(); 但我在实际编写中总是出现错误。
    //创建一个名为WordDoc的文档对象
    Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
    //增加一表格
    Microsoft.Office.Interop.Word.Table table = WordDoc.Tables.Add(WordApp.Selection.Range, 3, 3, ref Nothing, ref Nothing);
    //在表格第一单元格中添加自定义的文字内容
    table.Cell(1, 1).Range.Text = "lllll";
    //在文档空白地方添加文字内容
    WordDoc.Paragraphs.Last.Range.Text = "Wellcome To Aspxcn.Com";
    //将WordDoc文档对象的内容保存为DOC文档
    WordDoc.SaveAs(ref filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
    //关闭WordDoc文档对象
    WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
    //关闭WordApp组件对象
    WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);

  • 相关阅读:
    实战DeviceIoControl 之五:列举已安装的存储设备
    zend server 安装及相关配置
    php中echo 与print 的区别
    PHP:Deprecated: Function set_magic_quotes_runtime() is deprecated 错误
    php与ascii码
    计算EXCEL列名代号的程序
    笔记本电脑键盘不能正常输入字符
    读写注册表
    创建用于监视对student表进行插入和更新操作的触发器
    在客户机上安装Windows服务
  • 原文地址:https://www.cnblogs.com/sgivee/p/1806881.html
Copyright © 2011-2022 走看看