根據用戶名的列跟身份證號判斷是否有重複,如下圖:返回用戶名ff重複,行號爲4,5,6,7
身份證號重複,行號爲4,5
代碼:如下
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<String,String> map=new HashMap<String,String>();
HashMap<String,String> tmap=new HashMap<String,String>();
HashMap<String,String> map1=new HashMap<String,String>();
HashMap<String,String> tmap1=new HashMap<String,String>();
List<UserVO> userVOS = new ArrayList<UserVO>();
//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<rowNum;i++){
UserVO user = new UserVO();
Row row = sheet.getRow(i);
int cellNum = row.getLastCellNum();
for(int j=0;j<cellNum;j++){
Cell cell = row.getCell(j,Row.CREATE_NULL_AS_BLANK);
String cellValue = readCell(cell);
if(cellValue == null || "".equals(cellValue))
continue;
switch(j){//通過列數來判斷對應插如的字段
case 0 : user.setUsername(cellValue);break;
case 1 : user.setPassword(cellValue);break;
case 2 : user.setRealname(cellValue);break;
case 3 : user.setSex(cellValue);break;
case 4 : user.setTel(cellValue);break;
case 5 : user.setEmail(cellValue);break;
case 6 : user.setIdCard(cellValue);break;
}
}
if(user.getPassword()==null || user.getPassword().equals("")){
user.setPassword("123456");
}
if("".equals(user.getSex())){
user.setSex("男");
}
if (user.getIdCard()!=null && !user.getIdCard().equals("")){
int newNum=i+1;
if(map.containsKey(user.getIdCard())){//如果Map集合中包含指定的鍵名,則返回true;否則返回false。
String lineNum=map.get(user.getIdCard());//拿到先前保存的行號
//System.out.println("先前保存的行號value="+value+" lineNum="+lineNum);
if(tmap.containsKey(user.getIdCard())){
String str=tmap.get(user.getIdCard());//拿到先前保存的所有行號記錄
tmap.put(user.getIdCard(), str+" ,"+newNum);//更新後,顯示效果:——》行重複:在第 2 ,3 , 5
}else{
tmap.put(user.getIdCard(), "重複:行數位於第 "+lineNum+" ,"+newNum);//最後顯示效果:——》行重複:在第 2 ,3
}
}
map.put(user.getIdCard(), newNum+"");//把i行的第column列的值與行號保存到map中
}
//判斷用戶名是否重複
int newNum1=i+1;
if(map1.containsKey(user.getUsername())){//如果Map集合中包含指定的鍵名,則返回true;否則返回false。
String lineNum1=map1.get(user.getUsername());//拿到先前保存的行號
//System.out.println("先前保存的行號value="+value+" lineNum="+lineNum);
if(tmap1.containsKey(user.getUsername())){
String str=tmap1.get(user.getUsername());//拿到先前保存的所有行號記錄
tmap1.put(user.getUsername(), str+" ,"+newNum1);//更新後,顯示效果:——》行重複:在第 2 ,3 , 5
}else{
tmap1.put(user.getUsername(), "重複:行數位於第 "+lineNum1+" ,"+newNum1);//最後顯示效果:——》行重複:在第 2 ,3
}
}
map1.put(user.getUsername(), newNum1+"");//把i行的第column列的值與行號保存到map中
userVOS.add(user);
count++;
}
if (tmap.size() > 0 || tmap1.size() >0){
String msg1 = "";
if (tmap1.size()>0){
Iterator<Map.Entry<String, String>> it=tmap1.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, String> entry = (Map.Entry<String, String>) it.next();
msg1 += "用戶名:" +entry.getKey() +" "+entry.getValue() +";";
}
}
if (tmap.size()>0){
Iterator<Map.Entry<String, String>> it=tmap.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, String> entry = (Map.Entry<String, String>) 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();
}
}
}