java 對兩個excal進行比較

java初學者一直在努力用java來做一些小東西,讓我們的工作變得簡單,前幾天老師要求從後臺調出excal文件,每個班從中挑出自己班級學生名單,然後對比找出自己班級未在名單中的學生。這其實是很無趣的事情,所以想一勞永逸,在此分享一些源碼:
在這裏插入圖片描述
架包是:jxl.jar 可以自行下載導入即可

在這裏插入代碼片
package test;

import jxl.*;
import jxl.read.biff.BiffException;
import java.io.*;
import java.util.regex.Pattern;

public class demo {
	public static void main(String[] args) throws BiffException, IOException {
		System.out.println("17網工班人員名單統計");
		// 讀取表格中第一個sheet表
		String[] result1 = getSheet("報名列表");// 17網工成員導出
		
		System.out.println("信息學院已學習青年大學習人員名單統計");
		// 讀取表格中第二個sheet表
		String[] result2 = getSheet1("報名列表");// 成員導出信息學院
		
		CompareResult(result1, result2);//對比兩個表 並輸出17網工班未填表人名單

	}

	public static String[] getSheet(String sh) throws BiffException,
			IOException {
		String[] result;

		Workbook book = Workbook.getWorkbook(new File("17網工成員導出.xls"));// 放在java項目下的文件名
		// 獲得excel文件的sheet表
		Sheet sheet = book.getSheet(sh);

		int rows = sheet.getRows();// 行數
		int cols = sheet.getColumns();// 列數

		System.out.println("總列數:" + cols);
		System.out.println("總行數:" + rows);
		System.out.println("----------------------------");
		result = new String[rows];
		int i = 0;
		// 循環讀取數據
		for (i = 1; i < rows; i++) {

			result[i] = new String(sheet.getCell(0, i).getContents());// getCell(x,y)
																		// 第y行的第x列
		}
		return result;
	}

	public static String[] getSheet1(String sh) throws BiffException,
			IOException {
		String[] result;

		Workbook book1 = Workbook.getWorkbook(new File("成員導出.xls"));

		Sheet sheet = book1.getSheet(sh);

		int rows1 = sheet.getRows();// 行數
		int cols1 = sheet.getColumns();// 列數

		System.out.println("總列數:" + cols1);
		System.out.println("總行數:" + rows1);
		System.out.println("----------------------------");
		result = new String[rows1];
		int i = 0;
		// 循環讀取數據
		for (i = 1; i < rows1; i++) {
			// getCell(x,y) 第y行的第x列
			result[i] = new String(sheet.getCell(0, i).getContents());
		}
		return result;
	}

	public static void CompareResult(String[] result1, String[] result2) {
		int count = 0;
		System.out.println("17網工班未進行青年大學習人員名單:");
		for (int i = 1; i < result1.length; i++) {
			if (result1[i] != null) {
				for (int j = 1; j < result2.length; j++) {
					
					 Pattern regex = Pattern.compile(result1[i]);
					 
				       if(regex.matcher(result2[j]).find()==true){
				    	   count = 1;
				       } // true
//					if (result1[i].equals(result2[j])) {
//						count = 1;
//					}
				}
				if (count == 0) {														
					System.out.println(result1[i]);
				}
			}
			count = 0;

		}

	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章