Java比较两份excel-Apache POI

1:下列代码为Java比较两份excel 或者同一份excel的两个不同的sheet每一行是否相同;同时提供模糊匹配和精准匹配两个方案可以选择。大家如果有更好的方法 欢迎大家在下面留言哦...

  A:读取excel组装成list集合

public class RWExcel {
	
	private String filePath;
	private String anotherfilePath;

	
	/**
	 * 构造方法
	 * */
	
	public RWExcel(String filePath,String anotherfilePath){
		
		this.filePath = filePath;
		this.anotherfilePath = anotherfilePath;
	}

	/**
	 * 
	 * 读取excel 封装成集合
	 * 该程序需要传入一份excel 和excel的列数 行数由程序自动检测
	 * 注意:该方法统计的行数是默认第一行为title 不纳入统计的
	 * 
	 * @return
	 * 
	 */
	// @Test
	public ArrayList<List> ReadExcel(int sheetNum) {

		// int column = 5;//column表示excel的列数

		ArrayList<List> list = new ArrayList<List>();

		try {
			// 建需要读取的excel文件写入stream
			HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filePath));
			// 指向sheet下标为0的sheet 即第一个sheet 也可以按在sheet的名称来寻找
			HSSFSheet sheet = workbook.getSheetAt(sheetNum);
			// 获取sheet1中的总行数
			int rowTotalCount = sheet.getLastRowNum();
			//获取总列数
			int columnCount = sheet.getRow(0).getPhysicalNumberOfCells();
			
			//System.out.println("行数为:"+rowTotalCount+"列数为:"+columnCount);

			for (int i = 0; i <= rowTotalCount; i++) {
				// 获取第i列的row对象
				HSSFRow row = sheet.getRow(i);
				
				ArrayList<String> listRow = new ArrayList<String>();

				for (int j = 0; j < columnCount; j++) {
					//下列步骤为判断cell读取到的数据是否为null 如果不做处理 程序会报错
					String cell = null;
					//如果未null则加上""组装成非null的字符串
					if(row.getCell(j) == null){
						
						cell = row.getCell(j)+"";
						
						listRow.add(cell);
					//如果读取到额cell不为null 则直接加入	listRow集合
					}else{
						cell = row.getCell(j).toString();
						listRow.add(cell);
					}
					// 在第i列 依次获取第i列的第j个位置上的值 %15s表示前后间隔15个字节输出
					//System.out.printf("%15s", cell);

				}

				list.add(listRow);

				//System.out.println();
			}

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}

		return list;
	}
	
	/**
	 * 读取另外一份Excel 保存成list集合
	 * */
	
	public ArrayList<List> ReadAnotherExcel(int anotherSheetNum){
		

		ArrayList<List> list = new ArrayList<List>();

		try {
			// 建需要读取的excel文件写入stream
			HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(anotherfilePath));
			// 指向sheet下标为0的sheet 即第一个sheet 也可以按在sheet的名称来寻找
			HSSFSheet sheet = workbook.getSheetAt(anotherSheetNum);
			// 获取sheet1中的总行数
			int rowTotalCount = sheet.getLastRowNum();
			//获取总列数
			int columnCount = sheet.getRow(0).getPhysicalNumberOfCells();
			
			//System.out.println("行数为:"+rowTotalCount+"列数为:"+columnCount);

			for (int i = 0; i <= rowTotalCount; i++) {
				// 获取第i列的row对象
				HSSFRow row = sheet.getRow(i);
				
				ArrayList<String> listRow = new ArrayList<String>();

				for (int j = 0; j < columnCount; j++) {
					//下列步骤为判断cell读取到的数据是否为null 如果不做处理 程序会报错
					String cell = null;
					//如果未null则加上""组装成非null的字符串
					if(row.getCell(j) == null){
						
						cell = row.getCell(j)+"";
						
						listRow.add(cell);
					//如果读取到额cell不为null 则直接加入	listRow集合
					}else{
						cell = row.getCell(j).toString();
						listRow.add(cell);
					}
					// 在第i列 依次获取第i列的第j个位置上的值 %15s表示前后间隔15个字节输出
					//System.out.printf("%15s", cell);

				}

				list.add(listRow);

				//System.out.println();
			}

		} catch (FileNotFoundException e) {

			e.printStackTrace();
		} catch (IOException e) {

			e.printStackTrace();
		}

		return list;
	}
	
	
	/**
	 * 调试方法
	 * */

