Excel 匹配模板处理工具

本文介绍了如何利用特定工具进行Excel模板的匹配和处理,详细阐述了操作步骤和jsonhandler的使用方法,帮助用户高效地管理和操作Excel数据。
摘要由CSDN通过智能技术生成
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.util.CollectionUtils;

import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Excel 匹配模板处理工具  通配包含  ${}和#{}
 * ${}  字段模板 将${}及里面的内容更换成指定json的内容
 * #{}  集合模板 将#{}及里面的内容更换成指定json的内容
 *      compile group: 'org.apache.poi', name: 'poi', version: '4.1.2'
 *      compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'
 *      compile group: 'org.apache.poi', name: 'poi-ooxml-schemas', version: '4.1.2'
 * @Description
 * @Author 不爱写代码的码农
 * @Date 2020/7/13 14:03
 */
@Slf4j
public class ExcelProcessUtils {

    private final static Pattern fieldPatternAll = Pattern.compile("\\$\\{(.+?)}");
    private final static Pattern listPatternAll = Pattern.compile("\\#\\{(.+?)}");
    private final static String fieldMatchAll = "\\$\\{[^\\}]+\\}";
    private final static String listMatchAll = "\\#\\{[^\\}]+\\}";

    /**
     * 行复制功能
     *
     * @param wb            工作簿
     * @param fromRow       从哪行开始
     * @param toRow         目标行
     * @param copyValueFlag true则连同cell的内容一起复制
     */
    public static void copyRow(Workbook wb, Row fromRow, Row toRow, boolean copyValueFlag) {
        toRow.setHeight(fromRow.getHeight());

        for (Iterator<Cell> cellIt = fromRow.cellIterator(); cellIt.hasNext(); ) {
            Cell tmpCell = cellIt.next();
            Cell newCell = toRow.createCell(tmpCell.getColumnIndex());
            copyCell(wb, tmpCell, newCell, copyValueFlag);
        }

        Sheet worksheet = fromRow.getSheet();

        for (int i = 0; i < worksheet.getNumMergedRegions(); i++) {
            CellRangeAddress cellRangeAddress = worksheet.getMergedRegion(i);
            if (cellRangeAddress.getFirstRow() == fromRow.getRowNum()) {
                CellRangeAddress newCellRangeAddress = new CellRangeAddress(toRow.getRowNum(),
                        (toRow.getRowNum() + (cellRangeAddress.getLastRow() - cellRangeAddress.getFirstRow())),
                        cellRangeAddress.getFirstColumn(), cellRangeAddress.getLastColumn());
                worksheet.addMergedRegionUnsafe(newCellRangeAddress);
            }
        }
    }


    /**
     * 复制单元格
     *
     * @param wb            wb
     * @param srcCell       被复制cell
     * @param distCell      复制cell
     * @param copyValueFlag true则连同cell的内容一起复制
     */
    private static void copyCell(Workbook wb, Cell srcCell, Cell distCell, boolean copyValueFlag) {
        CellStyle newStyle = wb.createCellStyle();
        CellStyle srcStyle = srcCell.getCellStyle();

        newStyle.cloneStyleFrom(srcStyle);
        newStyle.setFont(wb.getFontAt(srcStyle.getFontIndex()));

        // 样式
        distCell.setCellStyle(newStyle);

        // 内容
        if (srcCell.getCellComment() != null) {
            distCell.setCellComment(srcCell.getCellComment());
        }

        // 不同数据类型处理
        CellType srcCellType = srcCell.getCellType();
        distCell.setCellType(srcCellType);

        if (copyValueFlag) {
            if (srcCellType == CellType.NUMERIC) {
                if (DateUtil.isCellDateFormatted(srcCell)) {
                    distCell.setCellValue(srcCell.getDateCellValue());
                } else {
                    distCell.setCellValue(srcCell.getNumericCellValue());
                }
            } else if (srcCellType == CellType.STRING) {
                distCell.setCellValue(srcCell.getRichStringCellValue());
            } else if (srcCellType == CellType.BLANK) {

            } else if (srcCellType == CellType.BOOLEAN) {
                distCell.setCellValue(srcCell.getBooleanCellValue());
            } else if (srcCellType == CellType.ERROR) {
                distCell.setCellErrorValue(srcCell.getErrorCellValue());
            } else if (srcCellType == CellType.FORMULA) {
                distCell.setCellFormula(srcCell.getCellFormula());
            }
        }
    }

