惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
小众软件
小众软件
T
Tailwind CSS Blog
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
V
Visual Studio Blog
罗磊的独立博客
有赞技术团队
有赞技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Jina AI
Jina AI
量子位
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
Hugging Face - Blog
Hugging Face - Blog
P
Proofpoint News Feed
N
Netflix TechBlog - Medium
GbyAI
GbyAI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
腾讯CDC
美团技术团队

博客园 - 馒头

加载安卓SDK失败的办法 WCF服务端配置容易被忽略的细节问题 对 wcf 的安全机制传输安全的疑惑求教 [转].net试用分布式数据库事务com+ [转].net下跨数据分布式事务的处理办法TransactionScope [转]Page的生存周期 [转]如何获得动态添加的html控件的值 - 馒头 - 博客园 如何在客户端清除fileUpLoad控件的文件路径 - 馒头 - 博客园 来自朋友的关爱 T_Sql之Str()和Cast .net word编程对象简介 Oracle 与SQL Server 2000常用函数对照 [摘抄] 项目中除了需求外还需要知道的问题 .net调用数据库存储过程应当注意的问题 [转]Asp.Net下导出/导入规则的Excel(.xls)文件 [转]常用表达式 [转]ASP.NET提供文件下载函数 [转]项目的成败 如何向存储过程传送数组
.Net将数据导出Word
馒头 · 2007-05-21 · via 博客园 - 馒头

.Net将数据导出Word

1,首先要导入Com文件Microsoft Word 11.0 Object Library.
2.声明using System.Text.RegularExpressions;
-----------------------------------------------------------------
3.执行下面步骤
Object Nothing = System.Reflection.Missing.Value;
        //取得Word文件保存路径
        object filename = System.Web.HttpRuntime.AppDomainAppPath + "\\XMLFiles\\EduceWordFiles\\" + this.Context.User.Identity.Name + ".doc";
        //创建一个名为WordApp的组件对象
        Word.Application WordApp = new Word.ApplicationClass();
        //创建一个名为WordDoc的文档对象
        Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
        //增加一表格
        //Word.Table table = WordDoc.Tables.Add(WordApp.Selection.Range, 1, 1, ref Nothing, ref Nothing);
        //在表格第一单元格中添加自定义的文字内容
        //table.Cell(1, 1).Range.Text = "在表格第一单元格中添加自定义的文字内容";
        //在文档空白地方添加文字内容
        //WordDoc.Paragraphs.Last.Range.Bold = 72;
        //WordApp.Visible = true;
        //WordDoc.Activate();


        #region 标题
        WordApp.Selection.Font.Size = 15;
        WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; // 居中
        WordApp.Selection.Font.Bold = 1;    // 黑体
        WordApp.Selection.TypeText(SaveShowInfo.Title);
        #endregion

        #region 时间和来源
        WordApp.Selection.TypeParagraph();
        WordApp.Selection.Font.Size = 10;
        WordApp.Selection.Font.Bold = 0;    // 取消黑体
        WordApp.Selection.TypeText("发布时间:" + SaveShowInfo.SaveTime + " 来源:" + SaveShowInfo.WebSiteName);
        #endregion

        #region 摘要
        WordApp.Selection.TypeParagraph();
        WordApp.Selection.TypeParagraph();
        WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; // 居左
        WordApp.Selection.TypeText("摘要:");
        WordApp.Selection.TypeParagraph();
        //WordApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = 2.0f;  //首行缩进2个字符
        WordApp.Selection.TypeText("    " + SaveShowInfo.Summary);
        #endregion

        #region 内容

        WordApp.Selection.TypeParagraph();
        WordApp.Selection.TypeParagraph();
        WordApp.Selection.TypeText("内容:");

        string strPageContent = SaveShowInfo.Content.ToString();
        //将一个<br>变成两个<br>
        //strPageContent = Regex.Replace(strPageContent, "(<br>[\\s]*)+", "<br /><br />");
        //将所有标签去掉,只剩下\r\n
        strPageContent = Regex.Replace(strPageContent, @"<[^>]+/?>|</[^>]+>", "", RegexOptions.IgnoreCase);


        string[] strContents = strPageContent.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

        foreach (string strContent in strContents)
        {
            WordApp.Selection.TypeParagraph();
            WordApp.Selection.TypeText("    " + strContent);
        }

        #endregion

        #region 图片导出
       
        string[] strPictureUrls = SaveShowInfo.PictureUrl.Split(new string[] { "<br />" }, StringSplitOptions.RemoveEmptyEntries);
        if (strPictureUrls.Length != 0 && strPictureUrls[0] != "")
        {
            WordApp.Selection.TypeParagraph();
            WordApp.Selection.TypeParagraph();
            WordApp.Selection.TypeText("图片:");
            WordApp.Selection.TypeParagraph();
            WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; // 居中
            foreach (string strPictureUrl in strPictureUrls)
            {
                if (strPictureUrl.Length > 10)
                {
                    string strUrl = getPictureOnlyUrl(strPictureUrl);

                    WordApp.Selection.InlineShapes.AddPicture(strUrl, ref Nothing, ref Nothing, ref Nothing);
                    WordApp.Selection.TypeParagraph();

                }
            }
        }
        #endregion

        //WordDoc.Paragraphs.Last.Range.Text += SaveShowInfo.PictureUrl;

        //将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);
        //返回结果
        //lblMsg.Text = "文档路径:<a href='/c:\\111.doc'>c:\\111.doc</a>(点击链接查看)<br>生成结果:成功!";

        //使导出文件清除特殊符号
        string outFileName = si.Title.Replace("/", " ");
        outFileName = outFileName.Replace("\\", " ");
        outFileName = outFileName.Replace(":", " ");
        outFileName = outFileName.Replace("*", " ");
        outFileName = outFileName.Replace("?", " ");
        outFileName = outFileName.Replace("\"", " ");
        outFileName = outFileName.Replace("<", " ");
        outFileName = outFileName.Replace(">", " ");
        outFileName = outFileName.Replace("|", " ");
//这个是从服务器中下载文件,(请参考我另外一个文章)
//参考网址http://www.cnblogs.com/ghostljj/archive/2007/01/24/629293.html
ResponseFile(Page.Request, Page.Response, outFileName + ".doc"
            , System.Web.HttpRuntime.AppDomainAppPath + "\\XMLFiles\\EduceWordFiles\\" + this.Context.User.Identity.Name + ".doc", 1024000);
//--------------------------------------------------------
3.如果是放在IIS中,现在是不能到出的,还要配置一下
方案一:在Web.config中添加
         <system.web>
               <identity impersonate="true" userName="管理员名" password="密码" />
         <system.web>
方案二:
         (1)在运行->dcomcnfg打开组件服务
         (2) 在 控制台根目录->组件服务->计算机->我的电脑->DCOM配置->Microsoft Word 文档->属性->安全
         (3)启动和激活权限->使用自定义->添加一个ASPNET用户,还有打开本地启动本地激活
              访问权限->使用自定义->添加一个ASPNET用户,还有打开本地访问远程访问