企业标准导入功能测试
This commit is contained in:
+9
-1
@@ -557,7 +557,15 @@ public class SarBussionessStandController extends BaseController<SarBussionessSt
|
||||
FileUnZip.deleteDir(saveDirectory);
|
||||
return Result.error("fail", "读取失败,请严格按照模板文件导入数据");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ApiOperation(value = "导入功能 测试")
|
||||
@PostMapping("/importSarBussionessStandFile2")
|
||||
public ResponseMessage<SarBussionessStand> importSarBussionessStandFile2(@RequestParam(value = "file",required = false)MultipartFile file) throws Exception{
|
||||
sarBussionessStandEOService.importSarBussionessStandFile2(file);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+2
@@ -58,5 +58,7 @@ public interface ISarBussionessStandService extends IService<SarBussionessStand>
|
||||
|
||||
ResponseMessage<SarBussionessStand> importSarBussionessStand(List<Map<String, String>> dataList, String filepath)throws Exception;
|
||||
|
||||
//导入
|
||||
ResponseMessage<SarBussionessStand> importSarBussionessStandFile2(MultipartFile file) throws Exception;
|
||||
}
|
||||
|
||||
|
||||
+146
-8
@@ -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<SarBussionessStan
|
||||
@Autowired
|
||||
private EncryptFileUtils encryptFileUtils;
|
||||
|
||||
@Autowired
|
||||
private ISarLawsInfoService sarLawsInfoEOService;
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
@@ -1978,4 +1979,141 @@ public class SarBussionessStandServiceImpl extends ServiceImpl<SarBussionessStan
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 导入 测试
|
||||
* @param file
|
||||
* @throws Exception
|
||||
*/
|
||||
@Override
|
||||
public ResponseMessage<SarBussionessStand> 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<File> 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<Map<String, String>> 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<String, String> 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<File> readImpExcelFile(String path) {
|
||||
File file = new File(path);
|
||||
List<File> 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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user