    /**
     * cell处理
     *
     * @param cell       处理的cell
     * @param jsonObject 所有数据
     */
    public static void cellProcess(Cell cell, JSONObject jsonObject, List<ExcelProcessDto> excelProcessDtos, Row row) {
        String content = getCellStringValue(cell);
        if (fieldPatternAll.matcher(content).find()) {
            // 匹配到字段
            String cellValue = fieldProcess(content, jsonObject);
            cell.setCellValue(cellValue);
        } else if (listPatternAll.matcher(content).find()) {
            // 匹配到列表
            String listName = ExcelProcessUtils.getListName(content);
            int rowNum = row.getRowNum();
            ExcelProcessDto excelProcessDto = new ExcelProcessDto();
            if (CollectionUtils.isEmpty(excelProcessDtos)) {
                excelProcessDto.setListName(listName);
                excelProcessDto.setRowNum(rowNum);
                excelProcessDtos.add(excelProcessDto);
                return;
            }
            for 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
软件介绍: 两个Excel表格匹配并合并Excel文件信息匹配软件使用说明软件功能本软件能实现两个excel文件之间整条数据按某一个列进行匹配,并将匹配成功的行数据合并为一整行,之后输出到新的excel中。(匹配的源文件中,未匹配成功的行也会输出到新的excel中)。例如:A表有数据内容  姓名 工号 年龄    张三 9001 23    李四 9002 45  B表有数据内容 姓名 性别 学历    张三 男 本科生    王丽 女 研究生  以A表的作为匹配的源文件,A表第一列作为匹配内容;B表作为搜索的文件,B表第一列作为匹配内容,匹配结果为新表数据内容姓名 工号 年龄 姓名 性别 学历    张三 9001 23 张三 男 本科生    李四 9002 45     软件使用指南指定需要进行匹配excel表格。需要在软件的根目录里,对“excel匹配设置.txt”进行修改(如没有此文件,请自行创建,文件名必须保持一致)。“excel匹配设置.txt”包含四行内容,如软件中的示例:各行的含义如下:第一行:匹配excel源文件的绝对路径(如果文件在本软件根目录下,请直接输入文件名称即可)。第二行:源文件里需要进行匹配的列数。第三行:被搜索的excel文件的绝对路径(如果文件在本软件根目录下,请直接输入文件名称即可)。第四行:被搜索文件中进行匹配的列数。按上文的文本框内的配置,软件将会依照根目录中《拟录取名单》中第2列的内容,在根目录中《硕士生复试名单》的第一列搜索匹配(相同)的数据,并将《硕士生复试名单》中匹配成功的整行数据复制到《拟录取名单》相应行的末尾空白处,之后输出到一个新的excel文件里(文件在软件的根目录中)。建议在使用时候,将文件都拷贝到软件的根目录进行操作。双击运行“run.bat”文件如果系统提示无法运行,请右键点击“run.bat”文件,选择“以管理员身份运行”。特别提醒本软件只支持对xls格式的excel文件进行匹配,无法匹配xlsx格式的excel文件,请在进行匹配之前,自行做好格式转换。在输出新的excel文件以后,打开时可能会报错(如下图),此为您使用的excel软件版本较新,与旧版本(xls格式)不太兼容,并不意味着数据丢失,请在文件打开以后,另存为其他文件即可。本人开发此工具仅为了便于个人使用,允许一切人员对其进行使用。虽然在个人使用过程中没有出现数据匹配不准确的问题,但本人不对数据合并后的准确性做任何的保证。在使用本软件时候,请对需要进行匹配excel文件进行备份。如果数据丢失、覆写或误删,本人不承担任何责任。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值