poi填充word模板

前提:用的都是poi的包,
网上看到很多方式,我用了几个都没成功,自己倒腾了下、

1 word模板提前制作好
给填充的地方增加书签
在这里插入图片描述

2代码

package io.renren.common.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Bookmark;
import org.apache.poi.hwpf.usermodel.Bookmarks;
import org.apache.poi.hwpf.usermodel.Range;

/**
 * 使用POI,进行Word相关的操作
 *
 */
public class MSWordTool {

	
	private static void replaceDoc(Map<String, String> dataMap, HWPFDocument doc) throws IOException {
		if (doc != null) {
			Bookmarks bookmarks = doc.getBookmarks();
			int count = bookmarks.getBookmarksCount();
			for (int i = 0; i < count; i++) {
				Bookmark bookmark = null;
				try {
					bookmark = bookmarks.getBookmark(i);
				} catch (Exception e) {
					System.err.println(e.getCause());
					continue;
				}
				if (bookmark.getName() != null && bookmark.getName().startsWith("PO_")) {
					String bookmarkName = bookmark.getName().trim().toUpperCase();
					String data = dataMap.get(bookmarkName) != null?dataMap.get(bookmarkName).trim():"";
					Range range = new Range(bookmark.getStart(), bookmark.getEnd(), doc);
					range.insertBefore(data.replaceAll("\n", "\r"));//word的\n换行不起作用,需要转为\r
					System.out.println(bookmark.getStart()+":"+bookmark.getEnd());
				}
			}
		}
	}
	public static String insertWord(Map<String, String> dataMap){
		try {
			Date d = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			//你的模板路径
			String template = "C:\\apache-tomcat-8.0.33\\webapps\\Resume\\resumeimgs\\pdf\\shikai-model1.docx";
			File source = new File(template);
			//这里是自己的业务,我将部分数据作为了新word的名字
			String fileName = dataMap.get("PO_CANDIDATENAME")+"-"+dataMap.get("PO_ADDRESS")+sdf.format(d);
			
			//做一个新的word,将模板内容copy过来,
			String templateBak = "C:\\apache-tomcat-8.0.33\\webapps\\Resume\\resumeimgs\\pdf\\"+fileName+".docx";
			File source1 = new File(templateBak);
			
			//copy模板内容到新的word中
			copyFileUsingJava7Files(source, source1);
			
			
			File outfile = new File(templateBak);
			HWPFDocument doc = null;
			InputStream is = null;
			is = new FileInputStream(outfile);
			doc = new HWPFDocument(is);
			replaceDoc(dataMap, doc);
			OutputStream os = new FileOutputStream(templateBak);
			// 把doc输出到输出流中
			doc.write(os);
			closeStream(os);
			closeStream(is);
			WordToPdfUtil.officeToPDF(templateBak, templateBak.replaceAll(".docx", ".pdf"));
			return null;
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return null;
	}
	public static void main(String[] args) throws IOException {
		Map<String, String> dataMap = new HashMap<>();
		dataMap.put("PO_CANDIDATENAME", "张三");
		dataMap.put("PO_ADDRESS", "西安11");
		dataMap.put("PO_AGE", "32岁");
		dataMap.put("PO_EDUCATIONEXPERIENCE", "2");
		dataMap.put("PO_POSTION", "java");
		dataMap.put("PO_POSTIONTITLE", "java工程师");
		dataMap.put("PO_PROJECTEXPERIENCE", "3                         \n asass");
		dataMap.put("PO_REMARK", "啊33");
		dataMap.put("PO_SEX", "男");
		dataMap.put("PO_SPECIALTYDATE", "33啊");
		dataMap.put("PO_WORKEXPERIENCE", "33");
		insertWord(dataMap);
	}

	/**
	 * 关闭输入流
	 * 
	 * @param is
	 */
	private static void closeStream(InputStream is) {
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 关闭输出流
	 * 
	 * @param os
	 */
	private static void closeStream(OutputStream os) {
		if (os != null) {
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	private static void copyFileUsingJava7Files(File source, File dest)
			throws IOException {
		Files.copy(source.toPath(), dest.toPath());
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值