com.github.xiao1wang.wordapp.renderpolicy.TableRenderPolicy Maven / Gradle / Ivy
package com.github.xiao1wang.wordapp.renderpolicy;
import com.deepoove.poi.exception.RenderException;
import com.deepoove.poi.policy.AbstractRenderPolicy;
import com.deepoove.poi.render.RenderContext;
import com.deepoove.poi.util.TableTools;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import com.github.xiao1wang.wordapp.renderData.TableRenderData;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;
/**
* TODO : 更新表格策略
*/
public class TableRenderPolicy extends AbstractRenderPolicy {
private static Logger LOGGER = LoggerFactory.getLogger(TableRenderPolicy.class);
@Override
protected boolean validate(TableRenderData data) {
return true;
}
@Override
protected void afterRender(RenderContext context) {
clearPlaceholder(context, true);
}
@Override
public void doRender(RenderContext context) throws Exception {
TableRenderData data = context.getData();
XWPFRun run = context.getRun();
NiceXWPFDocument document = (NiceXWPFDocument) run.getParent().getDocument();
try {
if (!TableTools.isInsideTable(run)) {
throw new IllegalStateException(" 变量的定义必须放在表格的单元格内 ");
}
XWPFTableCell currentCell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody();
XWPFTable table = currentCell.getTableRow().getTable();
if(data == null) {
return;
}
List