标签:
1 /// <summary> 2 /// 生产word 文档 3 /// </summary> 4 public class GenerateWord 5 { 6 /// <summary> 7 ///模板地址 8 /// </summary> 9 public string TempPath { get; set; } 10 11 /// <summary> 12 /// 保存地址 13 /// </summary> 14 public string SavePath { get; set; } 15 16 /// <summary> 17 /// 页眉数据 18 /// </summary> 19 public Hashtable HsHeads { get; set; } 20 21 /// <summary> 22 /// 页脚数据 23 /// </summary> 24 public Hashtable HsFoots { get; set; } 25 26 /// <summary> 27 /// 书签数据 28 /// </summary> 29 public Hashtable HsBookMark { get; set; } 30 31 /// <summary> 32 /// 文档内容 33 /// </summary> 34 public DataSet DsBody { get; set; } 35 36 /// <summary> 37 /// 文档名称 38 /// </summary> 39 public string FileName { get; set; } 40 public string EmptyLogo { get; set; } 41 private Thread thread; 42 43 public void SaveTemplate() 44 { 45 DocX document = DocX.Load(TempPath); 46 47 #region 生成页面页脚 48 49 Formatting f = new Formatting(); 50 f.FontColor = Color.FromArgb(50, 153, 205); 51 f.Size = 12; 52 f.Bold = true; 53 f.FontFamily = new FontFamily("Helvetica"); 54 55 Formatting ffoot = new Formatting(); 56 ffoot.FontColor = Color.Black; 57 ffoot.Size = 8; 58 ffoot.FontFamily = new FontFamily("Helvetica"); 59 60 document.AddHeaders(); //添加所有页眉 61 Headers heads = document.Headers; //获取该文档所有的页眉 62 Header hfirst = heads.first; 63 Header head1 = heads.even; 64 Header head2 = heads.odd; 65 66 hfirst.InsertParagraph(HsBookMark["MerchantName"].ToString(), false, f).Direction = Direction.RightToLeft; 67 head1.InsertParagraph(HsBookMark["MerchantName"].ToString(), false, f).Direction=Direction.RightToLeft; 68 head2.InsertParagraph(HsBookMark["MerchantName"].ToString(), false, f).Direction=Direction.RightToLeft; 69 70 document.AddFooters();//添加所有的页脚 71 Footers footers = document.Footers; //获取该文档所有的页脚 72 Footer ffirst = footers.first; 73 Footer feven = footers.even; 74 Footer fodd = footers.odd; 75 76 document.DifferentFirstPage = true; 77 document.DifferentOddAndEvenPages = true; 78 79 80 Paragraph pf = ffirst.InsertParagraph(HsFoots["comp_name"].ToString(), false, ffoot); 81 Paragraph pfeven = feven.InsertParagraph(HsFoots["comp_name"].ToString(), false, ffoot); 82 Paragraph pfodd = fodd.InsertParagraph(HsFoots["comp_name"].ToString(), false, ffoot); 83 84 //添加logo 85 Novacode.Image img; 86 MemoryStream stmMemory = new MemoryStream(); 87 88 Paragraph p = head1.InsertParagraph("", false); 89 if (HsHeads["LogoUrl"] == null || HsHeads["LogoUrl"].ToString() == "") 90 { 91 92 System.Drawing.Image myimg = System.Drawing.Image.FromFile(EmptyLogo); 93 myimg.Save(stmMemory, myimg.RawFormat); // 保存你的图片到memorystream 94 stmMemory.Seek(0, SeekOrigin.Begin); 95 img = document.AddImage(stmMemory); 96 } 97 else 98 { 99 System.Net.WebRequest webreq = System.Net.WebRequest.Create(HsHeads["LogoUrl"].ToString());//"http://www.bc.ccoo.cn/logo/logo.gif" 100 System.Net.WebResponse webres = webreq.GetResponse(); 101 Stream stream = webres.GetResponseStream(); 102 System.Drawing.Image myimg = System.Drawing.Image.FromStream(stream); 103 myimg.Save(stmMemory, myimg.RawFormat); // 保存你的图片到memorystream 104 stmMemory.Seek(0, SeekOrigin.Begin); 105 img = document.AddImage(stmMemory); 106 stream.Close(); 107 } 108 109 string footmark = string.Format("地址:{0} 电话:{1} QQ:{2}", HsFoots["address"].ToString(), HsFoots["tel"].ToString(), HsFoots["qq"].ToString()); 110 //将图像插入到段落后面 111 Picture pic = img.CreatePicture(); 112 113 //选择图像,并修改图像尺寸 114 pic.Rotation = 0; 115 pic.Width = 111; 116 pic.Height = 39; 117 118 //设置图片形状,并水平翻转图片 119 pic.SetPictureShape(BasicShapes.cube); 120 pic.FlipHorizontal = false; 121 122 pf.InsertPicture(pic, 0); 123 pf.Direction = Direction.RightToLeft; 124 pf.InsertParagraphAfterSelf(footmark, false, ffoot); 125 126 pfeven.InsertPicture(pic); 127 pfeven.Direction = Direction.RightToLeft; 128 pfeven.InsertParagraphAfterSelf(footmark, false, ffoot); 129 130 pfodd.InsertPicture(pic); 131 pfodd.Direction = Direction.RightToLeft; 132 pfodd.InsertParagraphAfterSelf(footmark, false, ffoot); 133 134 #endregion 135 136 #region 替换文本 137 138 document.ReplaceText("$ReportTitle$", HsBookMark["MerchantName"].ToString()); 139 document.ReplaceText("$r_year$", DateTime.Now .Year.ToString()); 140 document.ReplaceText("$r_month$", DateTime.Now.Month.ToString()); 141 document.ReplaceText("$Report_head$", HsBookMark["OperatingCommissioner"].ToString()); 142 143 document.ReplaceText("$IP$", HsBookMark["IPNUM"].ToString()); 144 document.ReplaceText("$PV$", HsBookMark["PVNUM"].ToString()); 145 document.ReplaceText("$FK$", HsBookMark["FKNUM"].ToString()); 146 147 document.ReplaceText("$UserFeedBack$", HsBookMark["UserFeedback"].ToString()); 148 document.ReplaceText("$ThinkingMonth$", HsBookMark["ThinkingMonth"].ToString()); 149 document.ReplaceText("$ThinkingNextMonth$", HsBookMark["ThinkingNextMonth"].ToString()); 150 151 #endregion 152 153 #region 替换表格数据 154 Table t = document.Tables[0]; 166 if (t != null) 167 { 168 //生成Pv流量 169 if (DsBody.Tables.Count > 0 && DsBody.Tables[0].Rows.Count > 0) 170 { 171 List<Cell> listcell = t.Rows[0].Cells; 172 listcell[1].Paragraphs[0].Append(DsBody.Tables[0].Columns[0].ColumnName.ToString()).Alignment = Alignment.center; 173 listcell[2].Paragraphs[0].Append(DsBody.Tables[0].Columns[1].ColumnName.ToString()).Alignment = Alignment.center; 174 listcell[3].Paragraphs[0].Append(DsBody.Tables[0].Columns[2].ColumnName.ToString()).Alignment = Alignment.center; 175 176 for (int i = 1; i < t.Rows.Count; i++) 177 { 178 listcell = t.Rows[i].Cells; 179 for (int j = 0; j < 3; j++) 180 { 181 listcell[j + 1].Paragraphs[0].Append(DsBody.Tables[0].Rows[i - 1][j].ToString()).Alignment = Alignment.center; 182 } 183 } 184 } 185 } 186 187 #endregion 188 189 #region 动态生成表格 190 //生成运维日志 191 if (DsBody.Tables.Count > 0 && DsBody.Tables[1].Rows.Count > 0) 192 { 193 for (int i = 0; i < DsBody.Tables[1].Rows.Count; i++) 194 { 195 CreateAndInsertTableAfter(ref document, DsBody.Tables[1].Rows[i], Convert.ToString(i + 1), HsBookMark["OperatingCommissioner"].ToString()); 196 } 197 } 198 #endregion 199 //声明内存流 200 MemoryStream ms = new MemoryStream(); 201 //文件保存到内存流 202 document.SaveAs(ms); 203 document.Dispose(); 204 #region 更新数据库 205 206 //更新数据库217 218 //发送到远处服务器进行保存230 231 #endregion 232 } 233 234 private static Table CreateAndInsertTableAfter(ref DocX document,DataRow dr,string indexnum,string head) 235 { 236 #region 定义字体颜色 237 Formatting f1 = new Formatting(); 238 f1.FontColor = Color.White; 239 f1.Size = 10; 240 f1.Bold = true; 241 f1.FontFamily = new FontFamily("Helvetica"); 242 243 Formatting f2 = new Formatting(); 244 f2.FontColor = Color.FromArgb(255, 102, 102); 245 f2.Size = 10; 246 f2.Bold = true; 247 f2.FontFamily = new FontFamily("Helvetica"); 248 249 Formatting f3 = new Formatting(); 250 f3.FontColor = Color.FromArgb(255, 102, 0); 251 f3.Size = 10; 252 f3.Bold = true; 253 f3.FontFamily = new FontFamily("Helvetica"); 254 255 Formatting f4 = new Formatting(); 256 f4.FontColor = Color.FromArgb(50, 205, 255); 257 f4.Size = 10; 258 f4.Bold = true; 259 f4.FontFamily = new FontFamily("Helvetica"); 260 261 Formatting f5 = new Formatting(); 262 f5.FontColor = Color.FromArgb(40, 212, 190); 263 f5.Size = 10; 264 f5.Bold = true; 265 f5.FontFamily = new FontFamily("Helvetica"); 266 267 268 Formatting f6 = new Formatting(); 269 f5.FontColor = Color.FromArgb(102, 102, 102); 270 f5.Size = 12; 271 f5.FontFamily = new FontFamily("Helvetica"); 272 #endregion 273 274 Table table = document.InsertTable(5, 2); 275 276 table.AutoFit = AutoFit.ColumnWidth; 277 278 #region Table title 279 280 table.Rows[0].Height = 36; 281 282 List<Cell> listtable = table.Rows[0].Cells; 283 listtable[0].Paragraphs[0].InsertText(indexnum, false, f1); 284 listtable[0].MarginTop = 5; 285 listtable[0].FillColor = Color.FromArgb(112, 180, 218); 286 listtable[0].Width = 30; 287 listtable[0].Paragraphs[0].Alignment = Alignment.center; 288 289 290 listtable[1].FillColor = Color.FromArgb(232, 243, 249); 291 listtable[1].Paragraphs[0].InsertText(dr["systime"].ToString() + "/" + (dr["OpMan"].ToString() == "" ? head : dr["OpMan"].ToString()), false, f6); 292 listtable[1].Paragraphs[0].Alignment = Alignment.right; 293 listtable[1].Width = 580; 294 295 296 List<Cell> listtable1 = table.Rows[1].Cells; 297 listtable1[0].Paragraphs[0].InsertText("行动", false, f2); 298 listtable1[0].Width = 50; 299 listtable1[0].Paragraphs[0].Alignment = Alignment.center; 300 301 listtable1[1].Paragraphs[0].InsertText(dr["dowhat"].ToString(), false, f6); 302 listtable1[1].Paragraphs[0].Alignment = Alignment.left; 303 304 305 List<Cell> listtable2 = table.Rows[2].Cells; 306 listtable2[0].Paragraphs[0].InsertText("原因", false, f3); 307 listtable2[0].Width = 50; 308 listtable2[0].Paragraphs[0].Alignment = Alignment.center; 309 310 listtable2[1].Paragraphs[0].InsertText(dr["reason"].ToString(), false, f6); 311 listtable2[1].Paragraphs[0].Alignment = Alignment.left; 312 313 314 List<Cell> listtable3 = table.Rows[3].Cells; 315 listtable3[0].Paragraphs[0].InsertText("作用", false, f4); 316 listtable3[0].Width = 50; 317 listtable3[0].Paragraphs[0].Alignment = Alignment.center; 318 319 320 listtable3[1].Paragraphs[0].InsertText(dr["effect"].ToString(), false, f6); 321 listtable3[1].Paragraphs[0].Alignment = Alignment.left; 322 323 324 List<Cell> listtable4 = table.Rows[4].Cells; 325 listtable4[0].Paragraphs[0].InsertText("结果", false, f5); 326 listtable4[0].Width = 50; 327 328 329 listtable4[0].Paragraphs[0].Alignment = Alignment.center; 330 Novacode.Image img; 331 MemoryStream stmMemory = new MemoryStream(); 332 if (dr["PicUrl"] != null && dr["PicUrl"].ToString() != "") 333 { 334 System.Net.WebRequest webreq = System.Net.WebRequest.Create(dr["PicUrl"].ToString()); 335 System.Net.WebResponse webres = webreq.GetResponse(); 336 Stream stream = webres.GetResponseStream(); 337 System.Drawing.Image myimg = System.Drawing.Image.FromStream(stream); 338 myimg.Save(stmMemory, myimg.RawFormat); // 保存你的图片到memorystream 339 stmMemory.Seek(0, SeekOrigin.Begin); 340 img = document.AddImage(stmMemory); 341 stream.Close(); 342 listtable4[1].Paragraphs[0].InsertPicture(img.CreatePicture(330, 530)); 343 } 344 else 345 { 346 var hrefresult = Regex.Match(dr["result"].ToString(), @"(?<=[\s+]?href[\s+]?=[\s+]?(‘|"")?)[^(""|‘)>]+?(?=""|‘)"); 347 if (hrefresult.ToString() != "") 348 { 349 Hyperlink link = document.AddHyperlink("点击查看结果", new Uri(hrefresult.ToString())); 350 listtable4[1].Paragraphs[0].AppendHyperlink(link); 351 } 352 else 353 { 354 hrefresult = Regex.Match(dr["result"].ToString(), @"http://www.*"); 355 if (hrefresult.ToString() != "") 356 { 357 Hyperlink link = document.AddHyperlink("点击查看结果", new Uri(hrefresult.ToString())); 358 listtable4[1].Paragraphs[0].AppendHyperlink(link); 359 } 360 else 361 { 362 listtable4[1].Paragraphs[0].Append(dr["result"].ToString()); 363 } 364 } 365 } 366 367 listtable4[1].Paragraphs[0].Alignment = Alignment.left; 368 369 #endregion 370 371 table.Alignment = Alignment.center; 372 table.InsertParagraphAfterSelf("",false).AppendLine().AppendLine(); 373 return table; 374 } 375 public GenerateWord(string TempPath, string FileName, string EmptyLogo, string SavePath, Hashtable HsHeads, Hashtable HsFoots, Hashtable HsBookMark, DataSet DsBody) 376 { 377 this.TempPath = TempPath; 378 this.SavePath = SavePath; 379 this.EmptyLogo = EmptyLogo; 380 this.HsHeads = HsHeads; 381 this.HsFoots = HsFoots; 382 this.HsBookMark = HsBookMark; 383 this.FileName = FileName; 384 this.DsBody = DsBody; 385 thread = new Thread(new ThreadStart(SaveTemplate)); 386 } 387 public void Start() 388 { 389 if (thread != null) 390 { 391 thread.Start(); 392 } 393 } 394 }
标签:
原文地址:http://www.cnblogs.com/linsu/p/4607781.html