POI替换Word中的mark标记(指定字符串)

哈哈,这是鄙人在博客园的第一篇博客,以前都是在简书上码字,废话不多说,直接开工...

需求分析:工作中遇到的一个技术需求,需要用java代码操作Word,查找Word中的mark标记,然后进行替换,简而言之就是“替换word中的指定字符串”;

解决办法:可以用JACOB和POI来实现,下面我用的是POI操作。

-------------------------------我是分割线----------------------------------------

更通俗一点,就是进行全局替换,如下图,我们将字符串mark替换成hello,把字符串aaa替换成bbb:

替换后下图:

代码如下:

直接写成了一个工具类WordUtil:

这个类能通用doc和docx。

  1 package word;
  2 
  3 import java.io.FileInputStream;
  4 import java.io.FileNotFoundException;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 import java.util.List;
  8 import java.util.Map;
  9 import java.util.Map.Entry;
 10 import org.apache.poi.xwpf.usermodel.XWPFDocument;
 11 import org.apache.poi.xwpf.usermodel.XWPFParagraph;
 12 import org.apache.poi.xwpf.usermodel.XWPFRun;
 13 import org.apache.poi.xwpf.usermodel.XWPFTable;
 14 import org.apache.poi.xwpf.usermodel.XWPFTableCell;
 15 import org.apache.poi.xwpf.usermodel.XWPFTableRow;
 16 /**
 17  * 
 18  * @author zhoulian
 19  * @time    2018/6/8 15:30:22
 20  */
 21 public class WordUtil {
 22     
 23     private static FileInputStream in;
 24     private static FileOutputStream out;
 25     
 26     /**替换word中的字符串
 27      * 
 28      * @param filePath 文件路径
 29      * @param map    其中,key--替换的标记    value--替换的值
 30      */
 31     public  static void replaceAll(String filePath,Map<String,String> map){
 32         
 33         try {
 34             in = new FileInputStream(filePath);
 35             XWPFDocument doc = new XWPFDocument(in);
 36             
 37             //处理段落
 38             //------------------------------------------------------------------
 39             List<XWPFParagraph> paragraphs = doc.getParagraphs();
 40             for (XWPFParagraph paragraph : paragraphs) {
 41                 List<XWPFRun> runs = paragraph.getRuns();
 42                 for (XWPFRun run : runs) {
 43                     String text = run.getText(0);
 44                     if(text!=null){
 45                         boolean isSetText = false;
 46                         for (Entry<String, String> entry : map.entrySet()) {
 47                             String key = entry.getKey();
 48                             String value = entry.getValue();
 49                             if(text.indexOf(key)!=-1){
 50                                 isSetText = true;
 51                                 text = text.replaceAll(key, value);
 52                             }
 53                             if (isSetText) {
 54                                 run.setText(text, 0);
 55                             }
 56                         }
 57                         
 58                     }
 59                     
 60                 }
 61             }
 62             
 63             //------------------------------------------------------------------
 64             
 65             //处理表格
 66             //------------------------------------------------------------------
 67             List<XWPFTable> tables = doc.getTables();
 68             for (XWPFTable table : tables) {
 69                 List<XWPFTableRow> rows = table.getRows();
 70                 for (XWPFTableRow row : rows) {
 71                     List<XWPFTableCell> cells = row.getTableCells();
 72                     for (XWPFTableCell cell : cells) {
 73                         
 74                         String text = cell.getText();
 75                         if(text!=null){
 76                             for(Entry<String,String> entry:map.entrySet()){
 77                                 String key = entry.getKey();
 78                                 String value = entry.getValue();
 79                                 if(text.equals(key)){
 80                                     //删除原单元格值
 81                                     cell.removeParagraph(0);
 82                                     //设置新单元格的值
 83                                     cell.setText(value);
 84                                 }
 85                             }
 86                         }
 87                     }
 88                     
 89                 }
 90             }
 91             //------------------------------------------------------------------
 92             
 93             out = new FileOutputStream(filePath);  
 94             doc.write(out);  
 95             
 96         } catch (FileNotFoundException e) {
 97             e.printStackTrace();
 98         } catch (IOException e) {
 99             e.printStackTrace();
100         }finally{
101             try {
102                 in.close();
103                 out.close();  
104             } catch (IOException e) {
105                 e.printStackTrace();
106             }
107         }
108         
109         
110         
111     }
112     
113 
114 }

main方法测试如下:

 1 package word;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 public class wordTest {
 7 
 8     public static void main(String[] args) {
 9         String filePath = "C:\\Users\\hp\\Desktop\\temp\\ceshi.docx";
10         Map<String,String> map = new HashMap<String,String>();
11         map.put("mark", "hello");
12         map.put("aaa", "bbb");
13         WordUtil.replaceAll(filePath, map);
14         
15     }
16 
17 }

 至于JACOB操作Word,请看另一片文章:

直通车:https://www.cnblogs.com/DreamDrive/p/7090284.html

猜你喜欢

转载自www.cnblogs.com/zhoulian/p/9156605.html