//	public static void main(String[] args) {
//
//		 RWExcel excel = new RWExcel("D:\\345.xls", "D:\\345.xls");
//		 
//		 ArrayList<List> list1 = excel.ReadExcel(0);
//
//		 ArrayList<List> list2 = excel.ReadAnotherExcel(1);
//		 
//		 for (List list : list1) {
//			
//			 System.out.println(list.toString());
//		}
//		 
//		 System.out.println("==========================");
//		 
//		 for (List list : list2) {
//				
//			 System.out.println(list.toString());
//		}
//	}
}

   B:对比两个list集合是否想同 包含两个方法可选isPerfectMatch=true为精准匹配 isPerfectMatch=false为模糊匹配方式。

/**
 * 比对两份excel或者两份sheet是否相同 有精准匹配/模糊匹配两种方式
 * @author AnndyTou
 * @Time 2018-07-17
 * @Detail  
 * */
import java.util.ArrayList;
import java.util.List;

/**
 * 调用RWExcel.java 获取两份excel封装成ArrrayList集合 通过集合的下标进行比对
 */

public class CompareTwoExcels {

	private String exportExcelFilePath;
	private String WebExcelFilePath;
	private boolean isPerfectMatch;

	/**
	 * 构造方法
	 * @isPerfectMatch 表示是否需要完全匹配
	 */

	public CompareTwoExcels(String exportExcelFilePath, String WebExcelFilePath, boolean isPerfectMatch) {

		this.exportExcelFilePath = exportExcelFilePath;
		this.WebExcelFilePath = WebExcelFilePath;
		this.isPerfectMatch = isPerfectMatch;
	}

	/**
	 * 逻辑比对
	 */

	public void comparedExcels(int exportExcelSheetNum, int WebExcelSheetNum) {

		RWExcel excel = new RWExcel(exportExcelFilePath, WebExcelFilePath);

		ArrayList<List> exportExcel_list = excel.ReadExcel(exportExcelSheetNum);

		ArrayList<List> WebExcel_list = excel.ReadAnotherExcel(WebExcelSheetNum);
		// 条件判断是否完全匹配方式去匹配两份excel
		if (isPerfectMatch) {
			// 以web端获取的excel报表为参考 比对导出的excel报表
			for (int i = 1; i <= WebExcel_list.size(); i++) {
				List list1 = WebExcel_list.get(i-1);
				List list2 = exportExcel_list.get(i-1);
				
				if (list2.equals(list1)) {
					String infos = "Success-----报表第" + i + "行完全匹配-------成功";
					System.out.println(infos);
				} else {
					String infos = "Fail-----报表第" + i + "行Web端与导出的报表不匹配-------失败";
					System.out.println(infos);
				}
			}

		}else if(!isPerfectMatch){
			// 以web端获取的excel报表为参考 比对导出的excel报表
			for (int i = 1; i <= WebExcel_list.size(); i++) {
				List list1 = WebExcel_list.get(i-1);
				List list2 = exportExcel_list.get(i-1);
				
				if(list1.size() > list2.size()){
					System.out.println("Fail-----第"+i+"行Web端大于导出的报表长度-------失败");
				}else if(list1.size() < list2.size()){
					System.out.println("Fail-----第"+i+"行Web端小于导出的报表长度-------失败");
				}else{
					for(int j = 1;j<= list1.size(); j++){
						
						if(list2.contains(list1.get(j-1))){
							System.out.println("Success-----第"+i+"行第"+j+"列Web端与导出的报表匹配-----成功");
						}else{
							System.out.println("Fail-----第"+i+"行第"+j+"列Web端与导出的报表不匹配----------失败");
						}
					}
				}
			}
		}
	}
	
	/**
	 * 调试方法
	 * */
	
	public static void main(String[] args) {
		//第一份excel为web端抓取的excel  第二份excel为导出的excel false表示不精准匹配
		CompareTwoExcels excels = new CompareTwoExcels("D:\\345.xls", "D:\\345.xls", false);
		//选择xls的sheet下标
		excels.comparedExcels(0, 1);

	}

}

3:针对上面的读取excel不完全匹配的情况下的改进,将ArrayList改成HashList HashList会自动删除重复的数据并且按照hashcode对集合中的字符串进行排序

package test;

