有一word文档表格

形如:

姓名 ${name}

电话 ${tel}

从数据库读取记录替换上述变量

import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow; public class DOCWriter {
public static void searchAndReplace(String srcPath, String destPath,
Map<String, String> map) {
try {
XWPFDocument document = new XWPFDocument(POIXMLDocument
.openPackage(srcPath));
Iterator it = document.getTablesIterator();
while (it.hasNext()) {
XWPFTable table = (XWPFTable) it.next();
int rcount = table.getNumberOfRows();
for (int i = 0; i < rcount; i++) {
XWPFTableRow row = table.getRow(i);
List<XWPFTableCell> cells = row.getTableCells();
for (XWPFTableCell cell : cells) {
for (Entry<String, String> e : map.entrySet()) {
if (cell.getText().equals(e.getKey())) {
cell.removeParagraph(0);
cell.setText(e.getValue());
}
}
}
}
}
FileOutputStream outStream = null;
outStream = new FileOutputStream(destPath);
document.write(outStream);
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) throws Exception {
HashMap map = new HashMap();
map.put("${name}", "王五");
map.put("${tel}", "8886666");
String srcPath = "c:\\1.docx";
String destPath = "c:\\2.doc";
searchAndReplace(srcPath, destPath, map);
}
}

以上方法,直接考入导入包后直接可运行

本文转自:http://www.geekcome.com/content-10-3567-1.html

poi读写word模板 / java生成word文档的更多相关文章

  1. c语言小程序以及java生成注释文档方法

    c语言小程序:sizeof和strlen() sizeof运算符以字节为单位给出数据的大小,strlen()函数以字符为单位给出字符串的长度,字符和字节不是一回事. char类型用于存储字母和标点符号 ...

  2. java生成excel文档

    要做一个后台自动化,要先预先生成一份文档,以下内容生成了文档 首先下载jxl.jar包,下载地址:http://download.csdn.net/detail/prstaxy/4469935 1.生 ...

  3. Chimm.Excel —— 使用Java 操作 excel 模板文件生成 excel 文档

    Chimm.Excel -- 设置模板,填充数据,就完事儿了~ _____ _ _ _____ _ / __ \ | (_) | ___| | | | / \/ |__ _ _ __ ___ _ __ ...

  4. C# 通过word模板动态生成Word

    object oMissing = System.Reflection.Missing.Value; Word._Application oWord = new Word.Application(); ...

  5. Java生成PDF文档(表格、列表、添加图片等)

    需要的两个包及下载地址: (1)iText.jar:http://download.csdn.net/source/296416 (2)iTextAsian.jar(用来进行中文的转换):http:/ ...

  6. java生成API文档

    1.选择项目右键-Export\javadoc 2.选择生成工具在jdk安装目录下jdk\bin\javadoc.exe 3.在Eclipse里 export 选 JavaDoc,在向导的最后一页的E ...

  7. Java生成 Word文档的并打印解决方案

    户要求用程序生成标准的word文档,要能打印,而且不能变形,以前用过很多解决方案,都在客户严格要求下牺牲的无比惨烈. POI读word文档还行,写文档实在不敢恭维,复杂的样式很难控制不提,想象一下一个 ...

  8. 使用Java生成word文档(附源码)

    当我们使用Java生成word文档时,通常首先会想到iText和POI,这是因为我们习惯了使用这两种方法操作Excel,自然而然的也想使用这种生成word文档.但是当我们需要动态生成word时,通常不 ...

  9. Java生成word文档

    itext-rtf-2.1.7.jar,下载地址:http://download.csdn.net/detail/xuxu198899223/7717727 itext-2.1.7.jar 下载地址: ...

随机推荐

  1. spring mvc 406 (Not Acceptable) json转换错误

    spring mvc通过@RequestMapping("/register")和@ResponseBody返回json格式的字符串时出现如下异常: The resource id ...

  2. maven web项目build失败

    通过maven build发布web项目到tomcat时报如下异常: [INFO] ---------------------------------------------------------- ...

  3. Avisynth + DirectShow + WebCamera 实现Avisynth脚本访问摄像头

    准备工作:需要以下三种软件 1.Avisynth_258 安装文件和源码下载地址(Avisynth_258.exe 4.2 MB) http://sourceforge.net/projects/av ...

  4. 自定义Notification

    private static void updateProgressNotification(Context cxt, int appsCount, int percent, String appNa ...

  5. LINUX_source

    Be careful! ./ and source are not quite the same. ./script runs the script as an executable file, la ...

  6. cf455a(简单dp)

    题意:给出一个长度为n的数列,元素为a1, a2, ...an:删除ai,ai+1,ai-1 可以得到ai积分,输出最多可以得到多少积分: 题解:开一个数组a存取数列,a[i]表示元素i的个数,所以删 ...

  7. zabbix 监控客户端数据库 zabbix客户端

    [root@zabbix客户端 zabbix]# grep -v "^$" etc/zabbix_agentd.conf|grep -v "^#" LogFil ...

  8. 搭建邮局(邮件服务器) - hmailserver

    1.查看服务器mx是否解析成功 nslookup  set type=mx   2.hmailserver服务器 smtp设置     3.foxmail 设置   4.使用webmail(after ...

  9. Java Hour 41 Maven ( 3 )

    有句名言,叫做10000小时成为某一个领域的专家.姑且不辩论这句话是否正确,让我们到达10000小时的时候再回头来看吧. 41.1 m2eclipse 简介 m2eclipse 是一款开源工具,为ec ...

  10. Android安卓知识点

    1 包名是唯一标识apk的记号,相当于公民身份证号. 2 ADB是Android Debug Brigde 的英文缩写,意思是Android程序调试桥,使用SDK自带的工具可以对Android模拟器或 ...