许鹏程
2023-06-29 1bbac1a2af0a774962e3f2398ceb1efb531da161
poi、easyexcel、poi-tl升级 ,合并空调中的报表、数据源模块
已修改2个文件
233 ■■■■ 文件已修改
src/main/java/com/product/print/config/CmnCode.java 6 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/print/util/DynamicTableRenderPolicy.java 227 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/product/print/config/CmnCode.java
@@ -28,8 +28,10 @@
    REPLACE_TEMPLATE_CONTENT_ERROR("替换模板内容错误", 13),
    //转换pdf错误
    CONVERT_PDF_ERROR("转换pdf错误", 14),
    //格中没有找到结束标识{{$~end~}}
    NOT_FIND_END_FLAG("格中没有找到结束标识{{$~end~}}", 15),
    //表格中没有找到起始下标
    NOT_FIND_START_FLAG("表格中没有找到起始下标", 15),
    //子表格中没有找到对应的字段
    NOT_FIND_CHILD_TABLE_FIELD("子表格中没有找到对应的字段", 16),
    ;
    private String text;
src/main/java/com/product/print/util/DynamicTableRenderPolicy.java
@@ -7,8 +7,12 @@
import com.product.common.lang.StringUtils;
import com.product.core.exception.BaseException;
import com.product.print.config.CmnCode;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xwpf.usermodel.*;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@@ -58,53 +62,102 @@
        List<Map<String, Object>> subTableData = (List<Map<String, Object>>) data;
        List<XWPFTableRow> rows = xwpfTable.getRows();
        //读取rows中的内容
        //获取最后一行
        XWPFTableRow xwpfTableRow = rows.get(rows.size() - 1);
        String tableExpression = "{{" + this.replaceKey + "}}";
        //获取表格起始行和结束行
        int startRowIndex = -1;
        int endRowIndex = -1;
        rows:
        for (int i = 0; i < rows.size(); i++) {
            XWPFTableRow row = rows.get(i);
            for (int j = 0; j < row.getTableCells().size(); j++) {
                if (startRowIndex > -1 && endRowIndex > -1) {
                    break rows;
                }
                XWPFTableCell cell = row.getTableCells().get(j);
                String text = cell.getText();
                if (tableExpression.equals(text)) {
                    startRowIndex = i;
                }
                if ("{{$~end~}}".equals(text)) {
                    endRowIndex = i;
                    break rows;
                }
            }
        }
        //表格起始行不在第一行且没有找到结束标记
        if (startRowIndex > 0 && endRowIndex == -1) {
            throw new BaseException(CmnCode.NOT_FIND_END_FLAG);
        if (startRowIndex == -1) {
            throw new BaseException(CmnCode.NOT_FIND_START_FLAG);
        }
        if (startRowIndex == 0 && endRowIndex > 0) {
            //删除结束标记所在行
            xwpfTable.removeRow(endRowIndex);
        //在表格中查找子表字段以{{$开头}}以}}结尾的内容
        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[xwpfTableRow.getTableCells().size()];
//        for (int i = 0; i < xwpfTableRow.getTableCells().size(); i++) {
//            XWPFTableCell cell = xwpfTableRow.getTableCells().get(i);
//            String text = cell.getText();
//            String replaceKey = getReplaceKey(text);
//            //获取表达式中的值
//            if (StringUtils.isEmpty(replaceKey)) {
//                //设置单元格为空值
//                replaceKey = "";
//            }
//            fieldNames[i] = replaceKey;
//        }
        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;
        }
        //获取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.getTableCells().get(j);
                    //清空单元格内容
                    cell.removeParagraph(0);
                } 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());
            }
        }
        //删除起始行
        xwpfTable.removeRow(startRowIndex);
        //删除fieldRow
        xwpfTable.removeRow(fieldRowIndex - 1);
//        //读取完毕后删除最后一行
//        xwpfTable.removeRow(rows.size() - 1);
        //获取所有的行判断单元格是否有值没有就删除该行
@@ -174,4 +227,124 @@
        }
        return null;
    }
    /**
     * 复制行,从source到target
     *
     * @param target
     * @param source
     */
    public void copyTableRow(XWPFTableRow target, XWPFTableRow source) {
        // 复制样式
        if (source.getCtRow() != null) {
            target.getCtRow().setTrPr(source.getCtRow().getTrPr());
        }
        // 复制单元格
        for (int i = 0; i < source.getTableCells().size(); i++) {
            XWPFTableCell cell1 = target.getCell(i);
            XWPFTableCell cell2 = source.getCell(i);
            if (cell1 == null) {
                cell1 = target.addNewTableCell();
            }
            copyTableCell(cell1, cell2);
        }
    }
    /**
     * 复制单元格,从source到target
     *
     * @param target
     * @param source
     */
    public void copyTableCell(XWPFTableCell target, XWPFTableCell source) {
        // 列属性
        if (source.getCTTc() != null) {
            target.getCTTc().setTcPr(source.getCTTc().getTcPr());
        }
        // 删除段落
        for (int pos = 0; pos < target.getParagraphs().size(); pos++) {
            target.removeParagraph(pos);
        }
        // 添加段落
        for (XWPFParagraph sp : source.getParagraphs()) {
            XWPFParagraph targetP = target.addParagraph();
            copyParagraph(targetP, sp);
        }
    }
    /**
     * 复制图片到target
     *
     * @param target
     * @param picture
     * @throws IOException
     * @throws InvalidFormatException
     */
    public void copyPicture(XWPFRun target, XWPFPicture picture) throws IOException, InvalidFormatException {
        String filename = picture.getPictureData().getFileName();
        InputStream pictureData = new ByteArrayInputStream(picture
                .getPictureData().getData());
        int pictureType = picture.getPictureData().getPictureType();
        int width = (int) picture.getCTPicture().getSpPr().getXfrm().getExt()
                .getCx();
        int height = (int) picture.getCTPicture().getSpPr().getXfrm().getExt()
                .getCy();
        // target.addBreak();
        target.addPicture(pictureData, pictureType, filename, width, height);
        // target.addBreak(BreakType.PAGE);
    }
    /**
     * 复制段落,从source到target
     *
     * @param target
     * @param source
     */
    public void copyParagraph(XWPFParagraph target, XWPFParagraph source) {
        // 设置段落样式
        target.getCTP().setPPr(source.getCTP().getPPr());
        // 移除所有的run
        for (int pos = target.getRuns().size() - 1; pos >= 0; pos--) {
            target.removeRun(pos);
        }
        // copy 新的run
        for (XWPFRun s : source.getRuns()) {
            XWPFRun targetrun = target.createRun();
            copyRun(targetrun, s);
        }
    }
    /**
     * 复制RUN,从source到target
     *
     * @param target
     * @param source
     */
    public void copyRun(XWPFRun target, XWPFRun source) {
        // 设置run属性
        target.getCTR().setRPr(source.getCTR().getRPr());
        // 设置文本
        target.setText(source.text());
        // 处理图片
        List<XWPFPicture> pictures = source.getEmbeddedPictures();
        for (XWPFPicture picture : pictures) {
            try {
                copyPicture(target, picture);
            } catch (InvalidFormatException e) {
                logger.error("copyRun", e);
            } catch (IOException e) {
                logger.error("copyRun", e);
            }
        }
    }
}