excl标准写入并导出错误excl表

This commit is contained in:
zhaokaiyao@91isoft.com
2021-09-07 08:45:35 +08:00
parent 0c1a57a345
commit 8f91325053
6 changed files with 258 additions and 74 deletions
@@ -0,0 +1,80 @@
package com.adc.da.slrs.ImportExcelDatas.comment;
import com.adc.da.slrs.ImportExcelDatas.entity.ImportDto;
import com.adc.da.utils.util.BussStandExportUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Field;
import java.util.List;
@Component
public class ExclErrorOut {
private static final Logger logger = LoggerFactory.getLogger(BussStandExportUtil.class);
public static Workbook exportDatas (List<ImportDto> datas,String header) {
Workbook workbook = new XSSFWorkbook();
try {
//创建工作表对象
Sheet sheet = workbook.createSheet();
// 创建头部
createHeader(workbook,sheet,header);
// 创建数据
createDatas(workbook,sheet,datas);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return workbook;
}
public static void createHeader(Workbook workbook, Sheet sheet, String header){
CellStyle cellStyle = workbook.createCellStyle();//初始化单元格格式对象
cellStyle.setAlignment(HorizontalAlignment.CENTER);
Row rowHeader = sheet.createRow(0);//开始创建标题行
if (StringUtils.isNotBlank(header)) {
String[] headerArr = header.split(",");
for (int i=0;i < headerArr.length; i++) {
rowHeader.createCell(i).setCellValue(headerArr[i]);
// rowHeader.createCell(i).setCellStyle(cellStyle);
}
}
}
public static void createDatas(Workbook workbook,Sheet sheet,List<ImportDto> datas) throws Exception{
CellStyle cellStyle = workbook.createCellStyle();//初始化单元格格式对象
cellStyle.setAlignment(HorizontalAlignment.CENTER);
if (datas != null && !datas.isEmpty()) {
for (int i=0;i < datas.size(); i++) {
ImportDto importDto = datas.get(i);
Row row = sheet.createRow(i+1);
int sheetNum = 0;
Class cls = importDto.getClass();
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if (field.get(importDto)!=null){
String value = field.get(importDto).toString();
row.createCell(sheetNum).setCellValue(value);
}else {
row.createCell(sheetNum).setCellValue("");
}
sheetNum++;
}
}
}
}
}
@@ -1,15 +1,23 @@
package com.adc.da.slrs.ImportExcelDatas.controller;
import com.adc.da.base.web.BaseController;
import com.adc.da.common.ReadExcel;
import com.adc.da.http.ResponseMessage;
import com.adc.da.slrs.ImportExcelDatas.comment.ExclErrorOut;
import com.adc.da.slrs.ImportExcelDatas.entity.ImportDto;
import com.adc.da.slrs.ImportExcelDatas.service.ImportExcelService;
import com.adc.da.slrs.ImportExcelDatas.service.impl.ImportExcelServiceImpl;
import com.adc.da.slrs.sarBussionessStand.entity.SarBussionessStand;
import com.adc.da.slrs.sarPersonalCenter.entity.SarUserStar;
import com.adc.da.slrs.sarPersonalCenter.entity.SarUserStarEO;
import com.adc.da.util.exception.AdcDaBaseException;
import com.adc.da.util.utils.IOUtils;
import com.adc.da.util.utils.StringUtils;
import com.adc.da.utils.util.BussStandExportUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
@@ -17,6 +25,10 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
@@ -28,15 +40,44 @@ public class ImportExcelController extends BaseController<ImportDto> {
@Autowired
private ImportExcelService importExcelService;
@Autowired
private ExclErrorOut exclErrorOut;
@ApiOperation("批量删除用户收藏")
@PostMapping("/import")
public ResponseMessage<Boolean> deleteList(MultipartFile file,MultipartFile file2) {
public void deleteList(MultipartFile file, MultipartFile file2, HttpServletResponse response, HttpServletRequest request) throws IOException {
Map<String, List<ImportDto>> mapMap=importExcelService.getExcelData(file,file2);
importExcelService.storageExclData(mapMap);
ResponseMessage responseMessage=new ResponseMessage();
responseMessage.setMessage(mapMap.toString());
responseMessage.isOk();
return responseMessage;
List<ImportDto> errorList = importExcelService.storageExclData(mapMap);
// ResponseMessage responseMessage=new ResponseMessage();
// responseMessage.setMessage(mapMap.toString());
// responseMessage.isOk();
OutputStream os = null;
Workbook workbook = null;
try {
String exportName="错误表格";
response.setHeader("Content-Disposition",
"attachment; filename=" + ReadExcel.encodeFileName(exportName+".xlsx",request));
response.setContentType("application/force-download");
//导出数据
String headStr="标准号,标准名称,英文名称,发布时间,实施时间,标准状态,代替标准号,附件路径,错误原因";
workbook = exclErrorOut.exportDatas(errorList,headStr);
os = response.getOutputStream();
workbook.write(os);
os.flush();
} catch (IOException e) {
throw new AdcDaBaseException("下载文件失败,请重试");
} finally {
IOUtils.closeQuietly(os);
if (workbook != null) {
workbook.close();
}
}
// return responseMessage;
}
}
@@ -22,4 +22,7 @@ public class ImportDto extends BaseEntity {
private String replaceId;
// 附件路径
private String path;
// 错误原因
private String errorStr;
}
@@ -12,6 +12,6 @@ public interface ImportExcelService extends IService<ImportDto> {
Map<String, List<ImportDto>> getExcelData(MultipartFile file,MultipartFile file2);
public int storageExclData(Map<String, List<ImportDto>> importListMap);
public List<ImportDto> storageExclData(Map<String, List<ImportDto>> importListMap);
}
@@ -3,7 +3,6 @@ package com.adc.da.slrs.ImportExcelDatas.service.impl;
import com.adc.da.att.service.IAttFileEOService;
import com.adc.da.att.vo.AttFileVo;
import com.adc.da.mq.CreateStandMQService;
import com.adc.da.scheduled.dao.SarStandardInfoDao;
import com.adc.da.slrs.ImportExcelDatas.dao.ImportExcelDao;
import com.adc.da.slrs.ImportExcelDatas.entity.ImportDto;
import com.adc.da.slrs.ImportExcelDatas.service.ImportExcelService;
@@ -18,13 +17,10 @@ import com.adc.da.slrs.sarStandAttrInfo.entity.SarStandAttrInfo;
import com.adc.da.slrs.sarStandAttrInfo.service.impl.SarStandAttrInfoServiceImpl;
import com.adc.da.slrs.sarStandardsInfo.dao.SarStandardsInfoDao;
import com.adc.da.slrs.sarStandardsInfo.entity.SarStandardsInfo;
import com.adc.da.slrs.sarStandardsInfo.service.ISarStandardsInfoService;
import com.adc.da.slrs.sarStandardsInfo.service.impl.SarStandardsInfoServiceImpl;
import com.adc.da.slrs.standardSplit.entity.SarStandardsInfoEO;
import com.adc.da.util.UUIDUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tdunning.math.stats.Sort;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@@ -32,13 +28,12 @@ import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
@@ -284,10 +279,14 @@ public class ImportExcelServiceImpl extends ServiceImpl<ImportExcelDao, ImportDt
@Value("file.importPath")
private String importPath;
private List<ImportDto> error=new ArrayList<>();
@Override
public int storageExclData(Map<String, List<ImportDto>> importListMap) {
public List<ImportDto> storageExclData(Map<String, List<ImportDto>> importListMap) {
error = importListMap.get("error");
/**
* 海外标准
@@ -335,13 +334,16 @@ public class ImportExcelServiceImpl extends ServiceImpl<ImportExcelDao, ImportDt
value+=","+"\'"+sarStandAttrInfo.getSsrq()+"\'";
}
sarStandAttrInfoDao.insertStandAttr(field,value);
// sarStandAttrInfoDao.insert(sarStandAttrInfo);
// sarStandAttrInfoService.save(sarStandAttrInfo);
}
}else{
item.setErrorStr("标准号无法解析");
error.add(item);
importListMap.replace("error",error);
}
@@ -352,7 +354,6 @@ public class ImportExcelServiceImpl extends ServiceImpl<ImportExcelDao, ImportDt
/**
* 国内标准
*/
importListMap.get("GNBZ").forEach(item->{
String standID = UUIDUtils.randomUUID20();
@@ -399,75 +400,79 @@ public class ImportExcelServiceImpl extends ServiceImpl<ImportExcelDao, ImportDt
value+=","+"\'"+sarStandAttrInfo.getSsrq()+"\'";
}
sarStandAttrInfoDao.insertStandAttr(field,value);
// sarStandAttrInfoDao.insert(sarStandAttrInfo);
// sarStandAttrInfoService.save(sarStandAttrInfo);
}
}else {
importListMap.get("error").add(item);
item.setErrorStr("标准号无法解析");
error.add(item);
importListMap.replace("error",error);
}
});
/**
* 企业标准
*/
importListMap.get("QYBZ").forEach(item->{
String standId=UUIDUtils.randomUUID20();
SarBussionessStand sarBussionessStand = parseImportDtoToSarBussionessStand(item, standId);
SarBussStandAttrInfo sarBussStandAttrInfo = parseImportDtoToSarBussStandAttrInfo(item, standId);
if (sarBussionessStand!=null) {
SarBussStandAttrInfo sarBussStandAttrInfo = parseImportDtoToSarBussStandAttrInfo(item, standId);
try {
QueryWrapper<SarBussionessStand> saveWrapper = new QueryWrapper<>();
saveWrapper.eq("STAND_CODE",item.getStandId());
SarBussionessStand one = sarBussionessStandService.getOne(saveWrapper);
if (one!=null){
sarBussionessStandService.update(sarBussionessStand,saveWrapper);
QueryWrapper<SarBussStandAttrInfo> bussAttrWrapper = new QueryWrapper<>();
bussAttrWrapper.eq("STAND_ID",one.getId());
sarBussStandAttrInfoService.update(sarBussStandAttrInfo,bussAttrWrapper);
sarBussStandAttrInfoDao.updateStandInfo(one.getId(),"GLWJBUSS",sarBussStandAttrInfo.getGlwj());
try {
QueryWrapper<SarBussionessStand> saveWrapper = new QueryWrapper<>();
saveWrapper.eq("STAND_CODE", item.getStandId());
SarBussionessStand one = sarBussionessStandService.getOne(saveWrapper);
if (one != null) {
sarBussionessStand.setId(one.getId());
sarBussionessStandService.update(sarBussionessStand, saveWrapper);
}else {
sarBussionessStandService.save(sarBussionessStand);
sarBussStandAttrInfoService.save(sarBussStandAttrInfo);
QueryWrapper<SarBussStandAttrInfo> bussAttrWrapper = new QueryWrapper<>();
bussAttrWrapper.eq("STAND_ID", one.getId());
sarBussStandAttrInfo.setStandId(one.getId());
sarBussStandAttrInfoService.update(sarBussStandAttrInfo, bussAttrWrapper);
sarBussStandAttrInfoDao.updateStandInfo(one.getId(), "GLWJBUSS", sarBussStandAttrInfo.getGlwj());
} else {
sarBussionessStandService.save(sarBussionessStand);
sarBussStandAttrInfoService.save(sarBussStandAttrInfo);
sarBussStandAttrInfoDao.updateStandInfo(sarBussStandAttrInfo.getStandId(), "GLWJBUSS", sarBussStandAttrInfo.getGlwj());
}
} catch (Exception e) {
item.setErrorStr("数据格式错误");
error.add(item);
importListMap.replace("error",error);
System.out.println("issueTime====Hello======" + sarBussionessStand.getIssueTime() + "===hello=====" + item);
e.printStackTrace();
sarBussStandAttrInfoDao.updateStandInfo(sarBussStandAttrInfo.getStandId(),"GLWJBUSS",sarBussStandAttrInfo.getGlwj());
}
}catch (Exception e){
importListMap.get("error").add(item);
System.out.println("issueTime====Hello======"+sarBussionessStand.getIssueTime()+"===hello====="+item);
e.printStackTrace();
}else {
item.setErrorStr("标准号无法解析");
error.add(item);
importListMap.replace("error",error);
}
});
//
// String head="standId,standName,standNameEN,publishTime,implementedTime" +
// ",standStatus,replaceId,path";
// String headStr="标准号,标准名称,英文名称,发布时间,实施时间,标准状态,代替标准号,附件路径";
importListMap.get("error");
String head="standId,standName,standNameEN,publishTime,implementedTime" +
",standStatus,replaceId,path";
String headStr="标准号,标准名称,英文名称,发布时间,实施时间,标准状态,代替标准号,附件路径";
List<ImportDto> error = importListMap.get("error");
System.out.println(error);
return 0;
return error;
}
@@ -488,6 +493,7 @@ public class ImportExcelServiceImpl extends ServiceImpl<ImportExcelDao, ImportDt
year=numberAndYear[1];
}catch (Exception e){
System.out.println("id为"+importDto.getStandId()+"格式无法解析");
return null;
}
@@ -533,33 +539,49 @@ public class ImportExcelServiceImpl extends ServiceImpl<ImportExcelDao, ImportDt
String path[] = importDto.getPath().split("/");
String realPath="";
for (int i = 4; i < path.length; i++) {
realPath=realPath+"\\"+path[i];
realPath=realPath+"/"+path[i];
}
File file = new File("C:\\Users\\22501\\Desktop\\foton\\2021\\" + realPath);
if (file.exists()){
AttFileVo fileInfo = attFileEOService.saveFileInfo(file);
HashMap<String, String> fileMap = new HashMap<>();
try {
// File file = new File("C:\\Users\\22501\\Desktop\\foton\\2021\\" + realPath);
// File file = new File(importPath + realPath);
File file = new File("/home/file/2021/" + realPath);
if (file.exists()){
AttFileVo fileInfo = attFileEOService.saveFileInfo(file);
HashMap<String, String> fileMap = new HashMap<>();
/**
* * @Param: [standId, fileMap] fileMap<field,fileIds>
*/
fileMap.put("GLWJ",fileInfo.getAttId());
try {
sarStandardsInfoService.createStandFile(standId,fileMap);
} catch (Exception e) {
e.printStackTrace();
System.out.println("创建标准文件异常");
/**
* * @Param: [standId, fileMap] fileMap<field,fileIds>
*/
fileMap.put("GLWJ",fileInfo.getAttId());
try {
sarStandardsInfoService.createStandFile(standId,fileMap);
} catch (Exception e) {
importDto.setErrorStr("创建标准文件异常");
error.add(importDto);
e.printStackTrace();
System.out.println("创建标准文件异常");
}
sarStandAttrInfo.setGlwj(fileInfo.getAttId());
}else {
importDto.setErrorStr("文件不存在");
error.add(importDto);
}
sarStandAttrInfo.setGlwj(fileInfo.getAttId());
}catch (Exception e){
importDto.setErrorStr("文件打开失败");
error.add(importDto);
e.printStackTrace();
}
}
@@ -574,8 +596,30 @@ public class ImportExcelServiceImpl extends ServiceImpl<ImportExcelDao, ImportDt
public SarBussionessStand parseImportDtoToSarBussionessStand(ImportDto importDto,String standId){
if(importDto.getStandId()==null){
return null;
}
String sort="";
String number="";
String year="";
try {
String[] sortAndNumberAndYear = importDto.getStandId().split("\\s+");
sort = sortAndNumberAndYear[0];
String[] numberAndYear = sortAndNumberAndYear[1].split("\\-");
number=numberAndYear[0];
year=numberAndYear[1];
}catch (Exception e){
System.out.println("id为"+importDto.getStandId()+"格式无法解析");
return null;
}
SarBussionessStand sarBussionessStand = new SarBussionessStand();
sarBussionessStand.setId(standId);
sarBussionessStand.setStandSort(sort);
sarBussionessStand.setStandYear(year);
sarBussionessStand.setStandCode(importDto.getStandId());
sarBussionessStand.setStandName(importDto.getStandName());
sarBussionessStand.setStandEnName(importDto.getStandNameEN());
@@ -603,13 +647,17 @@ public class ImportExcelServiceImpl extends ServiceImpl<ImportExcelDao, ImportDt
String path[] = importDto.getPath().split("/");
String realPath="";
for (int i = 4; i < path.length; i++) {
realPath=realPath+"\\"+path[i];
realPath=realPath+"/"+path[i];
}
File file = new File("C:\\Users\\22501\\Desktop\\foton\\2021\\" + realPath);
if (file.exists()){
try {
// File file = new File("C:\\Users\\22501\\Desktop\\foton\\2021\\" + realPath);
// File file = new File(importPath + realPath);
File file = new File("/home/file/2021/" + realPath);
if (file.exists()){
AttFileVo fileInfo = attFileEOService.saveFileInfo(file);
HashMap<String, String> fileMap = new HashMap<>();
@@ -622,14 +670,25 @@ public class ImportExcelServiceImpl extends ServiceImpl<ImportExcelDao, ImportDt
try {
sarBussionessStandService.createStandFile(standId,fileMap);
} catch (Exception e) {
importDto.setErrorStr("创建标准文件异常");
error.add(importDto);
e.printStackTrace();
System.out.println("创建标准文件异常");
}
sarBussStandAttrInfo.setGlwj(fileInfo.getAttId());
}else {
importDto.setErrorStr("文件不存在");
error.add(importDto);
}
}catch (Exception e){
importDto.setErrorStr("路径错误!");
error.add(importDto);
e.printStackTrace();
}
}
@@ -53,6 +53,7 @@ public class SarBussStandAttrInfo extends BaseEntity {
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date modifyTime;
@TableField(exist = false)
private String glwj;
//
// @TableField("SVPPS")