ApachePOI:使用多列进行重复行检测
创始人
2024-09-06 08:30:27
0

可以通过自定义Comparator和HashSet来实现多列检测重复行的功能。以下是示例代码:

import org.apache.poi.ss.usermodel.*;

import java.util.HashSet;
import java.util.Comparator;

public class ExcelRowComparator implements Comparator {
    private Workbook workbook;
    private int[] columnsToCompare;

    public ExcelRowComparator(Workbook workbook, int[] columnsToCompare) {
        this.workbook = workbook;
        this.columnsToCompare = columnsToCompare;
    }

    /**
     * Overrides the compare method of Comparator interface and implements
     * logic for comparing two rows based on multiple columns
     *
     * @param row1 The first row to compare
     * @param row2 The second row to compare
     * @return A negative integer, zero, or a positive integer as the first row
     * is less than, equal to, or greater than the second.
     */
    @Override
    public int compare(Row row1, Row row2) {
        // Compare values of specified columns in the two rows
        for (int i = 0; i < columnsToCompare.length; i++) {
            int columnToCompare = columnsToCompare[i];
            Cell cell1 = row1.getCell(columnToCompare);
            Cell cell2 = row2.getCell(columnToCompare);
            if (cell1 != null && cell2 != null) {
                int compareResult = compareCell(cell1, cell2);
                if (compareResult != 0) {
                    return compareResult;
                }
            }
        }
        return 0;
    }

    /**
     * Compares two cells based on the type of cell value
     *
     * @param cell1 The first cell to compare
     * @param cell2 The second cell to compare
     * @return A negative integer, zero, or a positive integer as the first cell
     * is less than, equal to, or greater than the second.
     */
    private int compareCell(Cell cell1, Cell cell2) {
        // Check cell type
        if (cell1.getCellType() == CellType.NUMERIC && cell2.getCellType() == CellType.NUMERIC) {
            return Double.compare(cell1.getNumericCellValue(), cell2.getNumericCellValue());
        } else

相关内容

热门资讯

透视免费!wpk俱乐部辅助器,... 透视免费!wpk俱乐部辅助器,wpk插件辅助,科技教程(有挂揭秘)1、每一步都需要思考,不同水平的挑...
透视游戏!wpk真吗,wpk透... 透视游戏!wpk真吗,wpk透视怎么安装,科技教程(有挂规律)1、金币登录送、破产送、升级送、活动送...
透视美元局!wpk有辅助器吗,... 透视美元局!wpk有辅助器吗,wpk德州局透视,规律教程(有挂黑科技)运wpk德州局透视辅助工具,进...
透视好友房!wpk俱乐部是真的... 透视好友房!wpk俱乐部是真的吗,wpk安卓下载辅助,解说技巧(有挂黑科技)1、让任何用户在无需wp...
透视最新!如何下载wpk透视版... 透视最新!如何下载wpk透视版,wpk私人辅助,详细教程(有挂脚本)如何下载wpk透视版软件透明挂微...
透视私人局!wpk有那种辅助吗... 透视私人局!wpk有那种辅助吗,wpk俱乐部有没有辅助,专业教程(有挂教程)1、wpk俱乐部有没有辅...
透视了解!wpk模拟器是什么,... 透视了解!wpk模拟器是什么,wpk模拟器是什么,爆料教程(有挂细节);1、进入游戏-大厅左侧-新手...
辅助透视!wpk插件,wpk透... 辅助透视!wpk插件,wpk透视辅助靠谱吗,攻略教程(有挂细节);所有人都在同一条线上,像星星一样排...
透视科技!wpk控制牌是真的吗... 透视科技!wpk控制牌是真的吗,wpk透视辅助,规律教程(有挂技巧);1、超多福利:超高返利,海量正...
透视辅助!wpk作弊是真的吗,... 透视辅助!wpk作弊是真的吗,wpk私人局有透视吗,安装教程(有挂插件);1、wpk私人局有透视吗透...