/// <summary>
/// 按照word模板文件 生成新word文件
/// </summary>
/// <param name="tempFile">模板文件路径</param>
/// <param name="saveFile">生成文件路径</param>
/// <param name="billRegister">议案登记</param>
public void CreateFileByTemplate(string tempFile,string saveFile, L1BillRegister billRegister)
{
using (FileStream stream = File.OpenRead(tempFile))
{
XWPFDocument doc = new XWPFDocument(stream); //遍历段落
foreach (var para in doc.Paragraphs)
{
ReplaceKey(para, billRegister);
} //遍历表格
foreach (var table in doc.Tables)
{
foreach (var row in table.Rows)
{
foreach (var cell in row.GetTableCells())
{
foreach (var para in cell.Paragraphs)
{
ReplaceKey(para, billRegister);
}
}
}
}
FileStream out1 = new FileStream(saveFile, FileMode.Create);
doc.Write(out1);
out1.Close();
}
} /// <summary>
/// 对模板中的值进行替换
/// </summary>
/// <param name="para">word文档对象</param>
/// <param name="billRegister">议案登记对象</param>
private static void ReplaceKey(XWPFParagraph para, L1BillRegister billRegister)
{
string text = "";
foreach (var run in para.Runs)
{
text = run.ToString();
if (text.Contains("a"))
{
run.SetText(billRegister.BillName, 0);
}
else
{
run.SetText(text, 0);
}
}
} //调用
string tempFile = System.Web.HttpContext.Current.Server.MapPath("~/weboffice/tempfiles/meetSolutions.docx");
string saveFile = System.Web.HttpContext.Current.Server.MapPath("~/weboffice/tempfiles/test/" + billRegister.BillId + ".doc");
this.CreateFileByTemplate(tempFile,saveFile, billRegister);

  

使用NPOI按照word模板文件生成新的word文件的更多相关文章

  1. C#读取Word模板替换相应的字符串(标签)生成新的Word

    在平常工作中,生成word的方式主要是C#读取html的模板文件处理之后保存为.doc文件,这样的好处是方便,快捷,能满足大部分的需求.不过有些特殊的需求并不能满足,如要生成的Word为一个表格,只是 ...

  2. MiniWord .NET Word模板引擎,藉由Word模板和数据简单、快速生成文件。

    Github / Gitee QQ群(1群) : 813100564 / QQ群(2群) : 579033769 介绍 MiniWord .NET Word模板引擎,藉由Word模板和数据简单.快速生 ...

  3. windows通过pfx文件生成key、crt文件

    nginx代理的时候,需要填写证书的crt跟rsa文件路径,通过iis导出的证书是pfx文件(不知道nginx能不能直接用pfx文件,没有查看过相关资料),所以要通过pfx文件生成crt.rsa文件. ...

  4. php根据word模板生成新的word文件

    原文地址:http://www.niu12.com/article/16 php使用phpword将word内容变量替换 a.安装phpword composer require phpoffice/ ...

  5. java代码操作word模板并生成PDF

    这个博客自己现在没时间写,等后面有时间了,自己再写. 这中需求是在实际的项目开发中是会经常遇到的. 下面我们先从简单入手一步一步开始. 1.首先,使用word创建一个6行两列的表格. 点击插入-6行2 ...

  6. 依据word模板批量生成试卷

    java-word-MassProduction 目录 使用方法 开发流程 一.使用方法 1.制造题库所需Word模板 需要填充数据的地方使用 ${pid} 代替. 将这个word选择另存为,保存格式 ...

  7. 依据BOM和已经存在的文件生成其他种类的文件

    在BOM中记录中有物料编码,物料名称,物料规格等,而且依据BOM已经生成了一些的文件,如采购规格书,这个时候需要生成相应的检验规格书模板,可以使用下面的VBA代码,具体代码如下: Function I ...

  8. POI3.10读取Excel模板填充数据后生成新的Excel文件

    private final DecimalFormat df = new DecimalFormat("#0.00"); public void test(){ String fi ...

  9. 使用T4模板同时生成多个类文件

    代码: <#@ template language="C#" debug="false" hostspecific="true"#&g ...

随机推荐

  1. Archiva 2.2.3 安装运行的时候出现协议版本错误

    在 Archiva 安装成功后运行的时候出现协议版本错误: Caused by: javax.net.ssl.SSLException: Received fatal alert: protocol_ ...

  2. C# 关键字this用法

    1.this代表当前类的实例对象 public class Test { public string Name{get;set;} public void TestChange(string strN ...

  3. Loadrunner常见的乱码问题

    1.录制的脚本出现了乱码 录制的时候出现乱码,如果不影响回放,我们可以不管它,如果影响回放结果,我们可以使用以下方法解决:     1)更改录制选项         选择菜单栏Tools---> ...

  4. analyse web.xml of hello1

    web.xml注释分析: 补充: 一.XML文档的xmlns.xmlns:xsi和xsi:schemaLocation (参考博客:https://www.cnblogs.com/osttwz/p/6 ...

  5. linux服务管理 服务启动和自启动

    服务启动和自启动 '服务启动' 就是在当前系统中让服务运行,并提供功能 '服务自启动' 自启动是指服务在系统开机或重启动之后,随着启动系统的启动而自动启动服务 服务自启动 [root@ssgao198 ...

  6. 跟随我在oracle学习php(5)

    框架(把一个页面引入当前页面 易维护 扩展 复用)<iframe src=”” frameborder=“”> 格式:iframe <frameset> <frame&g ...

  7. webpack配置接口路径

    比如在webpack.config.js中的plugins中加入 new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify(' ...

  8. 二十三. Python基础(23)--经典类和新式类

    二十三. Python基础(23)--经典类和新式类 ●知识框架   ●接口类&抽象类的实现 # 接口类&抽象类的实现 #①抛出异常法 class Parent(object):    ...

  9. Binary Tree Maximum Node

    Find the maximum node in a binary tree, return the node. Example Given a binary tree: 1 / \ -5 2 / \ ...

  10. Java 作业6

    我总算,又双叒叕拾起了Java,啊! 1.编写一个JApplet程序,包含一个JLabel对象,并显示用户的姓名. package experiment; import java.awt.Border ...