哈哈,這是鄙人在博客園的第一篇博客,之前都是在簡書上碼字,廢話很少說,直接開工...html
需求分析:工做中遇到的一個技術需求,須要用java代碼操做Word,查找Word中的mark標記,而後進行替換,簡而言之就是「替換word中的指定字符串」;java
解決辦法:能夠用JACOB和POI來實現,下面我用的是POI操做。apache
用poi必需要用到Apache的jar包,我用的是最新的poi3.17工具
連接: https://pan.baidu.com/s/1LfW9JuZdG0IrMz6PVQCdeA 密碼: 3t57測試
-------------------------------我是分割線----------------------------------------spa
更通俗一點,就是進行全局替換,以下圖,咱們將字符串mark替換成hello,把字符串aaa替換成bbb:3d
替換後下圖:code
代碼以下:htm
直接寫成了一個工具類WordUtil:blog
這個類能通用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,請看另外一片文章: