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

    在网络上看到很多关于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);
  • 相关阅读:
    java执行构造器和初始化字段的顺序
    java语言中的varargs
    对Java语言的byte类型变量进行无符号提升
    VisualStudio 切换帐号 (原帐号已过期且无法登录时用)
    C/C++ 的关系运算符采用短路运算
    实现std::string的ltrim、rtrim和trim方法
    Excel 用于批量把单元格设置为"文本格式保存的数字"的宏
    为什么要用webUI?
    CEF3中js调用delphi内部方法
    2016-1-1最新版本的linphone-android在mac上编译通过,同时建立了IDEA工程
  • 原文地址:https://www.cnblogs.com/hyl8218/p/1806129.html
Copyright © 2011-2022 走看看