zoukankan      html  css  js  c++  java
  • 网页中数据导出方法.net语言 3.导出word格式的

    .net将数据导出Word 
    1,首先要导入Com文件Microsoft Word 11.0 Object Library.
    2.声明using System.Text.RegularExpressions;powered by 25175.net
    -----------------------------------------------------------------
    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用户,还有打开本地访问和远程访问

  • 相关阅读:
    为什么要用webUI?
    探索WebKit内核(一)------ 菜鸟起步
    主进程退出的时候,杀死所有子进程
    那两年炼就的Android内功修养
    飞鸽---局域网聊天软件攻防战
    如何利用Fluxion诱惑目标用户获取WPA密码
    性能测试:CPU内存,硬盘IO读写,带宽速度,UnixBench
    解决maven编译错误:程序包com.sun.xml.internal.ws.spi不存在
    Apache-Flink深度解析-DataStream-Connectors之Kafka
    linux下find(文件查找)命令的用法总结
  • 原文地址:https://www.cnblogs.com/wei2yi/p/2100088.html
Copyright © 2011-2022 走看看