1 /// <summary> 2 /// 生成word文檔 3 /// </summary> 4 /// <param name="tempPath">模板絕對路徑</param> 5 /// <param name="savePath">模板保存路徑(包含文件名稱 後綴必須是docx)</param> 6 /// <param name="hsHeads">頁眉數據</param> 7 /// <param name="hsFoots">頁腳數據</param> 8 /// <param name="hsBookMark">書籤數據</param> 9 /// <param name="dtBody">文檔內容</param> 10 public static void SaveTemplate(string tempPath,string savePath,Hashtable hsHeads,Hashtable hsFoots,Hashtable hsBookMark,DataTable dtBody){ 11 using (DocX document = DocX.Load(tempPath)) 12 { 13 #region 生成頁眉 14 document.AddHeaders(); //添加全部頁眉 15 Headers heads = document.Headers; //獲取該文檔全部的頁腳 16 Header hfirst = heads.first; 17 Header head1 = heads.even; 18 Header head2 = heads.odd; 19 20 //添加logo 21 Paragraph p = head1.InsertParagraph("", false); 22 System.Net.WebRequest webreq = System.Net.WebRequest.Create("http://www.bc.ccoo.cn/logo/logo.gif"); 23 System.Net.WebResponse webres = webreq.GetResponse(); 24 Stream stream = webres.GetResponseStream(); 25 MemoryStream stmMemory = new MemoryStream(); 26 System.Drawing.Image myimg = System.Drawing.Image.FromStream(stream); 27 myimg.Save(stmMemory, myimg.RawFormat); // 保存你的圖片到memorystream 28 stmMemory.Seek(0, SeekOrigin.Begin); 29 Novacode.Image img = document.AddImage(stmMemory); 30 stream.Close(); 31 32 //將圖像插入到段落後面 33 Picture pic = img.CreatePicture(); 34 35 //選擇圖像,並修改圖像尺寸 36 pic.Rotation = 0; 37 pic.Width = 150; 38 pic.Height = 55; 39 40 //設置圖片形狀,並水平翻轉圖片 41 pic.SetPictureShape(BasicShapes.cube); 42 pic.FlipHorizontal = false; 43 p.InsertPicture(pic, 0); 44 p.InsertText(" 真誠爲您服務"); 45 p.AppendLine(); 46 Paragraph ph2 = head2.InsertParagraph("", false); 47 48 ph2.InsertPicture(pic, 0); 49 ph2.InsertText(" 真誠爲您服務"); 50 ph2.AppendLine(); 51 52 Paragraph phfirst = hfirst.InsertParagraph("", false); 53 54 phfirst.InsertPicture(pic, 0); 55 phfirst.UnderlineColor(System.Drawing.Color.Yellow); 56 phfirst.InsertText(" 真誠爲您服務"); 57 phfirst.AppendLine(); 58 #endregion 59 60 #region 生成文檔中內容 61 62 foreach (Paragraph pbody in document.Paragraphs) 63 { 64 var bookmarks= pbody.GetBookmarks(); 65 foreach (Bookmark item in bookmarks) 66 { 67 switch (item.Name) 68 { 69 case "MerchantName": //商家名稱 70 item.Paragraph.ReplaceText("{MerchantName}", hsBookMark["MerchantName"].ToString()); 71 break; 72 case "OperatingCommissioner"://運營專員 73 item.Paragraph.ReplaceText("{OperatingCommissioner}", hsBookMark["OperatingCommissioner"].ToString()); 74 break; 75 case "OperatingTime"://運營時間 76 item.Paragraph.ReplaceText("{OperatingTime}", hsBookMark["OperatingTime"].ToString()); 77 break; 78 case "IPNUM"://IP流量 79 item.Paragraph.ReplaceText("{IPNUM}", "88"+hsBookMark["OperatingTime"].ToString()); 80 break; 81 case "PVNUM"://PV 82 item.Paragraph.ReplaceText("{PVNUM}", "868" + hsBookMark["OperatingTime"].ToString()); 83 break; 84 case "FKNUM"://feek 85 item.Paragraph.ReplaceText("{FKNUM}", "878" + hsBookMark["OperatingTime"].ToString()); 86 break;116 } 117 } 118 } 119 List<Table> table = document.Tables; 120 Row newRow = table[1].InsertRow(1); 121 newRow.Cells[0].Paragraphs[0].InsertText("&&&&&&&hhhHHHH00000", false); 122 newRow.Cells[1].Paragraphs[0].InsertText("&&&&&&&hhhHHHH111111111111",false); 123 124 #endregion 125 126 #region 生成頁腳 127 document.AddFooters();//添加全部的頁腳 128 Footers footers = document.Footers; //獲取該文檔全部的頁腳 129 //獲取文檔第一頁的頁腳 130 Footer first = footers.first; 131 132 //獲取奇數頁的頁腳 133 Footer odd = footers.odd; 134 ////設置不一樣頁使用不一樣的頁腳 135 document.DifferentFirstPage = true; 136 document.DifferentOddAndEvenPages = true; 137 //設置頁腳的內容 138 Paragraph pf = first.InsertParagraph(); 139 pf.Append("頁腳內容替換成你的頁腳內容"); 140 141 Paragraph p2 = footers.even.InsertParagraph(); 142 p2.Append("頁腳內容替換成你的頁腳內容"); 143 144 Paragraph p3 = footers.odd.InsertParagraph(); 145 p3.Append("頁腳內容替換成你的頁腳內容"); 146 #endregion 147 148 document.SaveAs(savePath); 149 } 150 }
2.docx下載web