哈哈,这是鄙人在博客园的第一篇博客,以前都是在简书上码字,废话不多说,直接开工...
需求分析:工作中遇到的一个技术需求,需要用java代码操作Word,查找Word中的mark标记,然后进行替换,简而言之就是“替换word中的指定字符串”;
解决办法:可以用JACOB和POI来实现,下面我用的是POI操作。
用poi必须要用到Apache的jar包,我用的是最新的poi3.17
链接: https://pan.baidu.com/s/1LfW9JuZdG0IrMz6PVQCdeA 密码: 3t57
-------------------------------我是分割线----------------------------------------
更通俗一点,就是进行全局替换,如下图,我们将字符串mark替换成hello,把字符串aaa替换成bbb:

替换后下图:

代码如下:
直接写成了一个工具类WordUtil:
这个类能通用doc和docx。
1 1 package word; 2 2 3 3 import java.io.FileInputStream; 4 4 import java.io.FileNotFoundException; 5 5 import java.io.FileOutputStream; 6 6 import java.io.IOException; 7 7 import java.util.List; 8 8 import java.util.Map; 9 9 import java.util.Map.Entry; 10 10 import org.apache.poi.xwpf.usermodel.XWPFDocument; 11 11 import org.apache.poi.xwpf.usermodel.XWPFParagraph; 12 12 import org.apache.poi.xwpf.usermodel.XWPFRun; 13 13 import org.apache.poi.xwpf.usermodel.XWPFTable; 14 14 import org.apache.poi.xwpf.usermodel.XWPFTableCell; 15 15 import org.apache.poi.xwpf.usermodel.XWPFTableRow; 16 16 /** 17 17 * 18 18 * @author zhoulian 19 19 * @time 2018/6/8 15:30:22 20 20 */ 21 21 public class WordUtil { 22 22 23 23 private static FileInputStream in; 24 24 private static FileOutputStream out; 25 25 26 26 /**替换word中的字符串 27 27 * 28 28 * @param filePath 文件路径 29 29 * @param map 其中,key--替换的标记 value--替换的值 30 30 */ 31 31 public static void replaceAll(String filePath,Map<String,String> map){ 32 32 33 33 try { 34 34 in = new FileInputStream(filePath); 35 35 XWPFDocument doc = new XWPFDocument(in); 36 36 37 37 //处理段落 38 38 //------------------------------------------------------------------ 39 39 List<XWPFParagraph> paragraphs = doc.getParagraphs(); 40 40 for (XWPFParagraph paragraph : paragraphs) { 41 41 List<XWPFRun> runs = paragraph.getRuns(); 42 42 for (XWPFRun run : runs) { 43 43 String text = run.getText(0); 44 44 if(text!=null){ 45 45 boolean isSetText = false; 46 46 for (Entry<String, String> entry : map.entrySet()) { 47 47 String key = entry.getKey(); 48 48 String value = entry.getValue(); 49 49 if(text.indexOf(key)!=-1){ 50 50 isSetText = true; 51 51 text = text.replaceAll(key, value); 52 52 } 53 53 if (isSetText) { 54 54 run.setText(text, 0); 55 55 } 56 56 } 57 57 58 58 } 59 59 60 60 } 61 61 } 62 62 63 63 //------------------------------------------------------------------ 64 64 65 65 //处理表格 66 66 //------------------------------------------------------------------ 67 67 List<XWPFTable> tables = doc.getTables(); 68 68 for (XWPFTable table : tables) { 69 69 List<XWPFTableRow> rows = table.getRows(); 70 70 for (XWPFTableRow row : rows) { 71 71 List<XWPFTableCell> cells = row.getTableCells(); 72 72 for (XWPFTableCell cell : cells) { 73 73 74 74 String text = cell.getText(); 75 75 if(text!=null){ 76 76 for(Entry<String,String> entry:map.entrySet()){ 77 77 String key = entry.getKey(); 78 78 String value = entry.getValue(); 79 79 if(text.equals(key)){ 80 80 //删除原单元格值 81 81 cell.removeParagraph(0); 82 82 //设置新单元格的值 83 83 cell.setText(value); 84 84 } 85 85 } 86 86 } 87 87 } 88 88 89 89 } 90 90 } 91 91 //------------------------------------------------------------------ 92 92 93 93 out = new FileOutputStream(filePath); 94 94 doc.write(out); 95 95 96 96 } catch (FileNotFoundException e) { 97 97 e.printStackTrace(); 98 98 } catch (IOException e) { 99 99 e.printStackTrace(); 100100 }finally{ 101101 try { 102102 in.close(); 103103 out.close(); 104104 } catch (IOException e) { 105105 e.printStackTrace(); 106106 } 107107 } 108108 109109 110110 111111 } 112112 113113 114114 }
main方法测试如下:
1 1 package word; 2 2 3 3 import java.util.HashMap; 4 4 import java.util.Map; 5 5 6 6 public class wordTest { 7 7 8 8 public static void main(String[] args) { 9 9 String filePath = "C:\\Users\\hp\\Desktop\\temp\\ceshi.docx"; 1010 Map<String,String> map = new HashMap<String,String>(); 1111 map.put("mark", "hello"); 1212 map.put("aaa", "bbb"); 1313 WordUtil.replaceAll(filePath, map); 1414 1515 } 1616 1717 }
至于JACOB操作Word,请看另一片文章: