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);
}
}

2.docx下载

最新文章

  1. Hibernate 错题分析
  2. html中相似的标签、属性的区别
  3. C#创建自定义配置节
  4. Jquery easyui datagrid 删除多行问题
  5. php全角字符转换为半角函数
  6. 网络开发库从libuv说到epoll
  7. 2014-11-26----css的简介
  8. AutoFac初探
  9. CSS3之3D效果中的transform运用
  10. 扩展BaseAdapter实现不存储列表项的ListView
  11. waypoints
  12. Hadoop版本选择
  13. 如何在Microsoft Word里面插入图片作为背景/封面?
  14. combine_lat_dirs.sh
  15. Windows 远程桌面剪贴板失效的处理办法
  16. 1.3 java8新特性总结
  17. 微软BI SSIS 2012 ETL 控件与案例精讲面试 200 问(SSIS 面试题,ETL 面试题)
  18. 手把手教你用MATLAB画灰度直方图
  19. WPF布局管理之Canvas、InkCanvas (转)
  20. 新建linux服务器初始化操作

热门文章

  1. java_网络编程之BS(web案例)
  2. hdu5952 Counting Cliques 技巧dfs
  3. 解决Spring Boot Configuration Annotation Processor not found in classpath
  4. [JZOJ5232] 【NOIP2017模拟A组模拟8.5】带权排序
  5. Apache添加多端口及实现单ip多端口映射的方法
  6. thinkphp 子查询
  7. 0906NOIP模拟测试赛后总结
  8. duilib教程之duilib入门简明教程2.VS环境配置
  9. 恭喜"微微软"喜当爹,Github嫁入豪门。
  10. 07_jQuery对象初识(五)事件(非常重要)