
/// <summary>
/// 生产word 文档
/// </summary>
public class GenerateWord
{
/// <summary>
///模板地址
/// </summary>
public string TempPath { get; set; } /// <summary>
/// 保存地址
/// </summary>
public string SavePath { get; set; } /// <summary>
/// 页眉数据
/// </summary>
public Hashtable HsHeads { get; set; } /// <summary>
/// 页脚数据
/// </summary>
public Hashtable HsFoots { get; set; } /// <summary>
/// 书签数据
/// </summary>
public Hashtable HsBookMark { get; set; } /// <summary>
/// 文档内容
/// </summary>
public DataSet DsBody { get; set; } /// <summary>
/// 文档名称
/// </summary>
public string FileName { get; set; }
public string EmptyLogo { get; set; }
private Thread thread; public void SaveTemplate()
{
DocX document = DocX.Load(TempPath); #region 生成页面页脚 Formatting f = new Formatting();
f.FontColor = Color.FromArgb(, , );
f.Size = ;
f.Bold = true;
f.FontFamily = new FontFamily("Helvetica"); Formatting ffoot = new Formatting();
ffoot.FontColor = Color.Black;
ffoot.Size = ;
ffoot.FontFamily = new FontFamily("Helvetica"); document.AddHeaders(); //添加所有页眉
Headers heads = document.Headers; //获取该文档所有的页眉
Header hfirst = heads.first;
Header head1 = heads.even;
Header head2 = heads.odd; hfirst.InsertParagraph(HsBookMark["MerchantName"].ToString(), false, f).Direction = Direction.RightToLeft;
head1.InsertParagraph(HsBookMark["MerchantName"].ToString(), false, f).Direction=Direction.RightToLeft;
head2.InsertParagraph(HsBookMark["MerchantName"].ToString(), false, f).Direction=Direction.RightToLeft; document.AddFooters();//添加所有的页脚
Footers footers = document.Footers; //获取该文档所有的页脚
Footer ffirst = footers.first;
Footer feven = footers.even;
Footer fodd = footers.odd; document.DifferentFirstPage = true;
document.DifferentOddAndEvenPages = true; Paragraph pf = ffirst.InsertParagraph(HsFoots["comp_name"].ToString(), false, ffoot);
Paragraph pfeven = feven.InsertParagraph(HsFoots["comp_name"].ToString(), false, ffoot);
Paragraph pfodd = fodd.InsertParagraph(HsFoots["comp_name"].ToString(), false, ffoot); //添加logo
Novacode.Image img;
MemoryStream stmMemory = new MemoryStream(); Paragraph p = head1.InsertParagraph("", false);
if (HsHeads["LogoUrl"] == null || HsHeads["LogoUrl"].ToString() == "")
{ System.Drawing.Image myimg = System.Drawing.Image.FromFile(EmptyLogo);
myimg.Save(stmMemory, myimg.RawFormat); // 保存你的图片到memorystream
stmMemory.Seek(, SeekOrigin.Begin);
img = document.AddImage(stmMemory);
}
else
{
System.Net.WebRequest webreq = System.Net.WebRequest.Create(HsHeads["LogoUrl"].ToString());//"http://www.bc.ccoo.cn/logo/logo.gif"
System.Net.WebResponse webres = webreq.GetResponse();
Stream stream = webres.GetResponseStream();
System.Drawing.Image myimg = System.Drawing.Image.FromStream(stream);
myimg.Save(stmMemory, myimg.RawFormat); // 保存你的图片到memorystream
stmMemory.Seek(, SeekOrigin.Begin);
img = document.AddImage(stmMemory);
stream.Close();
} string footmark = string.Format("地址:{0} 电话:{1} QQ:{2}", HsFoots["address"].ToString(), HsFoots["tel"].ToString(), HsFoots["qq"].ToString());
//将图像插入到段落后面
Picture pic = img.CreatePicture(); //选择图像,并修改图像尺寸
pic.Rotation = ;
pic.Width = ;
pic.Height = ; //设置图片形状,并水平翻转图片
pic.SetPictureShape(BasicShapes.cube);
pic.FlipHorizontal = false; pf.InsertPicture(pic, );
pf.Direction = Direction.RightToLeft;
pf.InsertParagraphAfterSelf(footmark, false, ffoot); pfeven.InsertPicture(pic);
pfeven.Direction = Direction.RightToLeft;
pfeven.InsertParagraphAfterSelf(footmark, false, ffoot); pfodd.InsertPicture(pic);
pfodd.Direction = Direction.RightToLeft;
pfodd.InsertParagraphAfterSelf(footmark, false, ffoot); #endregion #region 替换文本 document.ReplaceText("$ReportTitle$", HsBookMark["MerchantName"].ToString());
document.ReplaceText("$r_year$", DateTime.Now .Year.ToString());
document.ReplaceText("$r_month$", DateTime.Now.Month.ToString());
document.ReplaceText("$Report_head$", HsBookMark["OperatingCommissioner"].ToString()); document.ReplaceText("$IP$", HsBookMark["IPNUM"].ToString());
document.ReplaceText("$PV$", HsBookMark["PVNUM"].ToString());
document.ReplaceText("$FK$", HsBookMark["FKNUM"].ToString()); document.ReplaceText("$UserFeedBack$", HsBookMark["UserFeedback"].ToString());
document.ReplaceText("$ThinkingMonth$", HsBookMark["ThinkingMonth"].ToString());
document.ReplaceText("$ThinkingNextMonth$", HsBookMark["ThinkingNextMonth"].ToString()); #endregion #region 替换表格数据
Table t = document.Tables[];
if (t != null)
{
//生成Pv流量
if (DsBody.Tables.Count > && DsBody.Tables[].Rows.Count > )
{
List<Cell> listcell = t.Rows[].Cells;
listcell[].Paragraphs[].Append(DsBody.Tables[].Columns[].ColumnName.ToString()).Alignment = Alignment.center;
listcell[].Paragraphs[].Append(DsBody.Tables[].Columns[].ColumnName.ToString()).Alignment = Alignment.center;
listcell[].Paragraphs[].Append(DsBody.Tables[].Columns[].ColumnName.ToString()).Alignment = Alignment.center; for (int i = ; i < t.Rows.Count; i++)
{
listcell = t.Rows[i].Cells;
for (int j = ; j < ; j++)
{
listcell[j + ].Paragraphs[].Append(DsBody.Tables[].Rows[i - ][j].ToString()).Alignment = Alignment.center;
}
}
}
} #endregion #region 动态生成表格
//生成运维日志
if (DsBody.Tables.Count > && DsBody.Tables[].Rows.Count > )
{
for (int i = ; i < DsBody.Tables[].Rows.Count; i++)
{
CreateAndInsertTableAfter(ref document, DsBody.Tables[].Rows[i], Convert.ToString(i + ), HsBookMark["OperatingCommissioner"].ToString());
}
}
#endregion
//声明内存流
MemoryStream ms = new MemoryStream();
//文件保存到内存流
document.SaveAs(ms);
document.Dispose();
#region 更新数据库 //更新数据库
//发送到远处服务器进行保存
#endregion
} private static Table CreateAndInsertTableAfter(ref DocX document,DataRow dr,string indexnum,string head)
{
#region 定义字体颜色
Formatting f1 = new Formatting();
f1.FontColor = Color.White;
f1.Size = ;
f1.Bold = true;
f1.FontFamily = new FontFamily("Helvetica"); Formatting f2 = new Formatting();
f2.FontColor = Color.FromArgb(, , );
f2.Size = ;
f2.Bold = true;
f2.FontFamily = new FontFamily("Helvetica"); Formatting f3 = new Formatting();
f3.FontColor = Color.FromArgb(, , );
f3.Size = ;
f3.Bold = true;
f3.FontFamily = new FontFamily("Helvetica"); Formatting f4 = new Formatting();
f4.FontColor = Color.FromArgb(, , );
f4.Size = ;
f4.Bold = true;
f4.FontFamily = new FontFamily("Helvetica"); Formatting f5 = new Formatting();
f5.FontColor = Color.FromArgb(, , );
f5.Size = ;
f5.Bold = true;
f5.FontFamily = new FontFamily("Helvetica"); Formatting f6 = new Formatting();
f5.FontColor = Color.FromArgb(, , );
f5.Size = ;
f5.FontFamily = new FontFamily("Helvetica");
#endregion Table table = document.InsertTable(, ); table.AutoFit = AutoFit.ColumnWidth; #region Table title table.Rows[].Height = ; List<Cell> listtable = table.Rows[].Cells;
listtable[].Paragraphs[].InsertText(indexnum, false, f1);
listtable[].MarginTop = ;
listtable[].FillColor = Color.FromArgb(, , );
listtable[].Width = ;
listtable[].Paragraphs[].Alignment = Alignment.center; listtable[].FillColor = Color.FromArgb(, , );
listtable[].Paragraphs[].InsertText(dr["systime"].ToString() + "/" + (dr["OpMan"].ToString() == "" ? head : dr["OpMan"].ToString()), false, f6);
listtable[].Paragraphs[].Alignment = Alignment.right;
listtable[].Width = ; List<Cell> listtable1 = table.Rows[].Cells;
listtable1[].Paragraphs[].InsertText("行动", false, f2);
listtable1[].Width = ;
listtable1[].Paragraphs[].Alignment = Alignment.center; listtable1[].Paragraphs[].InsertText(dr["dowhat"].ToString(), false, f6);
listtable1[].Paragraphs[].Alignment = Alignment.left; List<Cell> listtable2 = table.Rows[].Cells;
listtable2[].Paragraphs[].InsertText("原因", false, f3);
listtable2[].Width = ;
listtable2[].Paragraphs[].Alignment = Alignment.center; listtable2[].Paragraphs[].InsertText(dr["reason"].ToString(), false, f6);
listtable2[].Paragraphs[].Alignment = Alignment.left; List<Cell> listtable3 = table.Rows[].Cells;
listtable3[].Paragraphs[].InsertText("作用", false, f4);
listtable3[].Width = ;
listtable3[].Paragraphs[].Alignment = Alignment.center; listtable3[].Paragraphs[].InsertText(dr["effect"].ToString(), false, f6);
listtable3[].Paragraphs[].Alignment = Alignment.left; List<Cell> listtable4 = table.Rows[].Cells;
listtable4[].Paragraphs[].InsertText("结果", false, f5);
listtable4[].Width = ; listtable4[].Paragraphs[].Alignment = Alignment.center;
Novacode.Image img;
MemoryStream stmMemory = new MemoryStream();
if (dr["PicUrl"] != null && dr["PicUrl"].ToString() != "")
{
System.Net.WebRequest webreq = System.Net.WebRequest.Create(dr["PicUrl"].ToString());
System.Net.WebResponse webres = webreq.GetResponse();
Stream stream = webres.GetResponseStream();
System.Drawing.Image myimg = System.Drawing.Image.FromStream(stream);
myimg.Save(stmMemory, myimg.RawFormat); // 保存你的图片到memorystream
stmMemory.Seek(, SeekOrigin.Begin);
img = document.AddImage(stmMemory);
stream.Close();
listtable4[].Paragraphs[].InsertPicture(img.CreatePicture(, ));
}
else
{
var hrefresult = Regex.Match(dr["result"].ToString(), @"(?<=[\s+]?href[\s+]?=[\s+]?('|"")?)[^(""|')>]+?(?=""|')");
if (hrefresult.ToString() != "")
{
Hyperlink link = document.AddHyperlink("点击查看结果", new Uri(hrefresult.ToString()));
listtable4[].Paragraphs[].AppendHyperlink(link);
}
else
{
hrefresult = Regex.Match(dr["result"].ToString(), @"http://www.*");
if (hrefresult.ToString() != "")
{
Hyperlink link = document.AddHyperlink("点击查看结果", new Uri(hrefresult.ToString()));
listtable4[].Paragraphs[].AppendHyperlink(link);
}
else
{
listtable4[].Paragraphs[].Append(dr["result"].ToString());
}
}
} listtable4[].Paragraphs[].Alignment = Alignment.left; #endregion table.Alignment = Alignment.center;
table.InsertParagraphAfterSelf("",false).AppendLine().AppendLine();
return table;
}
public GenerateWord(string TempPath, string FileName, string EmptyLogo, string SavePath, Hashtable HsHeads, Hashtable HsFoots, Hashtable HsBookMark, DataSet DsBody)
{
this.TempPath = TempPath;
this.SavePath = SavePath;
this.EmptyLogo = EmptyLogo;
this.HsHeads = HsHeads;
this.HsFoots = HsFoots;
this.HsBookMark = HsBookMark;
this.FileName = FileName;
this.DsBody = DsBody;
thread = new Thread(new ThreadStart(SaveTemplate));
}
public void Start()
{
if (thread != null)
{
thread.Start();
}
}
}