班级管理方面的小应用

填表人员的缺漏查询

目录

  • 填表人员的缺漏查询
    • 班级管理方面的小应用

班级管理方面的小应用

注:根据固定名单查询所缺漏的人员名单,如,寻查谁还没有填表格

package com.xie.excel;

import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 使用POI操作Excel文件,进行班级名单的查询比对,查所漏缺的人员名单
 */
public class POITest1 {
    /**
     * 容器 44为本班的人数,自己根据情况设计
     * */
    String[] SourceArray = new String[44];
    ArrayList<String> TargetSet = new ArrayList();
    ArrayList<String> SourceSet = new ArrayList();
    HashMap hashMap = new HashMap();
    /**
     * 在主机中的excel表的路径 SourcePath为模版名单路径,TargetPath为查询对比的名单路径
     * */
    String SourcePath = "E:\\班级信息库\\基本信息表\\班级信息.xlsx";
    String TargetPath = "C:\\Users\\29060\\Desktop\\TempFileStation\\22软工C1班_假期去向登记表.xlsx";

    @Test
    public void check() throws Exception {
        // 调用
        source(SourceSet,SourcePath,1,2);
        excelOperation(TargetSet,TargetPath,5,3);
        System.out.println("--------------分隔符-----------------");
        set(SourceArray);
        remove(TargetSet);
        // 打印
        System.out.println("已交 " + (SourceArray.length-hashMap.size()) + "人");
        System.out.println("未完成名单:" + hashMap.keySet());
    }

    /**
     * 从HashMap容器中,移除重复元素,移除值为不存在的元素
     * */
    public void remove(ArrayList<String> arrayList) {
        for (int i = 0; i < arrayList.size(); i++) {
            if (hashMap.get(arrayList.get(i)) == "不存在") {
                hashMap.remove(arrayList.get(i));
            }
        }
    }

    /**
     * 初始化,设置全部元素值为不存在,并遍历进HashMap容器
     * */
    public void set(String[] s) {
        for (int j = 0; j < s.length; j++) {
            hashMap.put(s[j],"不存在");
        }
    }

    /**
     * 对excel表格操作,把相应单元格中的数据元素遍历进集合容器中
     * */
    public void excelOperation(ArrayList<String> arrayList,String path,int rows, int cells) throws Exception {
        // 建立连接通道
        InputStream in = new FileInputStream(new File(path));
        //读取磁盘上已经存在的Excel文件
        XSSFWorkbook excel = new XSSFWorkbook(in);
        //读取Excel文件中的第一个Sheet页(表)
        XSSFSheet sheet = excel.getSheetAt(0);
        //获取Sheet中最后一行的行号
        int lastRowNum = sheet.getLastRowNum();
        // 循环遍历取出每一个
        for (int i = rows; i <= lastRowNum ; i++) {
            //获得某一行
            XSSFRow row = sheet.getRow(i);
            // 统一设置单元格的值的类型,解决遇数字报错
            row.getCell(cells).setCellType(CellType.STRING);
            //获得单元格对象的值,从某一行开始,每一行的第几个单元格对象的值
            String cellValue1 = row.getCell(cells).getStringCellValue();
            // 简单异常处理
            if (cellValue1.length() >= 4) {
                System.out.println("格式不对");
                System.out.println(cellValue1);
            }
            // 把遍历出来的元素放进集合中
            arrayList.add(cellValue1);
        }
        //关闭资源
        in.close();
        excel.close();
    }

    /**
     * 从excel表格读取固定确切名单,然后转化为数组
     * */
    public void source(ArrayList<String> SourceSet,String SourcePath,int row,int cell) throws Exception {
        excelOperation(SourceSet,SourcePath,row,cell);
        for (int i = 0; i < SourceSet.size(); i++) {
            SourceArray[i] = SourceSet.get(i);
        }
    }
}

你可能感兴趣的:(#,Java,java)