package com.test;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.FieldsDocumentPart;
import org.apache.poi.hwpf.usermodel.Field;
import org.apache.poi.hwpf.usermodel.Fields;
import org.apache.poi.hwpf.usermodel.Range;
public class MSWordPoi4 {
/**
* @param args
*/
public static void main(String[] args)
{
// Map<String, String> map = new HashMap<String, String>();
// map.put("${yearstart}", "2014");
// map.put("${monthstart}", "01");
// map.put("${daystart}", "02");
// map.put("${yearend}", "2017");
// map.put("${monthend}", "07");
// map.put("${dayend}", "07");
// map.put("${username}", "乙方"); //用户名
// map.put("${idcard}", "333333333333"); //身份证号
// map.put("${householdaddress}", "户籍地址"); //户籍地址
// map.put("${localaddress}", "联系地址"); //联系地址
// map.put("${zipcode}", "666666"); //邮政编码
// map.put("${begindate}", "2014年01月02日"); //合同开始日期
// map.put("${enddate}", "2017年07月07日"); //合同结束日期
// map.put("${mobile}", "151045222233"); //联系电话
// map.put("${userpost}", "java工程师"); //岗位
// map.put("${orgcode}", "综合部"); //所在部门
// map.put("${userno}", "43000"); //工号
// map.put("${email}", "[email protected]"); //工号
// map.put("${systemuserid}", "yi.fang"); //登录名
// String srcPath = "D:\\temp\\SFIT-IT资源管理系统-方案建议书-m1.doc";
// readwriteWord(srcPath, map);
//
// String abc = "20140301";
// System.out.println(abc.substring(6, 8));
String iccard = Long.toHexString(Long.valueOf("123"));
}
/**
* 实现对word读取和修改操作
*
* @param filePath
* word模板路径和名称
* @param map
* 待填充的数据,从数据库读取
*/
public static void readwriteWord(String filePath, Map<String, String> map)
{
FileInputStream in = null;
try
{
in = new FileInputStream(new File(filePath));
}
catch (FileNotFoundException e1)
{
e1.printStackTrace();
}
HWPFDocument hdt = null;
try
{
hdt = new HWPFDocument(in);
}
catch (IOException e1)
{
e1.printStackTrace();
}
//读取word文本内容
Range range = hdt.getRange();
// 替换文本内容 ````````````
for (Map.Entry<String, String> entry : map.entrySet())
{
range.replaceText(entry.getKey(), entry.getValue());
}
String fileName = "" + System.currentTimeMillis();
fileName += ".doc";
FileOutputStream out = null;
try
{
out = new FileOutputStream("D:\\temp\\" + fileName, true);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
try
{
hdt.write(out);
}
catch (IOException e)
{
e.printStackTrace();
}
// 输出字节流
try
{
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
doc文件替换内容
猜你喜欢
转载自137015797.iteye.com/blog/2358663
今日推荐
周排行