公用导出奥利给 只负责生成数据不负责校验
This commit is contained in:
@@ -9,36 +9,34 @@ public class importExcelDTO {
|
|||||||
@Excel(name = "企标类别" ,orderNum = "0" , width = 20.0 )
|
@Excel(name = "企标类别" ,orderNum = "0" , width = 20.0 )
|
||||||
private String bussSort;
|
private String bussSort;
|
||||||
|
|
||||||
private String bussSortId;
|
|
||||||
|
|
||||||
@Excel(name = "企标名称",orderNum = "1", width = 20.0)
|
@Excel(name = "企标名称",orderNum = "1", width = 20.0)
|
||||||
private String esName;
|
private String esName;
|
||||||
|
|
||||||
private String esNameId;
|
|
||||||
|
|
||||||
@Excel(name = "制修订类型",orderNum = "2", width = 20.0)
|
@Excel(name = "制修订类型",orderNum = "2", width = 20.0)
|
||||||
private String reviseStatus;
|
private String reviseStatus;
|
||||||
|
|
||||||
private String reviseStatusId;
|
|
||||||
|
|
||||||
@Excel(name = "起草责任单位" , orderNum = "3", width = 20.0)
|
|
||||||
private String unitName;
|
private String unitName;
|
||||||
|
|
||||||
|
@Excel(name = "起草责任单位" , orderNum = "3", width = 20.0)
|
||||||
private String unit;
|
private String unit;
|
||||||
|
|
||||||
@Excel(name = "起草人" , orderNum = "4", width = 20.0)
|
|
||||||
private String drafterName;
|
private String drafterName;
|
||||||
|
|
||||||
|
@Excel(name = "起草人" , orderNum = "4", width = 20.0)
|
||||||
private String drafter;
|
private String drafter;
|
||||||
|
|
||||||
@Excel(name = "评审责任人" , orderNum = "5", width = 20.0)
|
|
||||||
private String resPersonName;
|
private String resPersonName;
|
||||||
|
|
||||||
|
@Excel(name = "评审责任人" , orderNum = "5", width = 20.0)
|
||||||
private String resPerson;
|
private String resPerson;
|
||||||
|
|
||||||
@Excel(name = "工作组组长" , orderNum = "6", width = 20.0)
|
|
||||||
private String wgLeaderName;
|
private String wgLeaderName;
|
||||||
|
|
||||||
|
@Excel(name = "工作组组长" , orderNum = "6", width = 20.0)
|
||||||
private String wgLeader;
|
private String wgLeader;
|
||||||
|
|
||||||
@Excel(name = "计划初稿完成日期" , orderNum = "7", width = 20.0)
|
@Excel(name = "计划初稿完成日期" , orderNum = "7", width = 20.0)
|
||||||
|
|||||||
+21
-13
@@ -60,25 +60,33 @@ public class IExportServiceImpl implements IExportService {
|
|||||||
tsUsers.forEach(tsUser -> {
|
tsUsers.forEach(tsUser -> {
|
||||||
tsUserMap.put(tsUser.getUserId(),tsUser.getUname());
|
tsUserMap.put(tsUser.getUserId(),tsUser.getUname());
|
||||||
});
|
});
|
||||||
List<importExcelDTO> res= ExcelImportUtilByWk.readExcel(file);
|
List<importExcelDTO> res= ExcelImportUtilByWk.readExcelpublic(importExcelDTO.class,file);
|
||||||
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
|
DateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
int i=1;
|
int i=1;
|
||||||
for (importExcelDTO importExcelDTO: res) {
|
for (importExcelDTO importExcelDTO: res) {
|
||||||
importExcelDTO.setWgLeaderName(tsUserMap.get(importExcelDTO.getWgLeader()));
|
if (null!=importExcelDTO.getWgLeader()) {
|
||||||
if (null!=importExcelDTO.getWgLeader() && null==tsUserMap.get(importExcelDTO.getWgLeader())){
|
importExcelDTO.setWgLeaderName(tsUserMap.get(importExcelDTO.getWgLeader()));
|
||||||
stringBuilder.append("第"+i+"行第7列工作组组长填写不符合格式要求请检查;");
|
if (null == tsUserMap.get(importExcelDTO.getWgLeader())) {
|
||||||
|
stringBuilder.append("第" + i + "行第7列工作组组长填写不符合格式要求请检查;");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
importExcelDTO.setUnitName(tsInstitutionMap.get(importExcelDTO.getUnit()));
|
if (null!=importExcelDTO.getUnit()){
|
||||||
if (null!=importExcelDTO.getUnit() && null==tsInstitutionMap.get(importExcelDTO.getUnit())){
|
importExcelDTO.setUnitName(tsInstitutionMap.get(importExcelDTO.getUnit()));
|
||||||
stringBuilder.append("第"+i+"行第4列起草责任单位填写不符合格式要求请检查;");
|
if (null==tsInstitutionMap.get(importExcelDTO.getUnit())){
|
||||||
|
stringBuilder.append("第"+i+"行第4列起草责任单位填写不符合格式要求请检查;");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
importExcelDTO.setDrafterName(tsUserMap.get(importExcelDTO.getDrafter()));
|
if (null!=importExcelDTO.getDrafter()){
|
||||||
if (null!=importExcelDTO.getDrafter() && null==tsUserMap.get(importExcelDTO.getDrafter())){
|
importExcelDTO.setDrafterName(tsUserMap.get(importExcelDTO.getDrafter()));
|
||||||
stringBuilder.append("第"+i+"行第5列起草人填写不符合格式要求请检查;");
|
if ( null==tsUserMap.get(importExcelDTO.getDrafter())){
|
||||||
|
stringBuilder.append("第"+i+"行第5列起草人填写不符合格式要求请检查;");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
importExcelDTO.setResPersonName(tsUserMap.get(importExcelDTO.getResPerson()));
|
if (null!=importExcelDTO.getResPerson()){
|
||||||
if (null!=importExcelDTO.getResPerson() && null==tsUserMap.get(importExcelDTO.getResPerson())){
|
importExcelDTO.setResPersonName(tsUserMap.get(importExcelDTO.getResPerson()));
|
||||||
stringBuilder.append("第"+i+"行第6列评审责任人填写不符合格式要求请检查;");
|
if ( null==tsUserMap.get(importExcelDTO.getResPerson())){
|
||||||
|
stringBuilder.append("第"+i+"行第6列评审责任人填写不符合格式要求请检查;");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
if (null!=importExcelDTO.getEndTime()) {
|
if (null!=importExcelDTO.getEndTime()) {
|
||||||
|
|||||||
+56
-221
@@ -1,9 +1,11 @@
|
|||||||
package com.adc.da.slrs.processModel.utils;
|
package com.adc.da.slrs.processModel.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import cn.afterturn.easypoi.excel.annotation.Excel;
|
||||||
import com.adc.da.slrs.processModel.entity.importExcelDTO;
|
import com.adc.da.slrs.processModel.entity.importExcelDTO;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
||||||
|
import org.apache.poi.ss.formula.functions.T;
|
||||||
import org.apache.poi.ss.usermodel.Cell;
|
import org.apache.poi.ss.usermodel.Cell;
|
||||||
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
@@ -13,9 +15,12 @@ import org.springframework.web.multipart.MultipartFile;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class ExcelImportUtilByWk {
|
public class ExcelImportUtilByWk {
|
||||||
@@ -30,7 +35,7 @@ public class ExcelImportUtilByWk {
|
|||||||
* @param file 上传的Excel文件
|
* @param file 上传的Excel文件
|
||||||
* @return 读取结果列表,读取失败时返回null
|
* @return 读取结果列表,读取失败时返回null
|
||||||
*/
|
*/
|
||||||
public static List<Class<?>> readExcelPbulic(MultipartFile file) {
|
public static <T> List<T> readExcelpublic(Class<T> tClass,MultipartFile file) {
|
||||||
|
|
||||||
Workbook workbook = null;
|
Workbook workbook = null;
|
||||||
|
|
||||||
@@ -46,9 +51,57 @@ public class ExcelImportUtilByWk {
|
|||||||
// 获取Excel工作簿
|
// 获取Excel工作簿
|
||||||
workbook = getWorkbook(file.getInputStream(), fileType);
|
workbook = getWorkbook(file.getInputStream(), fileType);
|
||||||
|
|
||||||
// 读取excel中的数据
|
|
||||||
//不同需求条件下可以 在此处修改 来获取不同的对象
|
//不同需求条件下可以 在此处修改 来获取不同的对象
|
||||||
List<Class<?>> resultDataList = parseExcelPublic(workbook);
|
List<T> resultDataList = new ArrayList<>();
|
||||||
|
|
||||||
|
// 解析sheet
|
||||||
|
for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
|
||||||
|
Sheet sheet = workbook.getSheetAt(sheetNum);
|
||||||
|
if(!"hidden".equals(sheet.getSheetName())) {
|
||||||
|
// 校验sheet是否合法
|
||||||
|
if (sheet == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取第一行数据
|
||||||
|
int firstRowNum = sheet.getFirstRowNum();
|
||||||
|
Row firstRow = sheet.getRow(firstRowNum);
|
||||||
|
if (null == firstRow) {
|
||||||
|
log.warn("解析Excel失败,在第一行没有读取到任何数据!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 解析每一行的数据,构造数据对象
|
||||||
|
int rowStart = firstRowNum + 1;
|
||||||
|
int rowEnd = sheet.getPhysicalNumberOfRows();
|
||||||
|
|
||||||
|
Map<Integer,Field> map=new HashMap<>();
|
||||||
|
Field[] fields=tClass.getDeclaredFields();
|
||||||
|
for (Field field:fields) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
Excel excel=field.getAnnotation(Excel.class);
|
||||||
|
if (null!=excel) {
|
||||||
|
map.put(Integer.valueOf(excel.orderNum()), field);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
T o = tClass.getConstructor(new Class[]{}).newInstance(new Object[]{});
|
||||||
|
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
|
||||||
|
Row row = sheet.getRow(rowNum);
|
||||||
|
if (null == row) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
for (Integer a:map.keySet()) {
|
||||||
|
Cell cell=null;
|
||||||
|
cell = row.getCell(a);
|
||||||
|
String value = convertCellValueToString(cell);
|
||||||
|
map.get(a).set(o,value);
|
||||||
|
}
|
||||||
|
resultDataList.add(o);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return resultDataList;
|
return resultDataList;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -84,224 +137,6 @@ public class ExcelImportUtilByWk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 解析Excel数据
|
|
||||||
* @param workbook Excel工作簿对象
|
|
||||||
* @return 解析结果
|
|
||||||
*/
|
|
||||||
private static List<Class<?>> parseExcelPublic(Workbook workbook) {
|
|
||||||
List<Class<?>> resultDataList = new ArrayList<>();
|
|
||||||
// 解析sheet
|
|
||||||
for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
|
|
||||||
Sheet sheet = workbook.getSheetAt(sheetNum);
|
|
||||||
if(!"hidden".equals(sheet.getSheetName())) {
|
|
||||||
// 校验sheet是否合法
|
|
||||||
if (sheet == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取第一行数据
|
|
||||||
int firstRowNum = sheet.getFirstRowNum();
|
|
||||||
Row firstRow = sheet.getRow(firstRowNum);
|
|
||||||
if (null == firstRow) {
|
|
||||||
log.warn("解析Excel失败,在第一行没有读取到任何数据!");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 解析每一行的数据,构造数据对象
|
|
||||||
int rowStart = firstRowNum + 1;
|
|
||||||
int rowEnd = sheet.getPhysicalNumberOfRows();
|
|
||||||
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
|
|
||||||
Row row = sheet.getRow(rowNum);
|
|
||||||
if (null == row) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Class<?> resultData = convertRowToDataPublic(row);
|
|
||||||
if (null == resultData) {
|
|
||||||
log.warn("第 " + row.getRowNum() + "行数据不合法,已忽略!");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
resultDataList.add(resultData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return resultDataList;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 提取每一行中需要的数据,构造成为一个结果数据对象
|
|
||||||
*
|
|
||||||
* 当该行中有单元格的数据为空或不合法时,忽略该行的数据
|
|
||||||
*
|
|
||||||
* @param row 行数据
|
|
||||||
* @return 解析后的行数据对象,行数据错误时返回null
|
|
||||||
*/
|
|
||||||
private static Class<?> convertRowToDataPublic(Row row) {
|
|
||||||
Class<?> resultData = null;
|
|
||||||
|
|
||||||
Cell cell;
|
|
||||||
int cellNum = 0;
|
|
||||||
// 获取企标类型
|
|
||||||
cell = row.getCell(cellNum++);
|
|
||||||
String bussSort = convertCellValueToString(cell);
|
|
||||||
// resultData.setBussSort(bussSort);
|
|
||||||
|
|
||||||
return resultData;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 读取Excel文件内容
|
|
||||||
* @param file 上传的Excel文件
|
|
||||||
* @return 读取结果列表,读取失败时返回null
|
|
||||||
*/
|
|
||||||
public static List<importExcelDTO> readExcel(MultipartFile file) {
|
|
||||||
|
|
||||||
Workbook workbook = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
// 获取Excel后缀名
|
|
||||||
String fileName = file.getOriginalFilename();
|
|
||||||
if (fileName == null || fileName.isEmpty() || fileName.lastIndexOf(".") < 0) {
|
|
||||||
log.warn("解析Excel失败,因为获取到的Excel文件名非法!");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
String fileType = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
|
|
||||||
|
|
||||||
// 获取Excel工作簿
|
|
||||||
workbook = getWorkbook(file.getInputStream(), fileType);
|
|
||||||
|
|
||||||
// 读取excel中的数据
|
|
||||||
//不同需求条件下可以 在此处修改 来获取不同的对象
|
|
||||||
List<importExcelDTO> resultDataList = parseExcel(workbook);
|
|
||||||
|
|
||||||
return resultDataList;
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn("解析Excel失败,文件名:" + file.getOriginalFilename() + " 错误信息:" + e.getMessage());
|
|
||||||
return null;
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
if (null != workbook) {
|
|
||||||
workbook.close();
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn("关闭数据流出错!错误信息:" + e.getMessage());
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 解析Excel数据
|
|
||||||
* @param workbook Excel工作簿对象
|
|
||||||
* @return 解析结果
|
|
||||||
*/
|
|
||||||
private static List<importExcelDTO> parseExcel(Workbook workbook) {
|
|
||||||
List<importExcelDTO> resultDataList = new ArrayList<>();
|
|
||||||
// 解析sheet
|
|
||||||
for (int sheetNum = 0; sheetNum < workbook.getNumberOfSheets(); sheetNum++) {
|
|
||||||
Sheet sheet = workbook.getSheetAt(sheetNum);
|
|
||||||
if(!"hidden".equals(sheet.getSheetName())) {
|
|
||||||
// 校验sheet是否合法
|
|
||||||
if (sheet == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取第一行数据
|
|
||||||
int firstRowNum = sheet.getFirstRowNum();
|
|
||||||
Row firstRow = sheet.getRow(firstRowNum);
|
|
||||||
if (null == firstRow) {
|
|
||||||
log.warn("解析Excel失败,在第一行没有读取到任何数据!");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 解析每一行的数据,构造数据对象
|
|
||||||
int rowStart = firstRowNum + 1;
|
|
||||||
int rowEnd = sheet.getPhysicalNumberOfRows();
|
|
||||||
for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) {
|
|
||||||
Row row = sheet.getRow(rowNum);
|
|
||||||
|
|
||||||
if (null == row) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
importExcelDTO resultData = convertRowToData(row);
|
|
||||||
if (null == resultData) {
|
|
||||||
log.warn("第 " + row.getRowNum() + "行数据不合法,已忽略!");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
resultDataList.add(resultData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return resultDataList;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 提取每一行中需要的数据,构造成为一个结果数据对象
|
|
||||||
*
|
|
||||||
* 当该行中有单元格的数据为空或不合法时,忽略该行的数据
|
|
||||||
*
|
|
||||||
* @param row 行数据
|
|
||||||
* @return 解析后的行数据对象,行数据错误时返回null
|
|
||||||
*/
|
|
||||||
private static importExcelDTO convertRowToData(Row row) {
|
|
||||||
importExcelDTO resultData = new importExcelDTO();
|
|
||||||
|
|
||||||
Cell cell;
|
|
||||||
int cellNum = 0;
|
|
||||||
// 获取企标类型
|
|
||||||
cell = row.getCell(cellNum++);
|
|
||||||
String bussSort = convertCellValueToString(cell);
|
|
||||||
resultData.setBussSort(bussSort);
|
|
||||||
// 获取企标名称
|
|
||||||
cell = row.getCell(cellNum++);
|
|
||||||
String esName = convertCellValueToString(cell);
|
|
||||||
resultData.setEsName(esName);
|
|
||||||
//获取制修订类型
|
|
||||||
cell = row.getCell(cellNum++);
|
|
||||||
String reviseStatus = convertCellValueToString(cell);
|
|
||||||
resultData.setReviseStatus(reviseStatus);
|
|
||||||
//获取起草责任单位
|
|
||||||
cell = row.getCell(cellNum++);
|
|
||||||
String unitName = convertCellValueToString(cell);
|
|
||||||
resultData.setUnit(unitName);
|
|
||||||
//获取起草人
|
|
||||||
cell = row.getCell(cellNum++);
|
|
||||||
String drafterName = convertCellValueToString(cell);
|
|
||||||
resultData.setDrafter(drafterName);
|
|
||||||
//获取评审责任人
|
|
||||||
cell = row.getCell(cellNum++);
|
|
||||||
String resPersonName = convertCellValueToString(cell);
|
|
||||||
resultData.setResPerson(resPersonName);
|
|
||||||
//获取工作组组长
|
|
||||||
cell = row.getCell(cellNum++);
|
|
||||||
String wgLeaderName = convertCellValueToString(cell);
|
|
||||||
resultData.setWgLeader(wgLeaderName);
|
|
||||||
//获取计划初稿完成日期
|
|
||||||
cell = row.getCell(cellNum++);
|
|
||||||
String draftTime = convertCellValueToString(cell);
|
|
||||||
resultData.setDraftTime(draftTime);
|
|
||||||
//获取计划标准发布时间
|
|
||||||
cell = row.getCell(cellNum++);
|
|
||||||
String releaseTime = convertCellValueToString(cell);
|
|
||||||
resultData.setReleaseTime(releaseTime);
|
|
||||||
//获取立项完成时间
|
|
||||||
cell = row.getCell(cellNum++);
|
|
||||||
String endTime = convertCellValueToString(cell);
|
|
||||||
resultData.setEndTime(endTime);
|
|
||||||
|
|
||||||
return resultData;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 将单元格内容转换为字符串
|
* 将单元格内容转换为字符串
|
||||||
* @param cell
|
* @param cell
|
||||||
|
|||||||
Reference in New Issue
Block a user