|
|
|
|
@ -6,6 +6,7 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
import org.apache.poi.hssf.usermodel.*;
|
|
|
|
|
import org.apache.poi.hssf.util.HSSFColor;
|
|
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
|
import org.springframework.web.context.ContextLoader;
|
|
|
|
|
import org.springframework.web.context.WebApplicationContext;
|
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
@ -14,6 +15,8 @@ import java.lang.reflect.Constructor;
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.time.LocalDate;
|
|
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -254,6 +257,163 @@ public class ImportExcelUtil {
|
|
|
|
|
excelEntity.setWorkBookKey(workBookKey);
|
|
|
|
|
return excelEntity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static ImportExcelEntity fileImportByBloodPurification(MultipartFile multipartFile, String[] fieldNames, String[] judgeMethods, String jedgeMethod) {
|
|
|
|
|
Workbook workbook = null;
|
|
|
|
|
int successCount = 0;
|
|
|
|
|
int wrongCount = 0;
|
|
|
|
|
String workBookKey = "";
|
|
|
|
|
//正确行数下角标
|
|
|
|
|
List<Integer> wrongRowIndex = new ArrayList<>();
|
|
|
|
|
try {
|
|
|
|
|
/*把文件流copy读取到文件中*/
|
|
|
|
|
workbook = WorkbookFactory.create(multipartFile.getInputStream());
|
|
|
|
|
/*遍历sheet页*/
|
|
|
|
|
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
|
|
|
|
|
Sheet sheet = workbook.getSheetAt(i);
|
|
|
|
|
if (sheet == null) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
//获取表头行
|
|
|
|
|
//Row head = sheet.getRow(0);
|
|
|
|
|
//处理多级表头
|
|
|
|
|
//Map<String, Object> headerMap = handleRow(sheet);
|
|
|
|
|
String[] head = {"序号","检查日期","患者姓名","透析日期","记录填写不完整","医嘱执行签名不规范","医护未签名","未做宣教","宣教填写不完整体","护患未签名","医生未签名","患者家属关系缺项","通路名称不规范","其他","责任护士","整改日期","整改签名","备注"};
|
|
|
|
|
|
|
|
|
|
List<Object> list = new ArrayList<>();
|
|
|
|
|
for (int j = 4; j < sheet.getPhysicalNumberOfRows(); j++) {
|
|
|
|
|
Row row = sheet.getRow(j);
|
|
|
|
|
boolean flag = isRowEmpty(row);
|
|
|
|
|
if (!flag) {
|
|
|
|
|
if (row.getPhysicalNumberOfCells() != 0) {
|
|
|
|
|
StringBuilder wrongStr = new StringBuilder();
|
|
|
|
|
for (int k = 0; k < fieldNames.length; k++) {
|
|
|
|
|
//对象赋值后返回错误对象
|
|
|
|
|
wrongStr.append(getValueByField(object, fieldNames[k], getCellValue(row.getCell(k)), head[k], null));
|
|
|
|
|
}
|
|
|
|
|
//有错误信息在行尾创建cell,并标红提示文字
|
|
|
|
|
if (StringUtils.isBlank(wrongStr)) {
|
|
|
|
|
Constructor constructor = ObjectClass.getDeclaredConstructor();
|
|
|
|
|
Object obj = (Object) constructor.newInstance();
|
|
|
|
|
//判断是否有自定义条件
|
|
|
|
|
if (StringUtils.isNotBlank(jedgeMethod)) {
|
|
|
|
|
//判断是否有报错信息,有报错信息添加到报错信息
|
|
|
|
|
String valueByField = judgeMethod(jedgeMethod);
|
|
|
|
|
if (StringUtils.isNotBlank(valueByField)) {
|
|
|
|
|
wrongStr.append(valueByField);
|
|
|
|
|
} else {
|
|
|
|
|
successCount++;
|
|
|
|
|
wrongRowIndex.add(j);
|
|
|
|
|
//复制
|
|
|
|
|
copy(object, obj);
|
|
|
|
|
list.add(obj);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
successCount++;
|
|
|
|
|
wrongRowIndex.add(j);
|
|
|
|
|
//复制
|
|
|
|
|
copy(object, obj);
|
|
|
|
|
list.add(obj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (StringUtils.isNotBlank(wrongStr)) {
|
|
|
|
|
//设置错误信息字体为红色加粗
|
|
|
|
|
Font font = workbook.createFont();
|
|
|
|
|
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
|
|
|
|
|
font.setColor(HSSFColor.RED.index);
|
|
|
|
|
CellStyle cellStyle = workbook.createCellStyle();
|
|
|
|
|
cellStyle.setFont(font);
|
|
|
|
|
Cell cell = row.createCell(fieldNames.length);
|
|
|
|
|
cell.setCellStyle(cellStyle);
|
|
|
|
|
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
|
|
|
|
|
wrongStr = new StringBuilder(wrongStr.substring(0, wrongStr.length() - 1));
|
|
|
|
|
cell.setCellValue(wrongStr.toString());
|
|
|
|
|
wrongCount++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//批量新增成功记录
|
|
|
|
|
SimpleInsert(list);
|
|
|
|
|
//删除正确行
|
|
|
|
|
if (!wrongRowIndex.isEmpty()) {
|
|
|
|
|
//定义删除次数,对应后方下角标都要减去删除次数
|
|
|
|
|
int deleteCount = 0;
|
|
|
|
|
//递归删除
|
|
|
|
|
removeSheet(sheet, deleteCount, wrongRowIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new RuntimeException();
|
|
|
|
|
}
|
|
|
|
|
//错误excel加入缓存
|
|
|
|
|
if (wrongCount != 0) {
|
|
|
|
|
workBookKey = UUIDUtils.getUUID();
|
|
|
|
|
workBookMap.put(workBookKey, workbook);
|
|
|
|
|
}
|
|
|
|
|
ImportExcelEntity excelEntity = new ImportExcelEntity();
|
|
|
|
|
excelEntity.setSuccessCount(successCount);
|
|
|
|
|
excelEntity.setWrongCount(wrongCount);
|
|
|
|
|
excelEntity.setWorkBookKey(workBookKey);
|
|
|
|
|
return excelEntity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Map<String, Object> handleRow(Sheet sheet) {
|
|
|
|
|
// 创建一个Map来存储表头信息
|
|
|
|
|
Map<String, Object> headerMap = new HashMap<>();
|
|
|
|
|
//读取真实行数
|
|
|
|
|
int physicalNumberOfRows = readExcelValueRows(sheet);
|
|
|
|
|
for (int i = 0; i <= physicalNumberOfRows - 1; i++) {
|
|
|
|
|
List<Object> fieldNames = new ArrayList<>();
|
|
|
|
|
// 获取第表头
|
|
|
|
|
Row headerRow = sheet.getRow(i);
|
|
|
|
|
int physicalNumberOfCells = headerRow.getPhysicalNumberOfCells();
|
|
|
|
|
for (int j = 0; j < physicalNumberOfCells; j++) {
|
|
|
|
|
// 获取表头
|
|
|
|
|
Cell cell = headerRow.getCell(j);
|
|
|
|
|
// 获取表头值
|
|
|
|
|
String value = getCellValue(cell);
|
|
|
|
|
if(!ObjectUtils.isEmpty(value)){
|
|
|
|
|
fieldNames.add(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 存储表头信息
|
|
|
|
|
headerMap.put("head" + i, fieldNames);
|
|
|
|
|
}
|
|
|
|
|
return headerMap;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用来得到真实行数
|
|
|
|
|
* @param sheet 需要读取的Excel表格(excel文件的工作簿的名称)
|
|
|
|
|
* @return
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public static int readExcelValueRows(Sheet sheet) {
|
|
|
|
|
int realRow = 0;// 返回的真实行数
|
|
|
|
|
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
|
|
|
|
|
//i从1开始,不判断第一行标题行
|
|
|
|
|
Row row = sheet.getRow(i);
|
|
|
|
|
if (row == null){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
for (Cell cell : row) {
|
|
|
|
|
if (cell == null){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
String value = getCellValue(cell);
|
|
|
|
|
if (value == null || "".equals(value)){
|
|
|
|
|
continue;
|
|
|
|
|
} else{
|
|
|
|
|
realRow++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return realRow;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//批量新增成功数据方法
|
|
|
|
|
private static int SimpleInsert(List<Object> list)throws Exception{
|
|
|
|
|
if(null != list && !list.isEmpty()){
|
|
|
|
|
@ -348,11 +508,15 @@ public class ImportExcelUtil {
|
|
|
|
|
String typeName = filedType.getName();
|
|
|
|
|
if("java.lang.Integer".equals(typeName)){
|
|
|
|
|
f.set(object,Integer.valueOf(value));
|
|
|
|
|
}if("java.lang.Short".equals(typeName)){
|
|
|
|
|
}else if("java.lang.Short".equals(typeName)){
|
|
|
|
|
f.set(object,Short.valueOf(value));
|
|
|
|
|
}else if("java.util.Date".equals(typeName)){
|
|
|
|
|
f.set(object,new SimpleDateFormat("yyyy-MM-dd").parse(value));
|
|
|
|
|
}else{
|
|
|
|
|
f.set(object,value);
|
|
|
|
|
}
|
|
|
|
|
}else{
|
|
|
|
|
f.set(object,value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|