.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(http://www.blog.com.cn/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用戶,還有打開本地訪問遠程訪問

 
發佈了69 篇原創文章 · 獲贊 3 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章