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下载

Docx 生成word文档的更多相关文章

  1. Docx 生成word文档二

    /// <summary> /// 生产word 文档 /// </summary> public class GenerateWord { /// <summary&g ...

  2. Aspose.Words简单生成word文档

    Aspose.Words简单生成word文档 Aspose.Words.Document doc = new Aspose.Words.Document(); Aspose.Words.Documen ...

  3. 将HTML导出生成word文档

    前言: 项目开发中遇到了需要将HTML页面的内容导出为一个word文档,所以有了这边随笔. 当然,项目开发又时间有点紧迫,第一时间想到的是用插件,所以百度了下.下面就介绍两个导出word文档的方法. ...

  4. POI生成word文档完整案例及讲解

    一,网上的API讲解 其实POI的生成Word文档的规则就是先把获取到的数据转成xml格式的数据,然后通过xpath解析表单式的应用取值,判断等等,然后在把取到的值放到word文档中,最后在输出来. ...

  5. PoiDocxDemo【Android将表单数据生成Word文档的方案之二(基于Poi4.0.0),目前只能java生成】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这个是<PoiDemo[Android将表单数据生成Word文档的方案之二(基于Poi4.0.0)]>的扩展,上一篇是根 ...

  6. PoiDemo【Android将表单数据生成Word文档的方案之二(基于Poi4.0.0)】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用Poi实现android中根据模板文件生成Word文档的功能.这里的模板文件是doc文件.如果模板文件是docx文件的话,请阅读 ...

  7. Android根据word模板文档将表单数据生成word文档的方案整理

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 尝试的方案包括以下几种: freemarker 只能在java项目上运行,无法在Android项目上运行: 参考资料:<Fre ...

  8. POI 生成 word 文档 简单版(包括文字、表格、图片、字体样式设置等)

      POI 生成word 文档 一般有两种方法: ① word模板 生成word 文档 : ② 写代码直接生成 word 文档: 我这里演示的是第二种方法,即写代码生成 word文档,不多说废话,直接 ...

  9. 使用PHPWord生成word文档

    有时我们需要把网页内容保存为Word文档格式,以供其他人员查看和编辑.PHPWord是一个用纯PHP编写的库,使用PHPWord可以轻松处理word文档内容,生成你想要的word文档. 下载源码 安装 ...

随机推荐

  1. 机器学习-线性回归算法(单变量)Linear Regression with One Variable

    1 线性回归算法 http://www.cnblogs.com/wangxin37/p/8297988.html 回归一词指的是,我们根据之前的数据预测出一个准确的输出值,对于这个例子就是价格,回归= ...

  2. cocos2dx触摸响应

      Layer其实继承了触控的接口. 所以只需要重写一些函数即可.   在helloword类中重写:     virtual bool init();     /** Callback functi ...

  3. nginx i.com.conf

    server { listen 9090; server_name i.com; root /Users/chong/Documents/www; # Load configuration files ...

  4. JavaScript特效源码(3、菜单特效)

    1.左键点击显示菜单 左键弹出式菜单[推荐][修改显示的文字及链接即可][共2步] ====1.将以下代码加入HEML的<head></head>之间: <style t ...

  5. JavaScript数组的2种定义方式

    JavaScript中没有数组类型,JavaScript中数组是以内置对象的形式存在的. 数组是存储多个值的集合(仓库). JS中定义数组的2种方式: 1.使用new Array()构造函数定义数组 ...

  6. 【转载】objective-c强引用与弱引用

    形象比喻蛮好玩的^_^    __weak 和 __strong 会出现在声明中   默认情况下,一个指针都会使用 __strong 属性,表明这是一个强引用.这意味着,只要引用存在,对象就不能被销毁 ...

  7. SQLite C++ Wrapper 是一个 C++ 语言对 SQLite 的最小封装包。

    SQLite C++ Wrapper 是一个 C++ 语言对 SQLite 的最小封装包. 示例代码1:   #include <string> #include <iostream ...

  8. RabbitMQ代码操作之AmqpAdmin和RabbitListener

    AmqpAdmin:RabbitMQ系统管理功能组件(可以创建exchange,queue,Binding) @Test public void createExchange(){ //创建交换器 / ...

  9. 《DSP using MATLAB》Problem 8.21

    代码: %% ------------------------------------------------------------------------ %% Output Info about ...

  10. 关于Spring Cloud Feign的一些记录!

    学习Spring Cloud Feign过程中,相关资料都会反复强调:微服务调用的话(@FeignClient)  客户端方法的返回值和服务端方法的返回值还有方法名之类的都是要求一致的! 关于方法名是 ...