美文网首页
POI解析读取Excel及生成Excel

POI解析读取Excel及生成Excel

作者: 码而优则仕 | 来源:发表于2020-05-14 21:31 被阅读0次

import cn.com.servyou.xqy.framework.utils.CollectionUtils;
import lombok.Data;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.PatternMatchUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.util.*;
import java.util.stream.Collectors;

public class FileParseNewUtils {

public static void main(String args[]) {

    List<FileInfo> rowFile = parseFile("/Users/wsq/Desktop/11111.xlsx");
    List<FileInfo> fullFile = parseFile("/Users/wsq/Desktop/rowrow.xlsx");
    //将名称和身份证连接后作为唯一建进行分组
    Map<String, Set<FileInfo>> handlerRowFile = rowFile.stream().collect(Collectors.groupingBy(file -> file.getSfzh().trim() + file.getName(), Collectors.toSet()));
    Map<String, Set<FileInfo>> handlerFullFile = fullFile.stream().collect(Collectors.groupingBy(file -> file.getSfzh().trim() + file.getName(), Collectors.toSet()));
    List<FinalFileInfo> finalList = new ArrayList<>();
    List<String> fullTaxNoAndNameList = new ArrayList<>(handlerFullFile.keySet());
    for (Map.Entry<String, Set<FileInfo>> entry : handlerRowFile.entrySet()) {
        List<FileInfo> likeTaxAndNameList = new ArrayList<>(entry.getValue());
        if (likeTaxAndNameList.size() > 1) {
            System.out.println("用户名称+模糊身份证未唯一锁定用户,存在重复现象 " + "身份证号:" + entry.getKey() + "   名称:" + entry.getValue());
            continue;
        }
        FileInfo likeTaxAndName = likeTaxAndNameList.get(0);
        //税号模糊处理后,根据税号分组人员信息,同一个模糊税号对应的人员集合
        FileInfo singlePersons = new ArrayList<>(entry.getValue()).get(0);
        List<String> fullTaxNoAndName = getMatchFullTaxNoAndName(entry.getKey(), fullTaxNoAndNameList);

        if (CollectionUtils.isEmpty(fullTaxNoAndName)) {
            System.out.println("姓名加身份证号精确匹配未定位到---" + entry.getKey());
            FinalFileInfo finalFileInfo = new FinalFileInfo();
            finalFileInfo.setName(likeTaxAndName.name);
            finalFileInfo.setSfzh(likeTaxAndName.getSfzh());
            finalFileInfo.setFullName("未找到该人准确信息");
            finalFileInfo.setFinalNo("未找到该人准确信息");
            finalList.add(finalFileInfo);
            continue;
        }
        Set<FileInfo> singleInfoSet = new HashSet<>();
        for (String fullTaxNoAndNames : fullTaxNoAndName) {
            singleInfoSet.addAll(handlerFullFile.get(fullTaxNoAndNames));
        }
        if (CollectionUtils.isEmpty(singleInfoSet)) {
            System.out.println("姓名加身份证号精确匹配未定位到---" + entry.getKey());
            continue;
        }
        FinalFileInfo finalFileInfo = new FinalFileInfo();
        finalFileInfo.setName(likeTaxAndName.name);
        finalFileInfo.setSfzh(likeTaxAndName.getSfzh());
        finalFileInfo.setFullName(singleInfoSet.stream().map(FileInfo::getName).collect(Collectors.joining(",")));
        finalFileInfo.setFinalNo(singleInfoSet.stream().map(FileInfo::getSfzh).collect(Collectors.joining(",")));
        finalList.add(finalFileInfo);
    }
    XSSFWorkbook file = new XSSFWorkbook();
    // /Users/wsq/Desktop/最后结果数据.xlsx
    XSSFSheet sheet = file.createSheet("用户信息");
    int rowIndex = 0;
    for (FinalFileInfo fileInfo : finalList) {
        XSSFRow sheetRow = sheet.createRow(rowIndex);
        sheetRow.createCell(0).setCellValue(fileInfo.getName());
        sheetRow.createCell(1).setCellValue(fileInfo.getSfzh());
        sheetRow.createCell(2).setCellValue(fileInfo.getFullName());
        sheetRow.createCell(3).setCellValue(fileInfo.getFinalNo());
        rowIndex++;
    }
    try {

        file.write(new FileOutputStream(new File("/Users/wsq/Desktop/宣仁.xlsx")));
        file.close();
        System.out.println("正常处理结束!!!!!!!!");
    } catch (Exception e) {
        System.out.println(e);
    }
}

private static List<String> getMatchFullTaxNoAndName(String likeTaxNoAndName, List<String> fullTaxNoAndNameList) {
    Set<String> matchedFullTaxAndName = fullTaxNoAndNameList.stream().filter(fullTaxNoAndName -> PatternMatchUtils.simpleMatch(likeTaxNoAndName, fullTaxNoAndName)).collect(Collectors.toSet());
    if (CollectionUtils.isEmpty(matchedFullTaxAndName)) {
        return null;
    }
    if (matchedFullTaxAndName.size() > 1) {
        System.out.println("匹配上多个确定人信息,请手动核实,--" + likeTaxNoAndName);
        System.out.println("匹配上多个确定人信息,请手动核实,--" + matchedFullTaxAndName);
    }
    return new ArrayList<>(matchedFullTaxAndName);
}


private static List<FileInfo> parseFile(String filePath) {
    XSSFWorkbook rowFile;
    List<FileInfo> rowFileList = new ArrayList<>();
    try {
        rowFile = new XSSFWorkbook(new File(filePath));
        XSSFSheet rowSheet = rowFile.getSheetAt(0);
        int totalRow = rowSheet.getLastRowNum();
        for (int i = 0; i < totalRow; i++) {
            XSSFRow row = rowSheet.getRow(i);
            FileInfo info = new FileInfo();
            info.setName(String.valueOf(row.getCell(0).getStringCellValue()));
            info.setSfzh(String.valueOf(row.getCell(1).getStringCellValue()));
            rowFileList.add(info);
        }
    } catch (Exception e) {
        System.out.println("解析文件异常!!!!");
    }
    return rowFileList;
}

@Data
private static class FileInfo {
    private String name;

    private String fullName;


    private String sfzh;
}

@Data
private static class FinalFileInfo {
    private String name;

    private String fullName;

    private String multyFullName;

    private String sfzh;

    private String finalNo;

    private String multyFinalNo;
}

}

相关文章

网友评论

      本文标题:POI解析读取Excel及生成Excel

      本文链接:https://www.haomeiwen.com/subject/zptfohtx.html