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

推荐订阅源

W
WeLiveSecurity
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
S
Security @ Cisco Blogs
T
Threat Research - Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
腾讯CDC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
罗磊的独立博客
F
Full Disclosure
博客园 - 【当耐特】
C
CERT Recently Published Vulnerability Notes
Engineering at Meta
Engineering at Meta
Application and Cybersecurity Blog
Application and Cybersecurity Blog
T
Threatpost
I
Intezer
V2EX - 技术
V2EX - 技术
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The Hacker News
The Hacker News
小众软件
小众软件
Google DeepMind News
Google DeepMind News
T
Tailwind CSS Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
N
News | PayPal Newsroom
MyScale Blog
MyScale Blog
AI
AI
Vercel News
Vercel News
Spread Privacy
Spread Privacy
美团技术团队
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
Google DeepMind News
Google DeepMind News
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
U
Unit 42
L
LangChain Blog
Recent Announcements
Recent Announcements

博客园 - 馒头

加载安卓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用户,还有打开本地访问远程访问