From 7d3e8707a5e4cf88d042fd2db7920e72d582662f Mon Sep 17 00:00:00 2001 From: SUNJIABIN Date: Mon, 27 Feb 2023 16:34:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=81=E4=B8=9A=E6=A0=87=E5=87=86=E5=AF=BC?= =?UTF-8?q?=E5=85=A5=E5=8A=9F=E8=83=BD=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SarBussionessStandController.java | 10 +- .../service/ISarBussionessStandService.java | 2 + .../impl/SarBussionessStandServiceImpl.java | 154 +++++++++++++++++- 3 files changed, 157 insertions(+), 9 deletions(-) diff --git a/adc-da-slrs/src/main/java/com/adc/da/slrs/sarBussionessStand/controller/SarBussionessStandController.java b/adc-da-slrs/src/main/java/com/adc/da/slrs/sarBussionessStand/controller/SarBussionessStandController.java index c5577125..dae1cfef 100644 --- a/adc-da-slrs/src/main/java/com/adc/da/slrs/sarBussionessStand/controller/SarBussionessStandController.java +++ b/adc-da-slrs/src/main/java/com/adc/da/slrs/sarBussionessStand/controller/SarBussionessStandController.java @@ -557,7 +557,15 @@ public class SarBussionessStandController extends BaseController importSarBussionessStandFile2(@RequestParam(value = "file",required = false)MultipartFile file) throws Exception{ + sarBussionessStandEOService.importSarBussionessStandFile2(file); + return Result.success(); + } } diff --git a/adc-da-slrs/src/main/java/com/adc/da/slrs/sarBussionessStand/service/ISarBussionessStandService.java b/adc-da-slrs/src/main/java/com/adc/da/slrs/sarBussionessStand/service/ISarBussionessStandService.java index 3d145986..5a6ce13e 100644 --- a/adc-da-slrs/src/main/java/com/adc/da/slrs/sarBussionessStand/service/ISarBussionessStandService.java +++ b/adc-da-slrs/src/main/java/com/adc/da/slrs/sarBussionessStand/service/ISarBussionessStandService.java @@ -58,5 +58,7 @@ public interface ISarBussionessStandService extends IService ResponseMessage importSarBussionessStand(List> dataList, String filepath)throws Exception; + //导入 + ResponseMessage importSarBussionessStandFile2(MultipartFile file) throws Exception; } diff --git a/adc-da-slrs/src/main/java/com/adc/da/slrs/sarBussionessStand/service/impl/SarBussionessStandServiceImpl.java b/adc-da-slrs/src/main/java/com/adc/da/slrs/sarBussionessStand/service/impl/SarBussionessStandServiceImpl.java index 50551181..4c0d58c0 100644 --- a/adc-da-slrs/src/main/java/com/adc/da/slrs/sarBussionessStand/service/impl/SarBussionessStandServiceImpl.java +++ b/adc-da-slrs/src/main/java/com/adc/da/slrs/sarBussionessStand/service/impl/SarBussionessStandServiceImpl.java @@ -3,10 +3,7 @@ package com.adc.da.slrs.sarBussionessStand.service.impl; import cn.hutool.core.bean.BeanUtil; import com.adc.da.att.entity.AttFileEO; import com.adc.da.att.service.IAttFileEOService; -import com.adc.da.common.ConvertMqEO; -import com.adc.da.common.SarTypeEnum; -import com.adc.da.common.SelectionTypeEnum; -import com.adc.da.common.UseModuleEnum; +import com.adc.da.common.*; import com.adc.da.http.ResponseMessage; import com.adc.da.http.Result; import com.adc.da.mq.CreateMQService; @@ -37,6 +34,7 @@ import com.adc.da.slrs.sarBussionessStand.entity.SarBussionessStand; import com.adc.da.slrs.sarBussionessStand.dao.SarBussionessStandDao; import com.adc.da.slrs.sarBussionessStand.entity.SarBussionessStandExport; import com.adc.da.slrs.sarBussionessStand.service.ISarBussionessStandService; +import com.adc.da.slrs.sarLawsInfo.service.ISarLawsInfoService; import com.adc.da.slrs.sarMenuStandard.entity.SarMenuStandardNew; import com.adc.da.slrs.sarMenuStandard.service.ISarMenuStandardNewService; import com.adc.da.slrs.sarResource.entity.TsResource; @@ -63,6 +61,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import net.sf.json.JSONObject; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; @@ -70,10 +69,7 @@ import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.json.JSONTokener; import org.slf4j.Logger; @@ -84,6 +80,7 @@ import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import java.io.File; import java.sql.Clob; import java.text.DateFormat; import java.text.DecimalFormat; @@ -185,6 +182,10 @@ public class SarBussionessStandServiceImpl extends ServiceImpl importSarBussionessStandFile2(MultipartFile file) throws Exception { + //获取文件全称 + String fileNameStr = file.getOriginalFilename(); + //获取最后.的位置 + int pos = fileNameStr.lastIndexOf("."); + //获取压缩文件名称并以小写显示 + String fileStr = fileNameStr.substring(pos + 1).toLowerCase(); + //校验是否是zip文件 + if (!fileStr.equals("zip")){ + return Result.error("请上传zip格式的文件"); + } + //进行拼接获取文件名称 + String fileName = fileNameStr.substring(0,pos); + //获取路径和文件名称 + String path = filePath + "/" + fileName; + + File saveDirectory = new File(path); + //判断saveDirectory中是否是文件夹 + if (!saveDirectory.isDirectory()){ + saveDirectory.mkdir(); + } + //将文件写入到指定路径中 + FileUtils.copyInputStreamToFile(file.getInputStream(),new File(path + "/" + fileNameStr)); + try { + //解压缩 + String zipEntryName = FileUnZip.unZipFiles(path + "/" + fileNameStr, path); + //获取文件信息 + List fileList = readImpExcelFile(zipEntryName); + //判断获取到的文件数量 + if (fileList.size() < 1){ + return Result.error("上传的文件是空文件"); + } + File excelfile = fileList.get(0); + if (excelfile.getName().contains(".xls") || excelfile.getName().contains("xlsx")) { + for (File file1 : fileList){ + excelfile = file1; + } + Workbook workbook = null; + try { + workbook = WorkbookFactory.create(excelfile); + List> dataList = new ArrayList<>(); + if (workbook != null){ + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); + Sheet sheet = workbook.getSheetAt(0); + if (sheet != null){ + StringBuilder sb = new StringBuilder(); + //获取总条数 + int rowNum = sheet.getLastRowNum(); + //循环遍历 + for (int i = 0;i < rowNum;i++){ + Row row = sheet.getRow(i); + Row headerRow = sheet.getRow(0); + boolean isBlank = sarLawsInfoEOService.isRowEmpty(row); + if (row != null && !isBlank) { + int columNos = headerRow.getLastCellNum();// 表头总共的列数 + Map rowList = new LinkedHashMap<>(); + for (int j = 0; j < columNos; j++) { + Cell cell = row.getCell(j); + Cell headerCell = headerRow.getCell(j); + if (cell != null) { + if (i == 0) { + cell.setCellType(CellType.STRING); + sb.append(cell.getStringCellValue() + ","); + } else { + if (CellType.NUMERIC == cell.getCellType() && HSSFDateUtil.isCellDateFormatted(cell)) { + Date d = cell.getDateCellValue(); + rowList.put(headerCell.getStringCellValue(), sdf.format(d)); + } else { + cell.setCellType(CellType.STRING); + rowList.put(headerCell.getStringCellValue(), cell.getStringCellValue()); + } + } + } else { + if (i != 0) { + rowList.put(headerCell.getStringCellValue(), ""); + } + } + } + if (i != 0) { + dataList.add(rowList); + } + } + } + String fields = FieldConvertUtil.exportBaseFieldNamesBuss + "," + FieldConvertUtil.exportBaseFieldNamesBussCode + ","; + String excelHeader = sb.toString(); + if (!fields.equals(excelHeader)) { + workbook.close(); + //删除原上传文件 + FileUnZip.deleteDir(saveDirectory); + return Result.error("fail", "读取失败,请严格按照模板文件导入数据"); + } + } + } + //判断解析后的数据是否为空 + if (dataList != null && !dataList.isEmpty() ){ + + } + }catch (Exception e){ + FileUnZip.deleteDir(saveDirectory); + return Result.error("导入失败"); + } + + }else { + Result.error("您上传的文件中没有Excel文件"); + } + }catch (Exception e){ + FileUnZip.deleteDir(saveDirectory); + Result.error("导入失败,没有可导入的数据,请检查!"); + } + + return null; + } + + private static List readImpExcelFile(String path) { + File file = new File(path); + List resultlist = new ArrayList<>(); + if (file.isDirectory()) { + File[] files = file.listFiles(); + for (File fi : files) { + // 对文件进行过滤,读取所有文件 + String name = fi.getName(); + //文件不为空 添加 + if (name != null){ + resultlist.add(fi); + } + } + } + return resultlist; + } + + }