【POI xls Java map】使用POI处理xls 抽取出异常信息 --java1.8Group by ---map迭代 -- 设置单元格高度

时间:2023-03-09 05:52:45
【POI  xls  Java  map】使用POI处理xls  抽取出异常信息  --java1.8Group by    ---map迭代  --  设置单元格高度

【POI  xls  Java  map】使用POI处理xls  抽取出异常信息  --java1.8Group by    ---map迭代  --  设置单元格高度

代码处理逻辑:

【POI  xls  Java  map】使用POI处理xls  抽取出异常信息  --java1.8Group by    ---map迭代  --  设置单元格高度

代码流程:

【POI  xls  Java  map】使用POI处理xls  抽取出异常信息  --java1.8Group by    ---map迭代  --  设置单元格高度

1.首先需要创建一个实体 用来存储 相关信息

 package com.sxd.test.unusualName;

 public class NameEntity {

     private String name;//姓名
private String num;//编号
private String rsNum;//RS号
private String disease;//疾病
private String rsInfo;//rs值 public NameEntity() {
// TODO Auto-generated constructor stub
} public NameEntity(String name, String num, String rsNum, String disease,
String rsInfo) {
super();
this.name = name;
this.num = num;
this.rsNum = rsNum;
this.disease = disease;
this.rsInfo = rsInfo;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getNum() {
return num;
} public void setNum(String num) {
this.num = num;
} public String getRsNum() {
return rsNum;
} public void setRsNum(String rsNum) {
this.rsNum = rsNum;
} public String getDisease() {
return disease;
} public void setDisease(String disease) {
this.disease = disease;
} public String getRsInfo() {
return rsInfo;
} public void setRsInfo(String rsInfo) {
this.rsInfo = rsInfo;
} }

