/// <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