1.生成word代码
/// <summary>
/// 生成word文档
/// </summary>
/// <param name="tempPath">模板绝对路径</param>
/// <param name="savePath">模板保存路径(包含文件名称 后缀必须是docx)</param>
/// <param name="hsHeads">页眉数据</param>
/// <param name="hsFoots">页脚数据</param>
/// <param name="hsBookMark">书签数据</param>
/// <param name="dtBody">文档内容</param>
public static void SaveTemplate(string tempPath,string savePath,Hashtable hsHeads,Hashtable hsFoots,Hashtable hsBookMark,DataTable dtBody){
using (DocX document = DocX.Load(tempPath))
{
#region 生成页眉
document.AddHeaders(); //添加所有页眉
Headers heads = document.Headers; //获取该文档所有的页脚
Header hfirst = heads.first;
Header head1 = heads.even;
Header head2 = heads.odd; //添加logo
Paragraph p = head1.InsertParagraph("", false);
System.Net.WebRequest webreq = System.Net.WebRequest.Create("http://www.bc.ccoo.cn/logo/logo.gif");
System.Net.WebResponse webres = webreq.GetResponse();
Stream stream = webres.GetResponseStream();
MemoryStream stmMemory = new MemoryStream();
System.Drawing.Image myimg = System.Drawing.Image.FromStream(stream);
myimg.Save(stmMemory, myimg.RawFormat); // 保存你的图片到memorystream
stmMemory.Seek(, SeekOrigin.Begin);
Novacode.Image img = document.AddImage(stmMemory);
stream.Close(); //将图像插入到段落后面
Picture pic = img.CreatePicture(); //选择图像,并修改图像尺寸
pic.Rotation = ;
pic.Width = ;
pic.Height = ; //设置图片形状,并水平翻转图片
pic.SetPictureShape(BasicShapes.cube);
pic.FlipHorizontal = false;
p.InsertPicture(pic, );
p.InsertText(" 真诚为您服务");
p.AppendLine();
Paragraph ph2 = head2.InsertParagraph("", false); ph2.InsertPicture(pic, );
ph2.InsertText(" 真诚为您服务");
ph2.AppendLine(); Paragraph phfirst = hfirst.InsertParagraph("", false); phfirst.InsertPicture(pic, );
phfirst.UnderlineColor(System.Drawing.Color.Yellow);
phfirst.InsertText(" 真诚为您服务");
phfirst.AppendLine();
#endregion #region 生成文档中内容 foreach (Paragraph pbody in document.Paragraphs)
{
var bookmarks= pbody.GetBookmarks();
foreach (Bookmark item in bookmarks)
{
switch (item.Name)
{
case "MerchantName": //商家名称
item.Paragraph.ReplaceText("{MerchantName}", hsBookMark["MerchantName"].ToString());
break;
case "OperatingCommissioner"://运营专员
item.Paragraph.ReplaceText("{OperatingCommissioner}", hsBookMark["OperatingCommissioner"].ToString());
break;
case "OperatingTime"://运营时间
item.Paragraph.ReplaceText("{OperatingTime}", hsBookMark["OperatingTime"].ToString());
break;
case "IPNUM"://IP流量
item.Paragraph.ReplaceText("{IPNUM}", ""+hsBookMark["OperatingTime"].ToString());
break;
case "PVNUM"://PV
item.Paragraph.ReplaceText("{PVNUM}", "" + hsBookMark["OperatingTime"].ToString());
break;
case "FKNUM"://feek
item.Paragraph.ReplaceText("{FKNUM}", "" + hsBookMark["OperatingTime"].ToString());
break; }
}
}
List<Table> table = document.Tables;
Row newRow = table[].InsertRow();
newRow.Cells[].Paragraphs[].InsertText("&&&&&&&hhhHHHH00000", false);
newRow.Cells[].Paragraphs[].InsertText("&&&&&&&hhhHHHH111111111111",false); #endregion #region 生成页脚
document.AddFooters();//添加所有的页脚
Footers footers = document.Footers; //获取该文档所有的页脚
//获取文档第一页的页脚
Footer first = footers.first; //获取奇数页的页脚
Footer odd = footers.odd;
////设置不同页使用不同的页脚
document.DifferentFirstPage = true;
document.DifferentOddAndEvenPages = true;
//设置页脚的内容
Paragraph pf = first.InsertParagraph();
pf.Append("页脚内容替换成你的页脚内容"); Paragraph p2 = footers.even.InsertParagraph();
p2.Append("页脚内容替换成你的页脚内容"); Paragraph p3 = footers.odd.InsertParagraph();
p3.Append("页脚内容替换成你的页脚内容");
#endregion document.SaveAs(savePath);
}
}