2.具体的处理方法

 package com.sxd.test.unusualName;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors; import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.junit.Test; public class Xls4Name { /**
* 主方法--处理多份xls文件中 不同sheet[疾病]下 姓名/位点一致||订单编号一致,但是位点值不同的信息
* @throws IOException
*/
@Test
public void mainTest() throws IOException{ test1();
test2();
test3(); }
/**
* 处理 肝癌 冠心病 脑梗 位点信息为rs1801133
* @throws IOException
*/
public void test1() throws IOException{
File file = new File("D:/20161110-时代基因大批量检测结果/");
File [] allFile = file.listFiles();
if(allFile.length > 0){
List<NameEntity> listAll = new ArrayList<NameEntity>();
String outPath = null;
for (int i = 0; i < allFile.length; i++) {
String fileName = allFile[i].getName();
String filePath = "D:/20161110-时代基因大批量检测结果/"+fileName;
String [] sheetName = {"肝癌","冠心病","脑梗(缺血性脑卒中)"};
String RsNum = "rs1801133";
outPath = "d:/异常/"+"异常1---"+fileName.substring(0,fileName.lastIndexOf("."))+".xls";
//获取原始数据
listAll = just4ListArr(filePath,RsNum,sheetName);
}
//对原始数据分组
List<NameEntity> list = groupByList(listAll);
//将处理好的数据存入异常文件
if(list.size() > 0){
just4Result(list,outPath);
}
}
}
/**
* 处理 胃癌 食管癌 位点信息 rs2274223
* @throws IOException
*/
public void test2() throws IOException{
File file = new File("D:/20161110-时代基因大批量检测结果/");
File [] allFile = file.listFiles();
if(allFile.length > 0){
List<NameEntity> listAll = new ArrayList<NameEntity>();//listAll放在循环内部 可以单独处理多个文件 放在循环外面可以将多个文件统一处理
String outPath = null;
for (int i = 0; i < allFile.length; i++) {
String fileName = allFile[i].getName();
String filePath = "D:/20161110-时代基因大批量检测结果/"+fileName;
String [] sheetName = {"胃癌","食管癌"};
String RsNum = "rs2274223";
outPath = "d:/异常/"+"异常2---"+fileName.substring(0,fileName.lastIndexOf("."))+".xls";
//获取原始数据
listAll = just4ListArr(filePath,RsNum,sheetName); }
//对原始数据分组
List<NameEntity> list = groupByList(listAll);
//将处理好的数据存入异常文件
if(list.size() > 0){
just4Result(list,outPath);
}
}
}
/**
* 高血压 脑梗 位点信息rs699
* @throws IOException
*/
public void test3() throws IOException{
File file = new File("D:/20161110-时代基因大批量检测结果/");
File [] allFile = file.listFiles();
if(allFile.length > 0){
List<NameEntity> listAll = new ArrayList<NameEntity>();
String outPath = null;
for (int i = 0; i < allFile.length; i++) {
String fileName = allFile[i].getName();
String filePath = "D:/20161110-时代基因大批量检测结果/"+fileName;
String [] sheetName = {"高血压","脑梗(缺血性脑卒中)"};
String RsNum = "rs699";
outPath = "d:/异常/"+"异常3---"+fileName.substring(0,fileName.lastIndexOf("."))+".xls"; //获取原始数据
listAll = just4ListArr(filePath,RsNum,sheetName);
}
//对原始数据分组
List<NameEntity> list = groupByList(listAll);
//将处理好的数据存入异常文件
if(list.size() > 0){
just4Result(list,outPath);
}
}
} /**
* 根据传入的 文件路径以及sheet名称 分别创建sheet,并传入just4List(listAll,sheet)进行处理
* @param filePath
* @param sheetName
* @return
* @throws IOException
*/
public List<NameEntity> just4ListArr(String filePath,String RsNum,String ...sheetName) throws IOException{
FileInputStream in = new FileInputStream(new File(filePath));
HSSFWorkbook work = new HSSFWorkbook(in); List<NameEntity> listAll = new ArrayList<>();
for (String sName : sheetName) {
Sheet sheet = work.getSheet(sName);//根据sheet名称 获取几种疾病的信息
listAll = just4List(listAll,sheet,RsNum);
} work.close();
in.close();
return listAll; } /**
* 对xls中抽取出来的原始数据进行分组处理 按照用户名name称分组/或者按照订单号num分组 java1.8
* @param listAll
* @return
*/
public List<NameEntity> groupByList(List<NameEntity> listAll){
List<NameEntity> result = new ArrayList<NameEntity>(); // Map<String,List<NameEntity>> map = listAll.stream().collect(Collectors.groupingBy(e->((NameEntity) e).getName()));
Map<String,List<NameEntity>> map = listAll.stream().collect(Collectors.groupingBy(e->((NameEntity) e).getNum()));
//下面对map进行迭代
Set<String> set = map.keySet();
for (String string : set) {
int listSize = map.get(string).size();
if(listSize > 1){
String rs = map.get(string).get(0).getRsInfo();//获取第一个的rs号
for (int i = 1; i < listSize; i++) {//循环判断 若出现rs号不一致 即抽取出同组的多条信息 保存
if(!map.get(string).get(i).getRsInfo().equals(rs)){
result.addAll(map.get(string));
break;
}
}
}
} return result;
} /**
* 根据最终传入的list
* @param list
* @throws IOException
*/
public void just4Result(List<NameEntity> list,String outPath) throws IOException{
//输出文档
FileOutputStream out = new FileOutputStream(new File(outPath));
HSSFWorkbook workOut = new HSSFWorkbook();
//设置sheet名称
Sheet sheet = workOut.createSheet("异常名称");
//设置第2列的宽度为 100*256
sheet.setColumnWidth(1, 50 * 256);
sheet.setColumnWidth(2, 15 * 256);
sheet.setColumnWidth(3, 30 * 256);
//创建首行
Row row1 = sheet.createRow(0);
//首行 行高
row1.setHeight((short)500);
//首行 列名数组
String []rowName = {"姓名","编号(采样号)","RS号","疾病","RS值"};
//设置首行样式
HSSFCellStyle cellStyle = workOut.createCellStyle();
//创建字体
HSSFFont font = workOut.createFont();
//设置加粗
font.setBold(true);
//设置字体颜色
font.setColor(HSSFColor.AQUA.index);
//设置字体大小
font.setFontHeightInPoints((short)14);
cellStyle.setFont(font);
for (int i = 0; i < 5; i++) {
Cell cell = row1.createCell(i);
cell.setCellValue(rowName[i]);
cell.setCellStyle(cellStyle);
} //对应列放入对应数据
for (int i = 0; i < list.size(); i++) {
Row row2 = sheet.createRow(i+1);
NameEntity nameEntity = list.get(i);
for (int j = 0; j < 5; j++) {
Cell cell = row2.createCell(j);
switch (j) {
case 0: cell.setCellValue(nameEntity.getName()); break;
case 1: cell.setCellValue(nameEntity.getNum()); break;
case 2: cell.setCellValue(nameEntity.getRsNum()); break;
case 3: cell.setCellValue(nameEntity.getDisease()); break;
case 4: cell.setCellValue(nameEntity.getRsInfo()); break; default: cell.setCellValue("数据异常");
break;
} }
} workOut.write(out);
out.close();
workOut.close();
} /**
* 从原始xls文件中抽取出来最原始的数据 存放如listAll中
* @param listAll
* @param sheet
* @return
*/
public List<NameEntity> just4List(List<NameEntity> listAll,Sheet sheet,String RsNum){
String diseaseName = sheet.getSheetName(); Row row1 = sheet.getRow(0);
Cell cell = row1.getCell(0);
int maxRowNum = just4MaxRowNum(sheet);//先计算出 最大行数
int maxCellNum = row1.getLastCellNum();//最大列数
row1 = sheet.getRow(2); //固定 rsNum放在第三行 故 获取第三行
int rsNum = 0;
//判断对应的rsNum在第几列 记录列号
cell = row1.getCell(4);
String value = cell.getRichStringCellValue().toString();
if(RsNum.equals(value)){
rsNum = 4;
}else {
rsNum = 5;
} //循环从5开始 因为原始文件中的前5行非数据
for (int i = 5; i < maxRowNum ; i++) {//然后纵向提取 获取所有rs信息
NameEntity nameEntity = new NameEntity();//实例化对象
row1 = sheet.getRow(i);
String rs = row1.getCell(rsNum).getStringCellValue();//获取rs 由于rs号不确定在第几列 故进行判断
String name = row1.getCell(3).getStringCellValue();//获取name name列确定列号为3 即在第四行 也可以自行判断
String num = null;
if(row1.getCell(2).getCellType() == Cell.CELL_TYPE_NUMERIC){
num = String.valueOf(row1.getCell(2).getNumericCellValue());//获取num 编号 条形码
}else{
num = String.valueOf(row1.getCell(2).getStringCellValue());
}
nameEntity.setNum(num);
nameEntity.setName(name);
nameEntity.setRsNum(RsNum);
nameEntity.setDisease(diseaseName);
nameEntity.setRsInfo(rs);
listAll.add(nameEntity);
} return listAll;
} /**
* 获取最大行数 由于人为原因 xls中某个单元格中内容虽然已经删除 但是单元格的对象依旧创建,因此需要自己获取有效行数
* @param sheet
* @return
*/
public int just4MaxRowNum(Sheet sheet){
int maxRowNum = sheet.getLastRowNum();//获取最大行号 但不是有效行号 for (int i = 5; i < maxRowNum; i++) {
Row row = sheet.getRow(i);
Cell cell = row.getCell(3);
if(cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK){//判断cell单元格为null或者单元格类型为blank就表示此单元格没有数据 那这一行的上一行就是有效行数
maxRowNum = i-1;
break;
}
}
return maxRowNum;
}
}

3.在处理完成业务之后   可以调用方法将文件夹中的 使用过的文件删除

 package com.sxd.test.unusualName;

 import java.io.File;

 import org.junit.Test;

 /**
* 一键删除 异常文件以及 源文件
* @author Administrator
*
*/
public class DeleteUnusualFile { @Test
public void deleteFile(){
deleteSourceFile();
deleteUnusualFile();
} /**
*删除源文件
*/
public void deleteSourceFile(){
File file = new File("D:/20161110-时代基因大批量检测结果");
File[] fileArr = file.listFiles();
if(file.isDirectory() && fileArr.length > 0){//若此路径是文件夹 且 下面的子文件不为null
for (int i = 0; i < fileArr.length; i++) {
if(fileArr[i].isFile()){
fileArr[i].delete();
}
}
} }
/**
* 删除异常文件
*/
public void deleteUnusualFile(){
File file = new File("D:/异常");
File[] fileArr = file.listFiles();
if(file.isDirectory() && fileArr.length > 0){//若此路径是文件夹 且 下面的子文件不为null
for (int i = 0; i < fileArr.length; i++) {
if(fileArr[i].isFile()){
fileArr[i].delete();
}
}
}
}
}