本文主要是介绍POI替换word中的指定文字(包含表格,表格中有回车),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
网上可以找到很多POI替换Word中指定文字的代码,然而基本上都没有对文档中的表格中包含的段落(回车)进行处理。自己写了,代码记录如下:
/**
*
*@templetStream 文档的输入流
*@data 要替换的key,value的map,将文档中的@key@替换为value值
*
**/
public byte[] genWord2003ByTemplet(InputStream templetStream,Map<String, String> data) throws IOException {XWPFDocument doc = new XWPFDocument(templetStream);// 替换段落中的指定文字Iterator<XWPFParagraph> itPara = doc.getParagraphsIterator();while (itPara.hasNext()) {XWPFParagraph paragraph = (XWPFParagraph) itPara.next();List<XWPFRun> runs = paragraph.getRuns();String beforeOneparaString="";for (int i=0; runs!=null&&i<runs.size(); i++) {String oneparaString = runs.get(i).getText(runs.get(i).getTextPosition());for (Map.Entry<String, String> entry : data.entrySet()) {if(oneparaString != null){if(oneparaString.contains(String.format("@%s@", entry.getKey()))){oneparaString = oneparaString.replace(String.format("@%s@", entry.getKey()), Strings.nullToEmpty(entry.getValue()));}else if(oneparaString.contains(entry.getKey()) && beforeOneparaString.endsWith("@")){oneparaString = oneparaString.replace(entry.getKey(), Strings.nullToEmpty(entry.getValue()));runs.get(i-1).setText(beforeOneparaString.substring(0,beforeOneparaString.length()-1), 0);if(i+1<runs.size()){String afterOneparaString=runs.get(i+1).getText(runs.get(i+1).getTextPosition());runs.get(i+1).setText(afterOneparaString.substring(0,beforeOneparaString.length()-1), 0);}}}}runs.get(i).setText(oneparaString, 0);beforeOneparaString=oneparaString;}}// 替换表格中的指定文字 Iterator<XWPFTable> itTable = doc.getTablesIterator(); while (itTable.hasNext()) { XWPFTable table = (XWPFTable) itTable.next(); int rcount = table.getNumberOfRows(); for (int i = 0; i < rcount; i++) { XWPFTableRow row = table.getRow(i); List<XWPFTableCell> cells = row.getTableCells(); for (XWPFTableCell cell : cells) { //表格中处理段落(回车)List<XWPFParagraph> cellParList= cell.getParagraphs();for(int p=0; cellParList!=null&&p<cellParList.size();p++){ //每个格子循环List<XWPFRun> runs = cellParList.get(p).getRuns(); //每个格子的内容都要单独处理String beforeOneparaString="";for (int q = 0; runs!=null&&q<runs.size(); q++) {String oneparaString = runs.get(q).getText(runs.get(q).getTextPosition());for (Map.Entry<String, String> entry : data.entrySet()) {if(oneparaString!=null){if(oneparaString.contains(String.format("@%s@", entry.getKey()))){oneparaString = oneparaString.replace(String.format("@%s@", entry.getKey()), Strings.nullToEmpty(entry.getValue()));}else if(oneparaString.contains(entry.getKey()) && beforeOneparaString.endsWith("@")){oneparaString = oneparaString.replace(entry.getKey(), Strings.nullToEmpty(entry.getValue()));runs.get(q-1).setText(beforeOneparaString.substring(0,beforeOneparaString.length()-1), 0);if(q+1<runs.size()){String afterOneparaString=runs.get(q+1).getText(runs.get(q+1).getTextPosition());runs.get(q+1).setText(afterOneparaString.substring(0,beforeOneparaString.length()-1), 0);}}}}runs.get(q).setText(oneparaString, 0);beforeOneparaString=oneparaString;}} } } }ByteArrayOutputStream os = new ByteArrayOutputStream();if (doc != null) {doc.write(os);}byte[] b = os.toByteArray();Closeables.close(templetStream, true);Closeables.close(os, true);return b;}
这篇关于POI替换word中的指定文字(包含表格,表格中有回车)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!