c# 將頁面匯出到word(含圖片及控制項)

來源:互聯網
上載者:User

標籤:

/// <summary>        /// 建立word        /// <param name="filePath">檔案路徑 </param>        /// </summary>        protected void CreateWordFile(string filePath)                {            if (File.Exists(filePath))                         {                File.Delete(filePath);            }            using (FileStream fs = File.Create(filePath))                        {                fs.Close();            }        }
    /// <summary>        /// 將頁面內容輸出到Word        /// <param name="filePath">檔案路徑 </param>        /// </summary>        protected void WritePageContentToWord(string filePath)        {            string pageHtml = string.Empty;            StringWriter sw = new StringWriter();            HtmlTextWriter htw = new HtmlTextWriter(sw);            this.divAll.RenderControl(htw);            pageHtml = sw.ToString();            pageHtml.Replace("../App_Themes/blue/Image/FiveStar.png", Server.MapPath("./App_Themes/blue/Image/FiveStar.png"));            pageHtml.Replace("../App_Themes/blue/Image/FourStar.png", Server.MapPath("./App_Themes/blue/Image/FourStar.png"));            pageHtml.Replace("../App_Themes/blue/Image/ThreeStar.png", Server.MapPath("./App_Themes/blue/Image/ThreeStar.png"));//這裡將圖片相對路徑換成絕對路徑            if (File.Exists(filePath))            {                StreamWriter streamW = File.CreateText(filePath);                streamW.Write(pageHtml);                streamW.Close();            }            sw.Close();            htw.Close();        }
protected void InsertPhoto(string filePath)        {            //產生圖片            string imagePath = MapPath("/File2/" + Request.QueryString["ID"].ToString() + ".png");            Session["imagePath"] = imagePath;            //插入圖片;            StringBuilder reportContent = new StringBuilder();            object Nothing = System.Reflection.Missing.Value;            object filename = filePath;            Microsoft.Office.Interop.Word.Application WordApp2 = new Microsoft.Office.Interop.Word.Application();//.ApplicationClass();            Microsoft.Office.Interop.Word.Document WordDoc2 = WordApp2.Documents.Open(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);            //此處開啟的這個word實際為網頁的文字格式設定(可用記事本開啟看一下),需另存新檔doc格式,            //才能把圖片嵌入到word文檔中,否則儲存的永遠就是連結            WordDoc2.SaveAs(filePath, WdSaveFormat.wdFormatDocument);            //迴圈Word文檔中所有插入的圖元,尋找顯示為空白的圖元,進行替換            //因該Word文檔是由HTML產生而來,存放的圖片實際為空白,類型為wdInlineShapeLinkedPicture,需替換為實際類型            //IEnumerable<InlineShape> shape2 =new IEnumerable  WordDoc2.InlineShapes;            foreach (InlineShape shape in WordDoc2.InlineShapes)
         { //尋找插入圖形的位置 object oRange = WordDoc2.Paragraphs[17].Range;//擷取圖片插入的位置 object LinkToFile = false; object SaveWithDocument = true; //插入圖形 WordDoc2.InlineShapes.AddPicture(imagePath, ref LinkToFile, ref SaveWithDocument, ref oRange); //刪除顯示為空白的圖元 shape.Select(); WordApp2.Selection.Delete(); break;  } //開啟該文檔時預設視圖為整頁模式方式 WordDoc2.ActiveWindow.View.Type = WdViewType.wdPrintView; //儲存插入圖片的Word WordDoc2.SaveAs(filePath, WdSaveFormat.wdFormatDocument); //關閉所有的Word文檔 WordApp2.NormalTemplate.Saved = true; Object saveChanges2 = Microsoft.Office.Interop.Word.WdSaveOptions.wdSaveChanges; WordApp2.Documents.Close(saveChanges2, Type.Missing, Type.Missing); //退出Word應用程式 try
       { WordApp2.Application.Quit(Type.Missing, Type.Missing, Type.Missing); if (WordApp2 != null)
         { WordApp2 = null; } } catch { } finally
       { GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } }
protected void lnkToWord_Click(object sender, EventArgs e)        {             Chart2.SaveImage(Server.MapPath("~/File2/" + Request.QueryString["ID"].ToString() + ".png"), ChartImageFormat.Png);            Label lbName = fv.FindControl("lbName") as Label;            string wordname = lbName.Text.ToString() + Request.QueryString["ID"];            string filePath = MapPath("~/File2/") + wordname + ".doc";  //檔案路徑            CreateWordFile(filePath);            WritePageContentToWord(filePath);            InsertPhoto(filePath);            string FileName = wordname + ".doc";            Response.Redirect(string.Format("~/File2/{0}", FileName));            string ImagePath = Session["imagePath"].ToString();        }

具體做法是:先將頁面寫成html,在寫成html時將圖片相對路徑改成絕對路徑,儲存成一份word到伺服器。此頁麵包含.Net裡面的chart控制項,所以先將chart控制項變成圖片存入伺服器,最後將圖片插入到word

c# 將頁面匯出到word(含圖片及控制項)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.