导入Excel如何判是否有重复的行

根据用户名的列跟身份证号判断是否有重复,如下图:返回用户名ff重复,行号为4,5,6,7
身份证号重复,行号为4,5
 代码:如下

导入Excel如何判是否有重复的行_第1张图片
 

package test;
import com.vodsys.vo.UserVO;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

/**
 * Created by huangy on 2018/7/17.
 */
public class TestExcelRepeat {



    public boolean batchAddUser( String newName) throws Exception{


         String filepath = newName;

        InputStream is = null;
        Workbook workbook = null;
        int count=0;


        HashMap map=new HashMap();

        HashMap tmap=new HashMap();


        HashMap map1=new HashMap();

        HashMap tmap1=new HashMap();





        List userVOS = new ArrayList();

        //UserMapper userMapper = session.getMapper(UserMapper.class);
        try {
            is = new FileInputStream(filepath);
            //读取excel
            String fileType = filepath.substring(filepath.lastIndexOf(".") + 1, filepath.length());
            if (fileType.equals("xls")) {
                workbook = new HSSFWorkbook(is);
            } else if (fileType.equals("xlsx")) {
                workbook = new XSSFWorkbook(is);
            } else {
                throw new Exception("读取的不是excel文件");
            }

            Sheet sheet = workbook.getSheetAt(0);
            int rowNum = sheet.getLastRowNum()+1;

            //i 从3开始,表头不读取
            for(int i=3;i 0 || tmap1.size() >0){

                String msg1 = "";


                if (tmap1.size()>0){
                    Iterator> it=tmap1.entrySet().iterator();

                    while(it.hasNext()){

                        Map.Entry entry = (Map.Entry) it.next();

                        msg1  += "用户名:" +entry.getKey() +" "+entry.getValue() +";";



                    }
                }




                if (tmap.size()>0){
                    Iterator> it=tmap.entrySet().iterator();
                    while(it.hasNext()){

                        Map.Entry entry = (Map.Entry) it.next();

                        msg1  += "身份证号:" +entry.getKey() +" "+entry.getValue() +";";

                        //System.out.println("字段:"+entry.getKey()+" "+entry.getValue());

                    }
                }


                System.out.println("重复的行:"+ msg1);

                return false;

              //  return  InvokeResultVO.Fail(msg1);
            }else{
                System.out.println("没有重复的行");
                return true;



            }


        } catch (Exception e) {

            throw new Exception(e);
        } finally {
            workbook=null;
            is.close();
//	           File file = new File(filepath);
//				  if(file.exists()){
//					  file.delete();
//				  }
        }

    }


    public String readCell(Cell cell) {
        String cellValue = null;
        switch(cell.getCellType()){ //判断excel单元格内容的格式,并对其进行转换,以便插入数据库
            case 0 : cellValue = String.valueOf((int)cell.getNumericCellValue()); break;
            case 1 : cellValue = cell.getStringCellValue(); break;
            case 2 : cellValue = String.valueOf(cell.getDateCellValue()); break;
            case 3 : cellValue = ""; break;
            case 4 : cellValue = String.valueOf(cell.getBooleanCellValue()); break;
            case 5 : cellValue = String.valueOf(cell.getErrorCellValue()); break;
            case 6 : cellValue = String.valueOf(cell.getErrorCellValue()); break;
        }
        return cellValue;
    }


    public static void main(String[] args) {
        TestExcelRepeat  testExcelRepeat = new TestExcelRepeat();
        try {
            testExcelRepeat.batchAddUser("E://user.xlsx");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
 

你可能感兴趣的:(导入Excel如何判是否有重复的行)