Chinaunix首页 | 论坛 | 博客
  • 博客访问: 604377
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2022-01-04 11:08:57


点击(此处)折叠或打开


  1. 定义一个工具类wordUtil:

  2. package com.keji.service.utils;

  3. import org.apache.poi.xwpf.usermodel.*;

  4. import java.io.FileInputStream;
  5. import java.io.InputStream;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;

  11. //from fhadmin.cn
  12. public class docUtil {

  13.     public static XWPFDocument replaceDoc(String srcPath, Map<String, Object> param) {
  14.         try {
  15.             // 读取word模板
  16.             InputStream fis = new FileInputStream(srcPath);
  17.             XWPFDocument doc = new XWPFDocument(fis);
  18.             //处理段落
  19.             List<XWPFParagraph> paragraphList = doc.getParagraphs();
  20.             processParagraph(paragraphList,doc,param);
  21.             //处理表格
  22.             Iterator<XWPFTable> it = doc.getTablesIterator();
  23.             while (it.hasNext()) {
  24.                 XWPFTable table = it.next();
  25.                 List<XWPFTableRow> rows = table.getRows();
  26.                 for (XWPFTableRow row : rows) {
  27.                     List<XWPFTableCell> cells = row.getTableCells();
  28.                     for (XWPFTableCell cell : cells) {
  29.                         List<XWPFParagraph> paragraphListTable = cell.getParagraphs();
  30.                         processParagraph(paragraphListTable, doc, param);
  31.                     }
  32.                 }
  33.             }
  34.             return doc;
  35.         }
  36.         catch (Exception e) {
  37.             e.printStackTrace();
  38.             return null;
  39.         }
  40.     }


  41.     public static void processParagraph(List<XWPFParagraph> paragraphList, XWPFDocument doc, Map<String, Object> param) {
  42.         if(paragraphList != null && paragraphList.size() > 0) {
  43.             for (XWPFParagraph paragraph : paragraphList) {
  44.                 List<XWPFRun> runs = paragraph.getRuns();
  45.                 for (XWPFRun run : runs) {
  46.                     String runString = run.toString();
  47.                     Matcher m = Pattern.compile("\\$\\{(.*?)}").matcher(runString);
  48.                     if (m.find()) {
  49.                         boolean isSetText = false;
  50.                         for (Map.Entry<String, Object> entry : param.entrySet()) {
  51.                             String key = entry.getKey();
  52.                             if (runString.indexOf(key) != -1) {
  53.                                 isSetText = true;
  54.                                 Object value = entry.getValue();
  55.                                 if (entry.getValue() == null || entry.getValue().equals("")){
  56.                                     value = "未填写";
  57.                                 }
  58.                                 runString = runString.replace(key, value.toString());
  59.                             }
  60.                         }
  61.                         if (isSetText) {
  62.                             run.setText(runString, 0);
  63.                         }
  64.                     }
  65.                 }
  66.             }
  67.         }
  68.     }
  69. }




  70. Map<String, Object> map=new HashMap<String, Object>();

  71. //文档里面的列名为${name}和${sex}
  72. map.put("${name}", "姓名");
  73. map.put("${sex}", "性别");

  74. //文件位置和map
  75. XWPFDocument doc = docUtil.replaceDoc("D:\\word\\month.docx", map);

  76. //文件保存位置
  77. OutputStream os = new FileOutputStream("D:\\tttt.doc");
  78.             doc.write(os);
  79.             os.close();
  80.             System.out.println("输出成功!");


  81. 最后会有一个奇怪的bug,word文档里的${name}如果是手打上去就可以用,如果是复制上去就会被识别为三个字符串,也就是不能替换,当然也有解决方法,可以把全文复制到win自带的写字板(不需要调任何格式),
  82. 然后把原文删了复制回来就行了,复制回来之后不能再改字段了,改了还会出问题,但是可以改样式什么的。这个问题不一定会

阅读(568) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~