当前位置:   article > 正文

POI Word单元格合并_looprowtablerenderpolicy

looprowtablerenderpolicy

Word表格模板

 导出word表格图例

解决方案:重写LoopRowTableRenderPolicy插件。

  1. public class MyTableRenderPolicy implements RenderPolicy {
  2. private String prefix;
  3. private String suffix;
  4. private boolean onSameLine;
  5. public MyTableRenderPolicy() {
  6. this(false);
  7. }
  8. public MyTableRenderPolicy(boolean onSameLine) {
  9. this("[", "]", onSameLine);
  10. }
  11. public MyTableRenderPolicy(String prefix, String suffix) {
  12. this(prefix, suffix, false);
  13. }
  14. public MyTableRenderPolicy(String prefix, String suffix, boolean onSameLine) {
  15. this.prefix = prefix;
  16. this.suffix = suffix;
  17. this.onSameLine = onSameLine;
  18. }
  19. @Override
  20. public void render(ElementTemplate eleTemplate, Object data_array, XWPFTemplate template) {
  21. Object data=null;
  22. Map data_array_map= (Map) data_array;
  23. data=data_array_map.get("data_list");
  24. List<Map> row_map_list= (List<Map>) data_array_map.get("row_map_list");
  25. RunTemplate runTemplate = (RunTemplate) eleTemplate;
  26. XWPFRun run = runTemplate.getRun();
  27. try {
  28. if (!TableTools.isInsideTable(run)) {
  29. throw new IllegalStateException(
  30. "The template tag " + runTemplate.getSource() + " must be inside a table");
  31. }
  32. XWPFTableCell tagCell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody();
  33. XWPFTable table = tagCell.getTableRow().getTable();
  34. run.setText("", 0);
  35. int templateRowIndex = getTemplateRowIndex(tagCell);
  36. if (null != data && data instanceof Iterable) {
  37. Iterator<?> iterator = ((Iterable<?>) data).iterator();
  38. XWPFTableRow templateRow = table.getRow(templateRowIndex);
  39. int insertPosition = templateRowIndex;
  40. TemplateResolver resolver = new TemplateResolver(template.getConfig().copy(prefix, suffix));
  41. boolean firstFlag = true;
  42. int index = 0;
  43. boolean hasNext = iterator.hasNext();
  44. while (hasNext) {
  45. Object root = iterator.next();
  46. hasNext = iterator.hasNext();
  47. insertPosition = templateRowIndex++;
  48. XWPFTableRow nextRow = table.insertNewTableRow(insertPosition);
  49. setTableRow(table, templateRow, insertPosition);
  50. // double set row
  51. XmlCursor newCursor = templateRow.getCtRow().newCursor();
  52. newCursor.toPrevSibling();
  53. XmlObject object = newCursor.getObject();
  54. nextRow = new XWPFTableRow((CTRow) object, table);
  55. if (!firstFlag) {
  56. // update VMerge cells for non-first row
  57. List<XWPFTableCell> tableCells = nextRow.getTableCells();
  58. for (XWPFTableCell cell : tableCells) {
  59. CTTcPr tcPr = TableTools.getTcPr(cell);
  60. CTVMerge vMerge = tcPr.getVMerge();
  61. if (null == vMerge) continue;
  62. if (STMerge.RESTART == vMerge.getVal()) {
  63. vMerge.setVal(STMerge.CONTINUE);
  64. }
  65. }
  66. } else {
  67. firstFlag = false;
  68. }
  69. setTableRow(table, nextRow, insertPosition);
  70. RenderDataCompute dataCompute = template.getConfig()
  71. .getRenderDataComputeFactory()
  72. .newCompute(EnvModel.of(root, EnvIterator.makeEnv(index++, hasNext)));
  73. List<XWPFTableCell> cells = nextRow.getTableCells();
  74. cells.forEach(cell -> {
  75. List<MetaTemplate> templates = resolver.resolveBodyElements(cell.getBodyElements());
  76. new DocumentProcessor(template, resolver, dataCompute).process(templates);
  77. });
  78. }
  79. for(Map row_map:row_map_list){
  80. //合并开头得单位
  81. TableTools.mergeCellsVertically(table, 0, (int)row_map.get("fromrow"), (int)row_map.get("torow"));
  82. }
  83. }
  84. table.removeRow(templateRowIndex);
  85. afterloop(table, data);
  86. } catch (Exception e) {
  87. throw new RenderException("HackLoopTable for " + eleTemplate + "error: " + e.getMessage(), e);
  88. }
  89. }
  90. private int getTemplateRowIndex(XWPFTableCell tagCell) {
  91. XWPFTableRow tagRow = tagCell.getTableRow();
  92. return onSameLine ? getRowIndex(tagRow) : (getRowIndex(tagRow) + 1);
  93. }
  94. protected void afterloop(XWPFTable table, Object data) {
  95. }
  96. @SuppressWarnings("unchecked")
  97. private void setTableRow(XWPFTable table, XWPFTableRow templateRow, int pos) {
  98. List<XWPFTableRow> rows = (List<XWPFTableRow>) ReflectionUtils.getValue("tableRows", table);
  99. rows.set(pos, templateRow);
  100. table.getCTTbl().setTrArray(pos, templateRow.getCtRow());
  101. }
  102. private int getRowIndex(XWPFTableRow row) {
  103. List<XWPFTableRow> rows = row.getTable().getRows();
  104. return rows.indexOf(row);
  105. }
  106. }
  1. public class WordPoiTest {
  2. public static void main(String[]args) throws IOException {
  3. WordPoiTest wordPoiTest=new WordPoiTest();
  4. wordPoiTest.test();
  5. }
  6. public void test() throws IOException {
  7. LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
  8. Configure config = Configure.builder()
  9. .bind("tables_2", new MyTableRenderPolicy()).build();
  10. XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\QYF\\Desktop\\template.docx",config);
  11. Map map=new HashMap();
  12. map.put("title", "Hi, poi-tl Word模板引擎");
  13. List listList=new ArrayList<>();
  14. Tables tables1=new Tables("type1",1,"占山占山占山占山占山占山占山占占山占山占山占山占山山占山占山","123456");
  15. Tables tables2=new Tables("type1",2,"占山","123456");
  16. Tables tables3=new Tables("type1",3,"占山","123456");
  17. listList.add(tables1);
  18. listList.add(tables2);
  19. listList.add(tables3);
  20. Tables tables4=new Tables("type2",4,"占山","123456");
  21. Tables tables5=new Tables("type2",1,"占山占山占山占山占山占山占山占占山占山占山占山占山山占山占山","123456");
  22. Tables tables6=new Tables("type2",2,"占山","123456");
  23. listList.add(tables4);
  24. listList.add(tables5);
  25. listList.add(tables6);
  26. Tables tables7=new Tables("type3",3,"占山","123456");
  27. listList.add(tables7);
  28. Map data_array=new HashMap();
  29. data_array.put("data_list",listList);
  30. List<Map> row_map_list=new ArrayList<>();
  31. Map row_map=new HashMap();
  32. row_map.put("fromrow",1);
  33. row_map.put("torow",3);
  34. row_map_list.add(row_map);
  35. data_array.put("row_map_list",row_map_list);
  36. map.put("tables_2",data_array);
  37. template.render(map);
  38. template.writeAndClose(new FileOutputStream("C:\\Users\\QYF\\Desktop\\output.docx"));
  39. }
  40. }
  41. class Tables{
  42. private String type;
  43. private int num;
  44. private String name;
  45. private String product_num;
  46. public String getType() {
  47. return type;
  48. }
  49. public void setType(String type) {
  50. this.type = type;
  51. }
  52. public int getNum() {
  53. return num;
  54. }
  55. public void setNum(int num) {
  56. this.num = num;
  57. }
  58. public String getName() {
  59. return name;
  60. }
  61. public void setName(String name) {
  62. this.name = name;
  63. }
  64. public String getProduct_num() {
  65. return product_num;
  66. }
  67. public void setProduct_num(String product_num) {
  68. this.product_num = product_num;
  69. }
  70. public Tables(String type, int num, String name, String product_num) {
  71. this.type = type;
  72. this.num = num;
  73. this.name = name;
  74. this.product_num = product_num;
  75. }
  76. public Tables() {
  77. }
  78. }

加载插件时,加载自定义的插件

Configure config = Configure.builder()
        .bind("tables_2", new MyTableRenderPolicy()).build();

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/606825
推荐阅读
相关标签
  

闽ICP备14008679号