bug: excel导入修改

This commit is contained in:
wxyclub
2023-11-30 19:59:26 +08:00
parent 77ffa2e74c
commit fcd3118190
5 changed files with 21 additions and 9 deletions
@@ -24,6 +24,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Cleanup;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFDateUtil;
@@ -35,7 +36,9 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@@ -279,7 +282,9 @@ public class InsideOutsideMeetingServiceImpl extends ServiceImpl<InsideOutsideMe
} }
Workbook workbook = null; Workbook workbook = null;
try { try {
workbook = WorkbookFactory.create(excelFile); @Cleanup
InputStream inputStream = new FileInputStream(excelFile);
workbook = WorkbookFactory.create(inputStream);
} catch (IOException e) { } catch (IOException e) {
FileUnZip.deleteDir(saveDirectory); FileUnZip.deleteDir(saveDirectory);
return Result.error("导入失败,需要导入的数据有问题"); return Result.error("导入失败,需要导入的数据有问题");
@@ -29,6 +29,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.adc.da.slrs.sarLawsInformation.entity.SarLawsInformation; import com.adc.da.slrs.sarLawsInformation.entity.SarLawsInformation;
import com.adc.da.slrs.sarLawsInformation.service.SarLawsInformationService; import com.adc.da.slrs.sarLawsInformation.service.SarLawsInformationService;
import com.adc.da.slrs.sarLawsInformation.dao.SarLawsInformationDao; import com.adc.da.slrs.sarLawsInformation.dao.SarLawsInformationDao;
import lombok.Cleanup;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@@ -40,7 +41,9 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@@ -518,7 +521,9 @@ public class SarLawsInformationServiceImpl extends ServiceImpl<SarLawsInformatio
} }
ImportParams importParams = new ImportParams(); ImportParams importParams = new ImportParams();
importParams.setTitleRows(1); importParams.setTitleRows(1);
List<SarLawsInformation> sarLawsInformationList = ExcelImportUtil.importExcel(readImpExcelFile(zipEntryName).get(0), SarLawsInformation.class, importParams); @Cleanup
InputStream inputStream = new FileInputStream(excelFile);
List<SarLawsInformation> sarLawsInformationList = ExcelImportUtil.importExcel(inputStream, SarLawsInformation.class, importParams);
// 获取需要校验的字段 // 获取需要校验的字段
List<TsDicType> firstMaterialTypeList = dicTypeService.selectAllDicTypeNameByDicCode("firstMaterialType"); List<TsDicType> firstMaterialTypeList = dicTypeService.selectAllDicTypeNameByDicCode("firstMaterialType");
Set<String> firstMaterialTypeNameSet = firstMaterialTypeList.stream().map(TsDicType::getDicTypeName).collect(Collectors.toSet()); Set<String> firstMaterialTypeNameSet = firstMaterialTypeList.stream().map(TsDicType::getDicTypeName).collect(Collectors.toSet());
@@ -150,7 +150,7 @@ public class SarLawsTopicController extends BaseController<SarLawsTopicVO> {
@ApiOperation("Excel文件导入政策资料信息") @ApiOperation("Excel文件导入政策资料信息")
@PostMapping("/importLawsTopicInfo") @PostMapping("/importLawsTopicInfo")
public ResponseMessage<?> importLawsTopicInfo(@RequestParam(value = "file",required = false) MultipartFile file) throws ParseException { public ResponseMessage<?> importLawsTopicInfo(@RequestParam(value = "file",required = false) MultipartFile file) throws Exception {
if (file == null) { if (file == null) {
return Result.error("文件为空,请重新上传"); return Result.error("文件为空,请重新上传");
} }
@@ -1,7 +1,6 @@
package com.adc.da.slrs.sarLawsTopic.service; package com.adc.da.slrs.sarLawsTopic.service;
import com.adc.da.http.ResponseMessage; import com.adc.da.http.ResponseMessage;
import com.adc.da.slrs.sarLawsInformation.entity.SarLawsInformation;
import com.adc.da.slrs.sarLawsTopic.entity.SarLawsTopic; import com.adc.da.slrs.sarLawsTopic.entity.SarLawsTopic;
import com.adc.da.slrs.sarLawsTopic.entity.SarLawsTopicVO; import com.adc.da.slrs.sarLawsTopic.entity.SarLawsTopicVO;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
@@ -30,5 +29,5 @@ public interface SarLawsTopicService extends IService<SarLawsTopic> {
List<SarLawsTopicVO> getAllLawsTopic(SarLawsTopicVO sarLawsTopicVO); List<SarLawsTopicVO> getAllLawsTopic(SarLawsTopicVO sarLawsTopicVO);
ResponseMessage<?> importLawsTopicInfo(MultipartFile file); ResponseMessage<?> importLawsTopicInfo(MultipartFile file) throws Exception;
} }
@@ -26,6 +26,7 @@ import com.adc.da.slrs.sarLawsTopic.entity.SarLawsTopic;
import com.adc.da.slrs.sarLawsTopic.service.SarLawsTopicService; import com.adc.da.slrs.sarLawsTopic.service.SarLawsTopicService;
import com.adc.da.slrs.sarLawsTopic.dao.SarLawsTopicMapper; import com.adc.da.slrs.sarLawsTopic.dao.SarLawsTopicMapper;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import lombok.Cleanup;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFDateUtil; import org.apache.poi.hssf.usermodel.HSSFDateUtil;
@@ -37,8 +38,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.File; import java.io.*;
import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@@ -239,7 +239,7 @@ public class SarLawsTopicServiceImpl extends ServiceImpl<SarLawsTopicMapper, Sar
} }
@Override @Override
public ResponseMessage<?> importLawsTopicInfo(MultipartFile file) { public ResponseMessage<?> importLawsTopicInfo(MultipartFile file) throws Exception {
//获取文件全称 //获取文件全称
String fileNameStr = file.getOriginalFilename(); String fileNameStr = file.getOriginalFilename();
//获取最后.的位置 //获取最后.的位置
@@ -397,7 +397,10 @@ public class SarLawsTopicServiceImpl extends ServiceImpl<SarLawsTopicMapper, Sar
ImportParams importParams = new ImportParams(); ImportParams importParams = new ImportParams();
importParams.setTitleRows(1); importParams.setTitleRows(1);
importParams.setHeadRows(2); importParams.setHeadRows(2);
List<SarLawsTopicVO> lawsTopicVOList = ExcelImportUtil.importExcel(readImpExcelFile(zipEntryName).get(0), SarLawsTopicVO.class, importParams); @Cleanup
InputStream inputStream = new FileInputStream(excelFile);
List<SarLawsTopicVO> lawsTopicVOList = ExcelImportUtil.importExcel(inputStream, SarLawsTopicVO.class, importParams);
excelFile.delete();
// 去除空数据 // 去除空数据
int index = 0; int index = 0;
for (SarLawsTopicVO lawsTopicVO : lawsTopicVOList) { for (SarLawsTopicVO lawsTopicVO : lawsTopicVOList) {