/**
 * 比对两份excel或者两份sheet是否相同 有精准匹配/模糊匹配两种方式
 * @author AnndyTou
 * @Time 2018-07-17
 * @Detail  
 * */
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * 调用RWExcel.java 获取两份excel封装成ArrrayList集合 通过集合的下标进行比对
 */

public class CompareTwoExcels {

	private String exportExcelFilePath;
	private String WebExcelFilePath;
	private boolean isPerfectMatch;

	/**
	 * 构造方法
	 * 
	 * @isPerfectMatch 表示是否需要完全匹配
	 */

	public CompareTwoExcels(String exportExcelFilePath, String WebExcelFilePath, boolean isPerfectMatch) {

		this.exportExcelFilePath = exportExcelFilePath;
		this.WebExcelFilePath = WebExcelFilePath;
		this.isPerfectMatch = isPerfectMatch;
	}

	/**
	 * 逻辑比对
	 */

	public void comparedExcels(int exportExcelSheetNum, int WebExcelSheetNum) {

		RWExcel excel = new RWExcel(exportExcelFilePath, WebExcelFilePath);

		ArrayList<List> exportExcel_list = excel.ReadExcel(exportExcelSheetNum);

		ArrayList<List> WebExcel_list = excel.ReadAnotherExcel(WebExcelSheetNum);
		// 条件判断是否完全匹配方式去匹配两份excel
		if (isPerfectMatch) {
			// 以web端获取的excel报表为参考 比对导出的excel报表
			for (int i = 1; i <= WebExcel_list.size(); i++) {
				List list1 = WebExcel_list.get(i - 1);
				List list2 = exportExcel_list.get(i - 1);

				if (list2.equals(list1)) {
					String infos = "Success-----报表第" + i + "行完全匹配-------成功";
					System.out.println(infos);
				} else {
					String infos = "Fail-----报表第" + i + "行Web端与导出的报表不匹配-------失败";
					System.out.println(infos);
				}
			}

		} else if (!isPerfectMatch) {
			// 以web端获取的excel报表为参考 比对导出的excel报表
			for (int i = 1; i <= WebExcel_list.size(); i++) {
				List list1 = WebExcel_list.get(i - 1);
				List list2 = exportExcel_list.get(i - 1);
				//根据HashSet的特点 不能存储重复的数据 并且狐疑按照码表进行排列 所有 HashSet集合可以忽视顺序
				Set<String> hashSet1 = new HashSet<>();
				Set<String> hashSet2 = new HashSet<>();

				if (list1 != null && list2 != null) {

					for (int q = 0; q < list1.size(); q++) {

						hashSet1.add((String) list1.get(q));
					}

					for (int v = 0; v < list2.size(); v++) {

						hashSet2.add((String) list2.get(v));
					}
				}
				//调试
				//System.out.println(hashSet1.toString()+"=========="+hashSet2.toString());
				
				

				if (hashSet2.size() != hashSet1.size()) {
					System.out.println("Fail-----第" + i + "行Web端与导出的报表长度不等-------失败");
					
					//System.out.println(hashSet1.toString()+"=========="+hashSet2.toString());
				}else {
					if(hashSet2.equals(hashSet1)){
						
						System.out.println("Success-----第" + i + "行Web端与导出的报表匹配-------成功");
					}else{
						System.out.println("Fail-----第" + i + "行Web端与导出的报表长度不等-------失败");
					}
					
					//System.out.println(hashSet1.toString()+"=========="+hashSet2.toString());
				}
			}
		}
	}

	/**
	 * 调试方法
	 */

	public static void main(String[] args) {
		// 第一份excel为web端抓取的excel 第二份excel为导出的excel false表示不精准匹配
		CompareTwoExcels excels = new CompareTwoExcels("D:\\345.xls", "D:\\345.xls", true);
		// 选择xls的sheet下标
		excels.comparedExcels(0, 1);

	}
	
	/**
	 * 封装一个方法 统计一个字符串在一个set集合中的个数  在本class中没有用武之地
	 * */
	
	public int setList(Set<String> hashSet , String str){
		
		int count = 0;
		
		//set集合不能使用get(index)的方法取值
		Iterator<String> iterator = hashSet.iterator();
		
		while(iterator.hasNext()){
			
			if(str.equals(iterator.next())){
				
				count++;
			}
		}
		
		return count;

	}
	
	/**
	 * 比较
	 * */

}

猜你喜欢

转载自blog.csdn.net/hujyhfwfh2/article/details/81082326
今日推荐