package com.ots.common.utils.poi.consultation; import org.apache.poi.xwpf.usermodel.*; import org.springframework.util.Assert; import java.util.List; import java.util.Map; import java.util.Optional; public class PoiWordUtils { public static final String PREFIX_FIRST = "$"; public static final String PREFIX_SECOND = "{"; public static final String PLACEHOLDER_PREFIX = PREFIX_FIRST + PREFIX_SECOND; public static final String PLACEHOLDER_END = "}"; public static final String addRowText = "tbAddRow:"; public static final String addRowRepeatText = "tbAddRowRepeat:"; public static final String addRowFlag = PLACEHOLDER_PREFIX + addRowText; public static final String addRowRepeatFlag = PLACEHOLDER_PREFIX + addRowRepeatText; public static final String tbRepeatMatrixSeparator = ","; public static final String PLACEHOLDER_SUFFIX = "}"; public static final String PICTURE_PREFIX = PLACEHOLDER_PREFIX + "image:"; public static boolean isAddRow(XWPFTableRow row) { return isDynRow(row, addRowFlag); } public static boolean isAddRowRepeat(XWPFTableRow row) { return isDynRow(row, addRowRepeatFlag); } private static boolean isDynRow(XWPFTableRow row, String dynFlag) { if (row == null) { return false; } List tableCells = row.getTableCells(); if (tableCells != null) { XWPFTableCell cell = tableCells.get(0); if (cell != null) { String text = cell.getText(); return text != null && text.startsWith(dynFlag); } } return false; } public static Object getValueByPlaceholder(Map paramMap, String key) { if (paramMap != null) { if (key != null) { return paramMap.get(getKeyFromPlaceholder(key)); } } return null; } public static String getTbRepeatMatrix(String key) { Assert.notNull(key, "占位符为空"); String $1 = key.replaceAll("\\" + PREFIX_FIRST + "\\" + PREFIX_SECOND + addRowRepeatText + "(.*:)(.*)" + "\\" + PLACEHOLDER_SUFFIX, "$2"); return $1; } public static String getKeyFromPlaceholder(String placeholder) { return Optional.ofNullable(placeholder).map(p -> p.replaceAll("[\\$\\{\\}]", "")).get(); } public static void main(String[] args) { String s = "${aa}"; s = s.replaceAll(PLACEHOLDER_PREFIX + PLACEHOLDER_SUFFIX , ""); System.out.println(s); } public static void copyCellAndSetValue(XWPFTableCell sourceCell, XWPFTableCell targetCell, String text) { List sourceCellParagraphs = sourceCell.getParagraphs(); if (sourceCellParagraphs == null || sourceCellParagraphs.size() <= 0) { return; } XWPFParagraph sourcePar = sourceCellParagraphs.get(0); XWPFParagraph targetPar = targetCell.getParagraphs().get(0); targetPar.getCTP().setPPr(sourcePar.getCTP().getPPr()); List sourceParRuns = sourcePar.getRuns(); if (sourceParRuns != null && sourceParRuns.size() > 0) { List runs = targetPar.getRuns(); Optional.ofNullable(runs).ifPresent(rs -> rs.stream().forEach(r -> r.setText("", 0))); if (runs != null && runs.size() > 0) { runs.get(0).setText(text, 0); } else { XWPFRun cellR = targetPar.createRun(); cellR.setText(text, 0); targetCell.getCTTc().setTcPr(sourceCell.getCTTc().getTcPr()); } setTypeface(sourcePar, targetPar); } else { List runs = targetPar.getRuns(); if (runs != null && runs.size() > 0) { runs.get(0).setText(text, 0); } else { XWPFRun newRun = targetPar.createRun(); newRun.setText(text, 0); } } } private static void setTypeface(XWPFParagraph sourcePar, XWPFParagraph targetPar) { XWPFRun sourceRun = sourcePar.getRuns().get(0); String fontFamily = sourceRun.getFontFamily(); String color = sourceRun.getColor(); boolean bold = sourceRun.isBold(); boolean italic = sourceRun.isItalic(); int kerning = sourceRun.getKerning(); UnderlinePatterns underline = sourceRun.getUnderline(); XWPFRun targetRun = targetPar.getRuns().get(0); targetRun.setFontFamily(fontFamily); targetRun.setBold(bold); targetRun.setColor(color); targetRun.setItalic(italic); targetRun.setKerning(kerning); targetRun.setUnderline(underline); } public static boolean checkText(String text){ boolean check = false; if(text.indexOf(PLACEHOLDER_PREFIX)!= -1){ check = true; } return check; } public static String getPlaceholderReg(String text) { return "\\" + PREFIX_FIRST + "\\" + PREFIX_SECOND + text + "\\" + PLACEHOLDER_SUFFIX; } public static String getDocKey(String mapKey) { return PLACEHOLDER_PREFIX + mapKey + PLACEHOLDER_SUFFIX; } public static boolean isPicture(String text) { return text.startsWith(PICTURE_PREFIX); } public static void removeCells(XWPFTableRow row) { int size = row.getTableCells().size(); try { for (int i = 0; i < size; i++) { row.removeCell(i); } } catch (Exception e) { } } }