| | |
| | | package com.product.print.util; |
| | | |
| | | import cn.hutool.core.io.IoUtil; |
| | | 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.product.common.lang.StringUtils; |
| | |
| | | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
| | | import org.apache.poi.util.Units; |
| | | import org.apache.poi.xwpf.usermodel.*; |
| | | import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; |
| | | import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; |
| | | import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.math.BigInteger; |
| | | import java.util.ArrayList; |
| | | import java.util.Base64; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | |
| | | |
| | | private final String indexKey = "~index~"; |
| | | |
| | | public DynamicTableRenderPolicy(String replaceKey) { |
| | | public DynamicTableRenderPolicy(String replaceKey) { |
| | | this.replaceKey = replaceKey; |
| | | |
| | | } |
| | | |
| | | private XWPFRun run; |
| | | private List<MetaTemplate> metaTemplateList; |
| | | |
| | | |
| | | |
| | | @Override |
| | | public void doRender(RenderContext<Object> context) throws Exception { |
| | |
| | | } |
| | | XWPFTableCell cell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody(); |
| | | XWPFTable table = cell.getTableRow().getTable(); |
| | | this.metaTemplateList = context.getTemplate().getElementTemplates(); |
| | | ElementTemplate eleTemplate = context.getEleTemplate(); |
| | | int index = metaTemplateList.indexOf(eleTemplate); |
| | | this.metaTemplateList = metaTemplateList.subList(index + 1, metaTemplateList.size()); |
| | | render(table, context.getData()); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | throw new RenderException("Dynamic render table error:" + e.getMessage(), e); |
| | | } |
| | | } |
| | |
| | | } |
| | | fieldNames[i] = replaceKey; |
| | | } |
| | | //获取fieldRow所在的下标 |
| | | int fieldRowIndex = xwpfTable.getRows().indexOf(fieldRow); |
| | | |
| | | for (int i = 0; i < subTableData.size(); i++) { |
| | | Map<String, Object> map = subTableData.get(i); |
| | | //创建一行在fieldRowIndex下面 |
| | | XWPFTableRow row = xwpfTable.insertNewTableRow(fieldRowIndex + 1 + i); |
| | | copyTableRow(row, fieldRow); |
| | | //设置row的属性与fieldRow一致 |
| | | row.setHeight(fieldRow.getHeight()); |
| | | //遍历字段每个字段创建一个单元格 |
| | | for (int j = 0; j < fieldNames.length; j++) { |
| | | //当前单元格 |
| | | XWPFTableCell cell; |
| | | //判断row中第j个单元格是否存在 |
| | | if (row.getTableCells().size() > j) { |
| | | cell = row.getCell(j); |
| | | 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 { |
| | | cell = row.createCell(); |
| | | //创建一行在fieldRowIndex下面 |
| | | row = xwpfTable.insertNewTableRow(xwpfTable.getRows().size()); |
| | | PrintPoiUtil.copyTableRow(row, fieldRow); |
| | | } |
| | | //设置单元格的值从map中取出 |
| | | //判断是否是序号列 |
| | | if (indexKey.equals(fieldNames[j])) { |
| | | cell.setText(String.valueOf(i + 1)); |
| | | continue; |
| | | } |
| | | Object value = map.get(fieldNames[j]); |
| | | if (value == null) { |
| | | value = ""; |
| | | } |
| | | String text = cell.getText(); |
| | | //删除单元格中的旧内容 |
| | | if (!StringUtils.isEmpty(text)) { |
| | | List<XWPFParagraph> paragraphs = cell.getParagraphs(); |
| | | if (paragraphs.size() > 1) { |
| | | cell.removeParagraph(1); |
| | | } |
| | | List<XWPFRun> runs = paragraphs.get(0).getRuns(); |
| | | //清空文字 |
| | | for (int k = 0; k < runs.size(); k++) { |
| | | runs.get(k).setText("", 0); |
| | | } |
| | | } |
| | | cell.setText(value.toString()); |
| | | if ("lx_flow_opinion".equals(this.replaceKey) && "opinion".equals(fieldNames[j]) && !StringUtils.isEmpty(map.get("sign_attachment_uuid"))) { |
| | | //意见框 插入签名图片到单元格右下角位置 |
| | | //遍历字段每个字段创建一个单元格 |
| | | for (int j = 0; j < fieldNames.length; j++) { |
| | | //当前单元格 |
| | | XWPFTableCell cell = row.getCell(j); |
| | | //清空单元格内容 |
| | | XWPFParagraph 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)); |
| | | inputStream.close(); |
| | | } catch (InvalidFormatException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | int index = cell.getParagraphs().indexOf(xwpfParagraph); |
| | | PrintPoiUtil.copyParagraph(xwpfParagraph, cell.getParagraphs().get(0)); |
| | | //删除所有的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 = ""; |
| | | } |
| | | xwpfRun.setText(value.toString(), 0); |
| | | if ("lx_flow_opinion".equals(this.replaceKey) && "opinion".equals(fieldNames[j]) && !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)); |
| | | inputStream.close(); |
| | | } catch (InvalidFormatException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | } else { |
| | | |
| | | // 删除fieldRow |
| | | int index = xwpfTable.getRows().indexOf(fieldRow); |
| | | xwpfTable.removeRow(index); |
| | | } |
| | | //删除起始行 |
| | | xwpfTable.removeRow(startRowIndex); |
| | | //删除fieldRow |
| | | xwpfTable.removeRow(fieldRowIndex - 1); |
| | | |
| | | // //读取完毕后删除最后一行 |
| | | // xwpfTable.removeRow(rows.size() - 1); |
| | | //获取所有的行判断单元格是否有值没有就删除该行 |
| | | // List<XWPFTableRow> tableRows = xwpfTable.getRows(); |
| | | // List<XWPFTableRow> deleteRows = new ArrayList<>(); |
| | | // for (int i = 0; i < tableRows.size(); i++) { |
| | | // XWPFTableRow row = tableRows.get(i); |
| | | // boolean flag = false; |
| | | // for (int j = 0; j < row.getTableCells().size(); j++) { |
| | | // XWPFTableCell cell = row.getTableCells().get(j); |
| | | // if (StringUtils.isNotEmpty(cell.getText())) { |
| | | // flag = true; |
| | | // break; |
| | | // } |
| | | // } |
| | | // if (!flag) { |
| | | // deleteRows.add(row); |
| | | // } |
| | | // } |
| | | // //遍历要删除的行获取所在的下标进行删除 |
| | | // for (int i = 0; i < deleteRows.size(); i++) { |
| | | // XWPFTableRow row = deleteRows.get(i); |
| | | // int index = xwpfTable.getRows().indexOf(row); |
| | | // xwpfTable.removeRow(index); |
| | | // } |
| | | // //遍历数据集合,每个map对应一行数据 |
| | | // for (int i = 0; i < subTableData.size(); i++) { |
| | | // Map<String, Object> map = subTableData.get(i); |
| | | // //创建一行 |
| | | // XWPFTableRow row = xwpfTable.createRow(); |
| | | // //遍历字段每个字段创建一个单元格 |
| | | // for (int j = 0; j < fieldNames.length; j++) { |
| | | // //当前单元格 |
| | | // XWPFTableCell cell; |
| | | // //判断row中第j个单元格是否存在 |
| | | // if (row.getTableCells().size() > j) { |
| | | // cell = row.getTableCells().get(j); |
| | | // } else { |
| | | // cell = row.createCell(); |
| | | // } |
| | | // //设置单元格的值从map中取出 |
| | | // //判断是否是序号列 |
| | | // if (indexKey.equals(fieldNames[j])) { |
| | | // cell.setText(String.valueOf(i + 1)); |
| | | // continue; |
| | | // } |
| | | // Object value = map.get(fieldNames[j]); |
| | | // if (value == null) { |
| | | // value = ""; |
| | | // } |
| | | // //设置单元格的值 |
| | | // cell.setText(value.toString()); |
| | | // } |
| | | // } |
| | | |
| | | } |
| | | |
| | | /** |