XWPFDocument读取word并修改发送

isFile为文件路径,data为一个map对象,data.put("userName",userName);那么就需要在文档中写${userName}就可以输出到文档了

package com.jeeplus.modules.stock;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;


public class FileAlterInfo {
	/**
	 * 实现对word读取和修改操作
	 *
	 * @param filePath
	 *            word模板路径和名称
	 * @param map
	 *            待填充的数据,从数据库读取
	 * @throws IOException
	 * @throws InvalidFormatException
	 */
	public static void readwriteWord(String inFile, Map data) throws InvalidFormatException, IOException {
		InputStream is = new FileInputStream(inFile);
		XWPFDocument document;
		try {
			document = new XWPFDocument(OPCPackage.open(is));
			// 替换段落里面的变量
			replaceInPara(document, data);
			OutputStream os = new FileOutputStream(inFile);
			document.write(os);
			close(os);
			close(is);
		} catch (Exception e) {

			e.printStackTrace();
		}

	}

	/**
	 * 替换段落里面的变量
	 *
	 * @param doc
	 *            要替换的文档
	 * @param params
	 *            参数
	 */
	private static void replaceInPara(XWPFDocument doc, Map params) {
		Iterator iterator = doc.getParagraphsIterator();
		XWPFParagraph para;
		while (iterator.hasNext()) {
			para = iterator.next();
			replaceInPara(para, params);
		}
	}

	/**
	 * 替换段落里面的变量
	 *
	 * @param para
	 *            要替换的段落
	 * @param params
	 *            参数
	 */
	private static void replaceInPara(XWPFParagraph para, Map params) {
		List runs;
		Matcher matcher;
		String runText = "";

			if (matcher(para.getParagraphText()).find()) {
				runs = para.getRuns();
				if (runs.size() > 0) {
					int j = runs.size();
					for (int i = 0; i < j; i++) {
						XWPFRun run = runs.get(0);
					String i1 = run.toString();
						runText += i1;
						para.removeRun(0);

					}

				}
			System.out.println(runText);
				matcher = matcher(runText);

				if (matcher.find()) {
					while ((matcher = matcher(runText)).find()) {
						runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));
					}
					// 直接调用XWPFRun的setText()方法设置文本时,在底层会重新创建一个XWPFRun,把文本附加在当前文本后面,
					// 所以我们不能直接设值,需要先删除当前run,然后再自己手动插入一个新的run。
					para.insertNewRun(0).setText(runText);
				}
			}

	}

	/**
	 * 正则匹配字符串
	 *
	 * @param str
	 * @return
	 */
	private static Matcher matcher(String str) {
		Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(str);
		return matcher;
	}

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

	/**
	 * 关闭输出流
	 *
	 * @param os
	 */
	private static void close(OutputStream os) {
		if (os != null) {
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}







你可能感兴趣的:(java工具类)