使用NPOI按照word模板文件生成新的word文件
/// <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文件的更多相关文章
- C#读取Word模板替换相应的字符串(标签)生成新的Word
在平常工作中,生成word的方式主要是C#读取html的模板文件处理之后保存为.doc文件,这样的好处是方便,快捷,能满足大部分的需求.不过有些特殊的需求并不能满足,如要生成的Word为一个表格,只是 ...
- MiniWord .NET Word模板引擎,藉由Word模板和数据简单、快速生成文件。
Github / Gitee QQ群(1群) : 813100564 / QQ群(2群) : 579033769 介绍 MiniWord .NET Word模板引擎,藉由Word模板和数据简单.快速生 ...
- windows通过pfx文件生成key、crt文件
nginx代理的时候,需要填写证书的crt跟rsa文件路径,通过iis导出的证书是pfx文件(不知道nginx能不能直接用pfx文件,没有查看过相关资料),所以要通过pfx文件生成crt.rsa文件. ...
- php根据word模板生成新的word文件
原文地址:http://www.niu12.com/article/16 php使用phpword将word内容变量替换 a.安装phpword composer require phpoffice/ ...
- java代码操作word模板并生成PDF
这个博客自己现在没时间写,等后面有时间了,自己再写. 这中需求是在实际的项目开发中是会经常遇到的. 下面我们先从简单入手一步一步开始. 1.首先,使用word创建一个6行两列的表格. 点击插入-6行2 ...
- 依据word模板批量生成试卷
java-word-MassProduction 目录 使用方法 开发流程 一.使用方法 1.制造题库所需Word模板 需要填充数据的地方使用 ${pid} 代替. 将这个word选择另存为,保存格式 ...
- 依据BOM和已经存在的文件生成其他种类的文件
在BOM中记录中有物料编码,物料名称,物料规格等,而且依据BOM已经生成了一些的文件,如采购规格书,这个时候需要生成相应的检验规格书模板,可以使用下面的VBA代码,具体代码如下: Function I ...
- POI3.10读取Excel模板填充数据后生成新的Excel文件
private final DecimalFormat df = new DecimalFormat("#0.00"); public void test(){ String fi ...
- 使用T4模板同时生成多个类文件
代码: <#@ template language="C#" debug="false" hostspecific="true"#&g ...
随机推荐
- linux中执行shell命令的几种常用方法
1 切换到shell脚本所在目录执行shell脚本: cd /test/shell ./test.sh 2 以绝对路径的方式执行shell脚本: /test/shell/test.sh 3 直接使用b ...
- PAT 1112 Stucked Keyboard
1112 Stucked Keyboard (20 分) On a broken keyboard, some of the keys are always stucked. So when yo ...
- js各种特效轮播图,选项卡,放大镜,窗口拖拽,楼层跳转
// 透明度轮播图 // img:轮播图片 // dot:轮播点 // lbtn:左箭头 // rbtn:右箭头 // banner:轮播盒子 // active:轮播点选中效果类名 // time: ...
- python 爬虫 记录
python3 爬虫需要安装:requests,beautifulsoup4,html5lib 带有中文的需要这样写,要不然就会出现乱码 html = response.content.decode( ...
- 生成器yield
#!/usr/bin/python#coding=utf-8'''Created on 2017年11月23日 from home @author: James zhan ''' def counte ...
- 关于规范NOIP试题管理办法的通知
由CCF主办的NOIP赛事举行在即,保密起见,现将有关规定发给各省赛区组织单位. 1.NOI各省组织单位负责试题保密工作. 2.NOIP初赛试卷为纸质版,复赛试卷为电子版. 3.在初赛进行中,如有选手 ...
- CodeForces - 1015 D.Walking Between Houses
Description Natasha is planning an expedition to Mars for nn people. One of the important tasks is t ...
- jdbc从基础到优化
package com.xk.demotest.tools; import java.io.IOException; import java.io.InputStream; import java.s ...
- 读取磁盘:CHS方式
读取磁盘:CHS方式 BIOS读取磁盘 读取磁盘也是调用BIOS: 中断命令: INT 13H 读取扇区的入口参数为 AH = 02H 功能参数,读取扇区 AL = 扇区数 CH = 柱面 CL = ...
- 第一章01:熟悉java,发展历史
1. java由来: 原公司,sun,后来被oracle\甲骨文公司收购 java,主要用于开发互联网软件,例如:QQ.迅雷.淘宝.京东