有一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. .html和.htm的区别

    很多人会认为网页扩展名html和htm是等同的,但事实上他们还是有区别的. 包含HTML内容的文件最常用的扩展名是.html,但是像DOS这样的旧操作系统限制扩展名为最多3个字符,所以.htm扩展名也 ...

  2. js:判断对象是否为空

    var list = []; var param = {}; ....... var hasProp = false; for (var prop in param){ hasProp = true; ...

  3. CodeForces - 405A

    Gravity Flip Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit ...

  4. HDU1695 GCD (欧拉函数+容斥原理)

    F - GCD Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  5. 昨天晚上也弄不清楚是自己密码被盗了还是由于ip冲突

    所以还是尽量要相信自己所见到的,今天上午是安卓课程,说实话,昨天晚上都是2:30睡的,现在硬是要把时间待这么晚才回去睡,是因为我想尽快入睡,昨天晚上就是眼睛都有点睁不开了,所以就睡得很快,但是早上也是 ...

  6. mysql:表注释和字段注释

    mysql:表注释和字段注释 1 创建表的时候写注释 create table test1 ( field_name int comment '字段的注释' )comment='表的注释'; 2 修改 ...

  7. Git Server & Git Hook

    http://ju.outofmemory.cn/entry/16893 我喜欢 github,我现在的个人代码全部是托管在上面了,但是一些公司或者某些项目不适合放入github中,你希望能有一个完全 ...

  8. Gym 100971D Laying Cables 单调栈

    Description One-dimensional country has n cities, the i-th of which is located at the point xi and h ...

  9. c# 作业1

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. HBase伪分布式环境下,HBase的API操作,遇到的问题

    在hadoop2.5.2伪分布式上,安装了hbase1.0.1.1的伪分布式 利用HBase的API创建个testapi的表时,提示  Exception in thread "main&q ...