poi-tl实现自定义RenderPolicy实现对表格进行列表

主程序如下:

 


import java.util.List;
import java.util.Map;

import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;

import com.deepoove.poi.NiceXWPFDocument;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.TableRenderData;
import com.deepoove.poi.policy.RenderPolicy;
import com.deepoove.poi.template.ElementTemplate;
import com.deepoove.poi.template.run.RunTemplate;

import net.ulsc.app.gzkg.C;

public class MyTableRenderPolicy implements RenderPolicy {
	@Override
	public void render(ElementTemplate eleTemplate, Object data, XWPFTemplate template) {
		// TODO Auto-generated method stub
		NiceXWPFDocument doc = template.getXWPFDocument();
		RunTemplate runTemplate = (RunTemplate) eleTemplate;
		XWPFRun run = runTemplate.getRun();
		if (null == data) {
			// clear table
			runTemplate.getRun().setText("", 0);
			runTemplate.getRun().removeTab();
			XWPFHelper.clearTable(eleTemplate.getTagName(), doc.getTables(), run);
			return;
		}
		TableRenderData tableData = (TableRenderData) data;
		List datas = tableData.getDatas();
		Map map = XWPFHelper.getTable(eleTemplate.getTagName(), doc.getTables());
		int startRow = 1; // TextHelper.intValue(map.get("startRow"));//getStartRow
		int startCell = 0;
		TextHelper.intValue(map.get("startCell"));
		// if(startRow==0) {
		// startRow++;
		// startCell=0;
		// }
		XWPFTable table = (XWPFTable) map.get("table");// doc.getTableArray(0);
		XWPFTableCell cell = null;
		XWPFRun r = null;
		if (null != table) {
			if (datas.size() > 0) {
				for (Object obj : datas) {
					if (null == obj)
						continue;
					String str = obj.toString();
					String[] split = str.split(C.CHAR_AND);
					int length = split.length;
					if (null == table.getRow(startRow + 1)) {
						// 复制样式
						XWPFHelper.copytTableRow(table.createRow(), table.getRow(startRow));
					}
					for (int m = startCell; m < length + startCell; m++) {
						cell = table.getRow(startRow).getCell(m);
						if (null != cell) {
							r = XWPFHelper.getRun(cell, run);
							r.setText(split[m - startCell]);
						}
						// table.getRow(startRow).getCell(m).setText(split[m]);
					}
					startRow++;
				}
			}
		}
		runTemplate.getRun().setText("", 0);
	}
}
 
  


工具类如下:

 

 


import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;


public class XWPFHelper {

	public static Map getTable(String pattern, List tables) {
		Map m = new HashMap<>();
		for (XWPFTable t : tables) {
			if (t.getText().contains(pattern)) {
				m.put("table", t);
				// m.put("startRow", i);
				// m.put("startCell", j);
				return m;
			}
		}
		return m;
	}

	public static void clearTable(String pattern, List tables, XWPFRun run) {
		// 清除自定义表格填写内容
		List rows = null;
		List cells = null;
		for (XWPFTable t : tables) {
			if (t.getText().contains(pattern)) {
				rows = t.getRows();
				//删除模板表格中新增加的行
				for (int i = 2; i < rows.size(); i++) {
					t.removeRow(i);
				}
				cells = rows.get(1).getTableCells();
				for (XWPFTableCell cell : cells) {
					XWPFParagraph par = cell.getParagraphs().get(0);
					for (XWPFRun r : par.getRuns()) {
						r.setText("", 0);
					}
				}

			}
		}
	}

	// 复制单元格
	public static void copyTableCell(XWPFTableCell target, XWPFTableCell source) {
		// 列属性
		target.getCTTc().setTcPr(source.getCTTc().getTcPr());
		// 删除目标 targetCell 所有单元格
		for (int pos = 0; pos < target.getParagraphs().size(); pos++) {
			target.removeParagraph(pos);
		}
		// 添加段落
		for (XWPFParagraph sp : source.getParagraphs()) {
			XWPFParagraph targetP = target.addParagraph();
			copyParagraph(targetP, sp);
		}
	}

	// public static void CopyRun(XWPFRun target, XWPFRun source) {
	// target.getCTR().setRPr(source.getCTR().getRPr());
	// // 设置文本
	// target.setText(source.text());
	// }
	public static void copyParagraph(XWPFParagraph target, XWPFParagraph source) {
		// 设置段落样式
		target.getCTP().setPPr(source.getCTP().getPPr());
		// 添加Run标签
		for (int pos = 0; pos < target.getRuns().size(); pos++) {
			target.removeRun(pos);
		}
		// for (XWPFRun s : source.getRuns()) {
		// XWPFRun targetrun = target.createRun();
		// CopyRun(targetrun, s);
		// }
	}

	public static void copytTableRow(XWPFTableRow target, XWPFTableRow source) {
		// 复制样式
		target.getCtRow().setTrPr(source.getCtRow().getTrPr());
		// 复制单元格
		for (int i = 0; i < target.getTableCells().size(); i++) {
			copyTableCell(target.getCell(i), source.getCell(i));
		}
	}

	public static XWPFRun getRun(XWPFTableCell cell, XWPFRun source) {
		XWPFParagraph par = cell.getParagraphs().get(0);
		// XWPFRun r = par.getRuns().size() > 0 ? par.getRuns().get(0) :
		// par.createRun();
		XWPFRun r = par.createRun();
		r.getCTR().setRPr(source.getCTR().getRPr());// 复制 run
		r.setFontFamily(source.getFontFamily());
		r.setFontSize(source.getFontSize());
		par.removeRun(source.getTextPosition());
		return r;
	}

	public static XWPFTable getTableByTBL(List tables, CTTbl tbl) {
		for (int i = 0; i < tables.size(); i++) {
			if (tables.get(i).getCTTbl() == tbl)
				return tables.get(i);
		}
		return null;
	}

}

 

 

//初始化模板
//调用
	XWPFTemplate tt = XWPFTemplate.compile(FileTools.getInstance().getArchiveFile(1),
				FileTools.getInstance().getMyarchiveConfigure()); 





//FileTools工具类中方法getMyarchiveConfigure
public Configure getMyarchiveConfigure() {
		if(null==this.myarchiveConfigure) {
			this.myarchiveConfigure=Configure.createDefault();
			this.myarchiveConfigure=this.myarchiveConfigure.plugin('&', new 
MyTableRenderPolicy()).plugin('$', new MyPictureRenderPolicy());
		}
		return myarchiveConfigure;
	}

 

模板如下图所示:

 

poi-tl实现自定义RenderPolicy实现对表格进行列表_第1张图片


实现效果如下图所示:

poi-tl实现自定义RenderPolicy实现对表格进行列表_第2张图片

 

 

循环时数据覆盖问题解决:

XWPFTemplate tt = XWPFTemplate.compile(FileTools.getInstance().getArchiveFile(1),
				FileTools.getInstance().getMyarchiveConfigure());
		Map clearData = new HashMap<>();
        //将模板中的标签key的值都设置为null
		for (ElementTemplate et : tt.getElementTemplates()) {
			clearData.put(et.getTagName(), null);
		}
    
    //循环中将清除模板中的数据
	while (mit.hasNext()) {
			// 先render 一个空数据,包括所有的标签
			tt.render(clearData);//清空数据

 

 

你可能感兴趣的:(java)