Word文档操作神器:POI功能全解析 ——POI Word 文本、图片、表格、图表、文本框操作
功能:文本、图表、文本框内容替换、表格动态增加内容、单元格合并增加图表、替换图表内容你能用到的word的功能这儿都有!话不多说,直接上代码!pom 引用:// https://mvnrepository.com/artifact/org.apache.poi/poi implementation 'org.apache.poi:poi:5.2.2'// https://mvnrepository.com/artifact/com.deepoove/poi-tl implementation 'com.deepoove:poi-tl:1.12.2'// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-full implementation 'org.apache.poi:poi-ooxml-full:5.2.3'// https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml implementation 'org.apache.poi:poi-ooxml:5.2.2'全量代码粘贴即用:`import com.deepoove.poi.XWPFTemplate;import com.deepoove.poi.data.ChartMultiSeriesRenderData;import com.deepoove.poi.data.Charts;import com.deepoove.poi.util.TableTools;import com.documents4j.api.DocumentType;import com.documents4j.api.IConverter;import com.documents4j.job.LocalConverter;import com.zzys.qhyscredit.utils.DateUtil;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.poi.ss.util.CellReference;import org.apache.poi.util.Units;import org.apache.poi.xddf.usermodel.chart.*;import org.apache.poi.xssf.usermodel.XSSFCell;import org.apache.poi.xssf.usermodel.XSSFRow;import org.apache.poi.xssf.usermodel.XSSFSheet;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import org.apache.poi.xwpf.usermodel.*;import org.apache.xmlbeans.XmlCursor;import org.apache.xmlbeans.XmlException;import org.apache.xmlbeans.XmlObject;import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;import java.io.*;import java.nio.file.Files;import java.util.*;/*** @Author:Internet_worm* @Date:2024/7/26:09:59* @Description: word 操作工具类*/public class WordUtils { /*** * @Description :替换段落文本 * @param document docx解析对象 * @param textMap 需要替换的信息集合 * @return void */ public static void changeText(XWPFDocument document, MapString, String textMap) { // 获取段落集合 IteratorXWPFParagraph iterator = document.getParagraphsIterator(); XWPFParagraph paragraph = null; while (iterator.hasNext()) { paragraph = iterator.next(); // 判断此段落是否需要替换 if (checkText(paragraph.getText())) { replaceValue(paragraph, textMap); } } } /*** * @Description :检查文本中是否包含指定的字符 * @param text * @return boolean */ public static boolean checkText(String text) {// System.out.println("paragraph text :"+text); boolean check = false; if (text.contains("$")) { check = true; } return check; } /** * 替换图片 * * @param document * @param picData * @throws Exception */ public static void changePic(XWPFDocument document, MapString, String picData) throws Exception { // 获取段落集合 IteratorXWPFParagraph iterator = document.getParagraphsIterator(); XWPFParagraph paragraph; while (iterator.hasNext()) { paragraph = iterator.next(); // 判断此段落是否需要替换 String text = paragraph.getText(); if (checkText(text)) { replacePicValue(paragraph, picData); } } } /*** * @Description :替换表格内的文字 * @param document * @param data * @return void */ public static void changeTableText(XWPFDocument document, MapString, String data) { // 获取文件的表格 IteratorXWPFTable tableList = document.getTablesIterator(); XWPFTable table; ListXWPFTableRow rows; ListXWPFTableCell cells; // 循环所有需要进行替换的文本,进行替换 while (tableList.hasNext()) { table = tableList.next(); if (checkText(table.getText())) { rows = table.getRows(); // 遍历表格,并替换模板 for (XWPFTableRow row : rows) { cells = row.getTableCells(); for (XWPFTableCell cell : cells) { // 判断单元格是否需要替换 if (checkText(cell.getText())) { ListXWPFParagraph paragraphs = cell.getParagraphs(); for (XWPFParagraph paragraph : paragraphs) { replaceValue(paragraph, data); } } } } } } } /*** * @Description :替换表格内图片 * @param document * @param picData * @return void * @Date 2022/11/18 11:29 */ public static void changeTablePic(XWPFDocument document, MapString, String picData) throws Exception { // 获取文件的表格 IteratorXWPFTable tableList = document.getTablesIterator(); XWPFTable table; ListXWPFTableRow rows; ListXWPFTableCell cells; // 循环所有需要进行替换的文本,进行替换 while (tableList.hasNext()) { table = tableList.next(); if (checkText(table.getText())) { rows = table.getRows(); // 遍历表格,并替换模板 for (XWPFTableRow row : rows) { cells = row.getTableCells(); for (XWPFTableCell cell : cells) { // 判断单元格是否需要替换 if (checkText(cell.getText())) {