| | |
| | | package com.product.print.util; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import com.deepoove.poi.exception.RenderException; |
| | | import com.deepoove.poi.render.RenderContext; |
| | | import com.deepoove.poi.template.ElementTemplate; |
| | | import com.deepoove.poi.template.MetaTemplate; |
| | | import com.deepoove.poi.template.run.RunTemplate; |
| | | import com.deepoove.poi.util.TableTools; |
| | | import com.google.common.collect.Maps; |
| | | import com.product.common.lang.StringUtils; |
| | | import com.product.core.exception.BaseException; |
| | | import com.product.print.config.CmnCode; |
| | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.*; |
| | | import java.util.Base64; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | |
| | | |
| | | private String replaceKey; |
| | | |
| | | private final String indexKey = "~index~"; |
| | | private final String indexKey = "~i~"; |
| | | |
| | | public DynamicTableRenderPolicy(String replaceKey) { |
| | | this.replaceKey = replaceKey; |
| | | |
| | | } |
| | | |
| | | private List<MetaTemplate> metaTemplateList; |
| | |
| | | |
| | | @Override |
| | | public void render(XWPFTable xwpfTable, Object data) throws Exception { |
| | | if (xwpfTable == null) { |
| | | return; |
| | | } |
| | | List<Map<String, Object>> subTableData = (List<Map<String, Object>>) data; |
| | | List<XWPFTableRow> rows = xwpfTable.getRows(); |
| | | //读取rows中的内容 |
| | | String tableExpression = "{{" + this.replaceKey + "}}"; |
| | | //获取表格起始行和结束行 |
| | | int startRowIndex = -1; |
| | | rows: |
| | | for (int i = 0; i < rows.size(); i++) { |
| | | XWPFTableRow row = rows.get(i); |
| | | for (int j = 0; j < row.getTableCells().size(); j++) { |
| | | XWPFTableCell cell = row.getTableCells().get(j); |
| | | String text = cell.getText(); |
| | | if (tableExpression.equals(text)) { |
| | | startRowIndex = i; |
| | | break rows; |
| | | } |
| | | try { |
| | | if (xwpfTable == null) { |
| | | return; |
| | | } |
| | | List<Map<String, Object>> subTableData = (List<Map<String, Object>>) data; |
| | | List<XWPFTableRow> rows = xwpfTable.getRows(); |
| | | //读取rows中的内容 |
| | | String tableExpression = "{{" + this.replaceKey + "}}"; |
| | | //获取表格起始行和结束行 |
| | | int startRowIndex = -1; |
| | | outer: for (int i = 0; i < rows.size(); i++) { |
| | | XWPFTableRow row = rows.get(i); |
| | | for (int j = 0; j < row.getTableCells().size(); j++) { |
| | | XWPFTableCell cell = row.getTableCells().get(j); |
| | | String text = cell.getText(); |
| | | if (tableExpression.equals(text)) { |
| | | startRowIndex = i; |
| | | break outer; |
| | | } |
| | | |
| | | } |
| | | } |
| | | if (startRowIndex == -1) { |
| | | throw new BaseException(CmnCode.NOT_FIND_START_FLAG); |
| | | } |
| | | } |
| | | } |
| | | if (startRowIndex == -1) { |
| | | throw new BaseException(CmnCode.NOT_FIND_START_FLAG); |
| | | } |
| | | |
| | | //在表格中查找子表字段以{{$开头}}以}}结尾的内容 |
| | | String regex = "\\{\\{\\$[a-zA-Z0-9_]+\\}\\}"; |
| | | Pattern pattern = Pattern.compile(regex); |
| | | //字段所在行 |
| | | XWPFTableRow fieldRow = null; |
| | | for (int i = startRowIndex; i < rows.size(); i++) { |
| | | XWPFTableRow row = rows.get(i); |
| | | for (int j = 0; j < row.getTableCells().size(); j++) { |
| | | XWPFTableCell cell = row.getTableCells().get(j); |
| | | String text = cell.getText(); |
| | | Matcher matcher = pattern.matcher(text); |
| | | if (matcher.find()) { |
| | | fieldRow = row; |
| | | i = rows.size(); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | if (fieldRow == null) { |
| | | throw new BaseException(CmnCode.NOT_FIND_CHILD_TABLE_FIELD); |
| | | } |
| | | //在表格中查找子表字段以{{$开头}}以}}结尾的内容 |
| | | String regex = "\\{\\{\\$[a-zA-Z0-9_]+\\}\\}"; |
| | | Pattern pattern = Pattern.compile(regex); |
| | | //字段所在行 |
| | | XWPFTableRow fieldRow = null; |
| | | for (int i = startRowIndex; i < rows.size(); i++) { |
| | | XWPFTableRow row = rows.get(i); |
| | | for (int j = 0; j < row.getTableCells().size(); j++) { |
| | | XWPFTableCell cell = row.getTableCells().get(j); |
| | | String text = cell.getText(); |
| | | Matcher matcher = pattern.matcher(text); |
| | | if (matcher.find()) { |
| | | fieldRow = row; |
| | | i = rows.size(); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | if (fieldRow == null) { |
| | | throw new BaseException(CmnCode.NOT_FIND_CHILD_TABLE_FIELD); |
| | | } |
| | | |
| | | //读取最后行每个单元格的值,调用getReplaceKey方法获取表达式中的值 |
| | | String[] fieldNames = new String[fieldRow.getTableCells().size()]; |
| | | for (int i = 0; i < fieldRow.getTableCells().size(); i++) { |
| | | XWPFTableCell cell = fieldRow.getTableCells().get(i); |
| | | String text = cell.getText(); |
| | | String replaceKey = getReplaceKey(text); |
| | | //获取表达式中的值 |
| | | if (StringUtils.isEmpty(replaceKey)) { |
| | | //设置单元格为空值 |
| | | replaceKey = ""; |
| | | } |
| | | fieldNames[i] = replaceKey; |
| | | } |
| | | //读取最后行每个单元格的值,调用getReplaceKey方法获取表达式中的值 |
| | | String[] fieldNames = new String[fieldRow.getTableCells().size()]; |
| | | for (int i = 0; i < fieldRow.getTableCells().size(); i++) { |
| | | XWPFTableCell cell = fieldRow.getTableCells().get(i); |
| | | String text = cell.getText(); |
| | | String replaceKey = getReplaceKey(text); |
| | | //获取表达式中的值 |
| | | if (StringUtils.isEmpty(replaceKey)) { |
| | | //设置单元格为空值 |
| | | replaceKey = ""; |
| | | } |
| | | fieldNames[i] = replaceKey; |
| | | } |
| | | |
| | | if (subTableData != null && subTableData.size() > 0) { |
| | | for (int i = 0; i < subTableData.size(); i++) { |
| | | Map<String, Object> map = subTableData.get(i); |
| | | XWPFTableRow row; |
| | | if (i == 0) { |
| | | row = fieldRow; |
| | | } else { |
| | | int fieldRowIndex = xwpfTable.getRows().indexOf(fieldRow); |
| | | //创建一行在fieldRowIndex下面 |
| | | row = xwpfTable.insertNewTableRow(fieldRowIndex + i); |
| | | PrintPoiUtil.copyTableRow(row, fieldRow); |
| | | } |
| | | //遍历字段每个字段创建一个单元格 |
| | | for (int j = 0; j < fieldNames.length; j++) { |
| | | //当前单元格 |
| | | XWPFTableCell cell = row.getCell(j); |
| | | //清空单元格内容 |
| | | XWPFParagraph xwpfParagraph = cell.addParagraph(); |
| | | int index = cell.getParagraphs().indexOf(xwpfParagraph); |
| | | PrintPoiUtil.copyParagraph(xwpfParagraph, cell.getParagraphs().get(0)); |
| | | //设置段落文字对齐方式 |
| | | xwpfParagraph.setAlignment(cell.getParagraphs().get(0).getAlignment()); |
| | | //删除所有的run |
| | | for (int k = xwpfParagraph.getRuns().size() - 1; k >= 0; k--) { |
| | | xwpfParagraph.removeRun(k); |
| | | } |
| | | XWPFRun xwpfRun = xwpfParagraph.createRun(); |
| | | PrintPoiUtil.copyRun(xwpfRun, cell.getParagraphs().get(0).getRuns().get(0)); |
| | | //删除cell中的段落但排除新增的段落 |
| | | for (int k = cell.getParagraphs().size() - 1; k >= 0; k--) { |
| | | if (k < index) { |
| | | cell.removeParagraph(k); |
| | | } |
| | | } |
| | | //判断是否是序号列 |
| | | if (indexKey.equals(fieldNames[j])) { |
| | | cell.setText(String.valueOf(i + 1)); |
| | | continue; |
| | | } |
| | | Object value = map.get(fieldNames[j]); |
| | | if (value == null) { |
| | | value = ""; |
| | | } |
| | | // if ("lx_flow_opinion".equals(this.replaceKey) && "opinion".equals(fieldNames[j])) { |
| | | // //将流程节点处理人加到意见栏中 |
| | | // value = "[" + map.get("actual_person") + "] " + value; |
| | | // } |
| | | xwpfRun.setText(value.toString(), 0); |
| | | if ("lx_flow_opinion".equals(this.replaceKey) && "opinion".equals(fieldNames[j])) { |
| | | if (!StringUtils.isEmpty(map.get("sign_attachment_uuid"))) { |
| | | //意见框 插入签名图片到单元格右下角位置 |
| | | xwpfParagraph = cell.addParagraph(); |
| | | xwpfParagraph.setAlignment(ParagraphAlignment.RIGHT); |
| | | XWPFRun run = xwpfParagraph.createRun(); |
| | | String signAttachmentBase64 = map.get("sign_attachment_uuid").toString(); |
| | | //将base64转换字节流 |
| | | byte[] bytes = Base64.getDecoder().decode(signAttachmentBase64.split(",")[1]); |
| | | //将字节流转换为输入流 |
| | | InputStream inputStream = new ByteArrayInputStream(bytes); |
| | | try { |
| | | //换行插入图片 |
| | | run.addPicture(inputStream, XWPFDocument.PICTURE_TYPE_PNG, "sign.png", Units.toEMU(50), Units.toEMU(20)); |
| | | run.addBreak(); |
| | | run.setText("" + map.get("deal_time").toString().substring(0, 16)); |
| | | inputStream.close(); |
| | | } catch (InvalidFormatException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } else { |
| | | xwpfParagraph = cell.addParagraph(); |
| | | xwpfParagraph.setAlignment(ParagraphAlignment.RIGHT); |
| | | XWPFRun run = xwpfParagraph.createRun(); |
| | | run.setText((String) map.get("actual_person")); |
| | | run.addBreak(); |
| | | run.setText(map.get("deal_time").toString().substring(0, 16)); |
| | | } |
| | | if (subTableData != null && subTableData.size() > 0) { |
| | | int rowCount; |
| | | for (int i = 0, count = 0; i < subTableData.size(); i += rowCount, count++) { |
| | | rowCount = 1; |
| | | Map<String, Object> map = subTableData.get(i); |
| | | XWPFTableRow row; |
| | | if (i == 0) { |
| | | row = fieldRow; |
| | | } else { |
| | | int fieldRowIndex = xwpfTable.getRows().indexOf(fieldRow); |
| | | //创建一行在fieldRowIndex下面 |
| | | row = xwpfTable.insertNewTableRow(fieldRowIndex + count); |
| | | PrintPoiUtil.copyTableRow(row, fieldRow); |
| | | } |
| | | //遍历字段每个字段创建一个单元格 |
| | | for (int j = 0; j < fieldNames.length; j++) { |
| | | String fieldName = fieldNames[j]; |
| | | if (fieldName.contains(" ")) { |
| | | int curIndex = Integer.parseInt(fieldName.substring(fieldName.indexOf(" ") + 1)); |
| | | if (curIndex > 1 && curIndex > rowCount) { |
| | | map = i + 1 == subTableData.size() ? Maps.newHashMap() : subTableData.get(i + 1); |
| | | rowCount = curIndex; |
| | | } |
| | | fieldName = fieldName.substring(0, fieldName.indexOf(" ")); |
| | | } |
| | | //当前单元格 |
| | | XWPFTableCell cell = row.getCell(j); |
| | | //清空单元格内容 |
| | | XWPFParagraph xwpfParagraph = cell.addParagraph(); |
| | | int index = cell.getParagraphs().indexOf(xwpfParagraph); |
| | | PrintPoiUtil.copyParagraph(xwpfParagraph, cell.getParagraphs().get(0)); |
| | | //设置段落文字对齐方式 |
| | | xwpfParagraph.setAlignment(cell.getParagraphs().get(0).getAlignment()); |
| | | //删除所有的run |
| | | for (int k = xwpfParagraph.getRuns().size() - 1; k >= 0; k--) { |
| | | xwpfParagraph.removeRun(k); |
| | | } |
| | | XWPFRun xwpfRun = xwpfParagraph.createRun(); |
| | | PrintPoiUtil.copyRun(xwpfRun, cell.getParagraphs().get(0).getRuns().get(0)); |
| | | //删除cell中的段落但排除新增的段落 |
| | | for (int k = cell.getParagraphs().size() - 1; k >= 0; k--) { |
| | | if (k < index) { |
| | | cell.removeParagraph(k); |
| | | } |
| | | } |
| | | //判断是否是序号列 |
| | | if (indexKey.equals(fieldName)) { |
| | | if (map.isEmpty()) { |
| | | xwpfRun.setText("", 0); |
| | | } else { |
| | | xwpfRun.setText(String.valueOf(i + rowCount), 0); |
| | | } |
| | | continue; |
| | | } |
| | | Object value = map.get(fieldName); |
| | | if (value == null) { |
| | | value = ""; |
| | | } |
| | | xwpfRun.setText(value.toString(), 0); |
| | | if ("lx_flow_opinion".equals(this.replaceKey) && "opinion".equals(fieldName)) { |
| | | if (!StringUtils.isEmpty(map.get("sign_attachment_uuid"))) { |
| | | //意见框 插入签名图片到单元格右下角位置 |
| | | xwpfParagraph = cell.addParagraph(); |
| | | xwpfParagraph.setAlignment(ParagraphAlignment.RIGHT); |
| | | XWPFRun run = xwpfParagraph.createRun(); |
| | | String signAttachmentBase64 = map.get("sign_attachment_uuid").toString(); |
| | | //将base64转换字节流 |
| | | byte[] bytes = Base64.getDecoder().decode(signAttachmentBase64.split(",")[1]); |
| | | //将字节流转换为输入流 |
| | | InputStream inputStream = new ByteArrayInputStream(bytes); |
| | | try { |
| | | //换行插入图片 |
| | | run.addPicture(inputStream, XWPFDocument.PICTURE_TYPE_PNG, "sign.png", Units.toEMU(50), Units.toEMU(20)); |
| | | run.addBreak(); |
| | | run.setText("" + map.get("deal_time").toString().substring(0, 16)); |
| | | inputStream.close(); |
| | | } catch (InvalidFormatException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } else { |
| | | xwpfParagraph = cell.addParagraph(); |
| | | xwpfParagraph.setAlignment(ParagraphAlignment.RIGHT); |
| | | XWPFRun run = xwpfParagraph.createRun(); |
| | | run.setText((String) map.get("actual_person")); |
| | | run.addBreak(); |
| | | run.setText(map.get("deal_time").toString().substring(0, 16)); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | } else { |
| | | } |
| | | } |
| | | } else { |
| | | |
| | | // 删除fieldRow |
| | | int index = xwpfTable.getRows().indexOf(fieldRow); |
| | | xwpfTable.removeRow(index); |
| | | } |
| | | //删除起始行 |
| | | xwpfTable.removeRow(startRowIndex); |
| | | int index = xwpfTable.getRows().indexOf(fieldRow); |
| | | xwpfTable.removeRow(index); |
| | | } |
| | | //删除起始行 |
| | | xwpfTable.removeRow(startRowIndex); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | /** |