前置说明:
<dependency><groupId>com.deepoove</groupId><artifactId>poi-tl</artifactId><version>1.12.1</version>
</dependency>
模板样式:
删除行表格测试.docx
实现思路:通过定制占位符,然后将占位符所在的行进行删除。
@Slf4j
public class CustomRenderPolicy implements RenderPolicy {@Overridepublic void render(ElementTemplate elementTemplate, Object o, XWPFTemplate xwpfTemplate) {log.info("=====================CustomRenderPolicy=====================");RunTemplate runTemplate = (RunTemplate) elementTemplate;XWPFRun run = runTemplate.getRun();XWPFTableCell cell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody();int rowIndex = getRowIndex(cell.getTableRow());XWPFTable table = cell.getTableRow().getTable();// 问题1:table.removeRow(rowIndex);}private int getRowIndex(XWPFTableRow row) {List<XWPFTableRow> rows = row.getTable().getRows();return rows.indexOf(row);}
}
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.render.RenderContext;
import com.deepoove.poi.xwpf.BodyContainer;
import com.deepoove.poi.xwpf.BodyContainerFactory;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import run.siyuan.poi.tl.policy.CustomRenderPolicy;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;public class Main {public static void main(String[] args) throws IOException {// 读取模板文件FileInputStream fileInputStream = new FileInputStream("/Users/wuzhiqian/Desktop/HY/删除行表格测试.docx");// 创建模板配置ConfigureBuilder configureBuilder = Configure.builder();// 问题1 处理方案configureBuilder.setValidErrorHandler(new Configure.ClearHandler(){@Overridepublic void handler(RenderContext<?> context) {System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++");try {XWPFRun run = context.getRun();run.setText("/");BodyContainer bodyContainer = BodyContainerFactory.getBodyContainer(run);bodyContainer.clearPlaceholder(run);} catch (Exception e) {System.out.println("标签不存在-------------------------------------------");}}});configureBuilder.addPlugin('!', new CustomRenderPolicy());// 创建模板上下文Map<String, Object> context = new HashMap<>();context.put("a", "a");// 使用模板引擎替换文本标签XWPFTemplate compile = XWPFTemplate.compile(fileInputStream, configureBuilder.build());compile.render(context);// 保存生成的文档FileOutputStream outputStream = new FileOutputStream("/Users/wuzhiqian/Desktop/HY/删除行表格测试_" + System.currentTimeMillis() + ".docx");compile.write(outputStream);outputStream.close();compile.close();fileInputStream.close();}
}
遇到的问题:
错误1:定制的占位符是 !开头的,而模板文件中后面还存在一个 {{aaaaaa}} 占位符, 此时调用删除行同样会把 {{aaaaaa}} 删除,而 {{aaaaa}} 是一个文本占位符对应的策略TextRenderPolicy,当该测路执行的时候发现该占位符没有对应值,则会执行错误处理,默认情况下的默认处理是:
public static class ClearHandler implements ValidErrorHandler {public ClearHandler() {}public void handler(RenderContext<?> context) {XWPFRun run = context.getRun();BodyContainer bodyContainer = BodyContainerFactory.getBodyContainer(run);bodyContainer.clearPlaceholder(run);}}
在调用context.getRun();
方法的时候就会发现 {{aaaaa}} 已经被删除了,才会报错。
处理方案:就是对会报错的方法进行处理,我这里是进行了捕获。