提交 e4e016f5 authored 作者: xc's avatar xc

[excel导入]修改百分比BUG

上级 619afa4e
...@@ -13,6 +13,7 @@ import org.springframework.http.ResponseEntity; ...@@ -13,6 +13,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -55,7 +56,8 @@ public class ExcelController { ...@@ -55,7 +56,8 @@ public class ExcelController {
@ApiOperation("导入预览") @ApiOperation("导入预览")
@PostMapping("/rule") @PostMapping("/rule")
public ResponseEntity importRule(){ public ResponseEntity importRule(){
return excelData.compareRule(); int num = 9;
return excelData.compareRule(num, new ArrayList<>(), new ArrayList<>());
} }
@ApiOperation("新增excel路径") @ApiOperation("新增excel路径")
......
...@@ -10,10 +10,10 @@ import java.util.List; ...@@ -10,10 +10,10 @@ import java.util.List;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public class ExcelVo { public class ExcelVo {
private Integer total; private int total;
private Integer successNum; private int successNum;
private Integer errorNum; private int errorNum;
private Integer importNum; private int importNum;
private Date date; private Date date;
private List<ExcelErrorVo> list; private List<ExcelErrorVo> list;
......
...@@ -78,13 +78,15 @@ public class ExcelData { ...@@ -78,13 +78,15 @@ public class ExcelData {
//{className={propertyName=""}} //{className={propertyName=""}}
Map<String, Map<String, Object>> dataMap = JSON.parseObject(jsonData, Map<String, Map<String, Object>> dataMap = JSON.parseObject(jsonData,
new TypeReference<Map<String, Map<String, Object>>>(){}); new TypeReference<Map<String, Map<String, Object>>>(){});
File file = new File(tmpPath);
if (!file.isDirectory()){ List<File> dataFiles = new ArrayList<>();
excelLogService.save(new ExcelLog("导入失败", "文件路径不是文件夹", tmpPath)); List<File> erroeFiles = new ArrayList<>();
return ResultUtil.failed("文件路径不是文件夹!"); compareRule(1, dataFiles, erroeFiles);
} System.out.println(path);
File[] dataFiles = file.listFiles(); File[] successFile = new File(path).listFiles();
if (dataFiles == null || dataFiles.length == 0){ int totalNum = successFile == null ? 0 : successFile.length;
// File[] dataFiles = file.listFiles();
if (dataFiles == null || dataFiles.size() == 0){
excelLogService.save(new ExcelLog("导入失败", "文件路径下没有文件", "")); excelLogService.save(new ExcelLog("导入失败", "文件路径下没有文件", ""));
return ResultUtil.failed("没有待导入文件!"); return ResultUtil.failed("没有待导入文件!");
} }
...@@ -99,7 +101,9 @@ public class ExcelData { ...@@ -99,7 +101,9 @@ public class ExcelData {
int total = 0;//操作完成的总数量 int total = 0;//操作完成的总数量
int successNum = 0; int successNum = 0;
int errorNum = 0; int errorNum = 0;
for (File file1 : erroeFiles){
file1.renameTo(new File(errorPath + file1.getName()));
}
for (File dataFile : dataFiles){ for (File dataFile : dataFiles){
//传给保存接口的list //传给保存接口的list
List<Map<String, Object>> saveMapList = new ArrayList<>(); List<Map<String, Object>> saveMapList = new ArrayList<>();
...@@ -202,7 +206,7 @@ public class ExcelData { ...@@ -202,7 +206,7 @@ public class ExcelData {
errorNum = errorNum + 1; errorNum = errorNum + 1;
} }
total = total + 1; total = total + 1;
this.percent = (float)total/dataFiles.length; this.percent = (float)total/dataFiles.size();
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
errorFileNameList = errorFileNameList + "[" + fileName + "]"; errorFileNameList = errorFileNameList + "[" + fileName + "]";
...@@ -211,16 +215,18 @@ public class ExcelData { ...@@ -211,16 +215,18 @@ public class ExcelData {
} }
File[] successFile = new File(tmpPath).listFiles();
int importNum = successFile == null ? 0 : successFile.length; System.out.println(successPath);
ExcelVo excelVo = new ExcelVo(dataFiles.length, successNum, errorNum, importNum, new Date(),null); System.out.println(errorPath);
errorNum = errorNum + erroeFiles.size();
ExcelVo excelVo = new ExcelVo(totalNum, successNum, errorNum, 0, new Date(),null);
if (!"".equals(errorFileNameList)){ if (!"".equals(errorFileNameList)){
excelLogService.save(new ExcelLog("导入失败", errorReason, errorFileNameList)); excelLogService.save(new ExcelLog("导入失败", errorReason, errorFileNameList));
String s = "没有文件"; String s = "导入失败";
if (!"".equals(successFileNameList)){ if (!"".equals(successFileNameList)){
s = successFileNameList; s = "部分文件导入失败," + successFileNameList + "导入成功";
} }
return ResultUtil.failed(excelVo + "导入失败," + s + "导入成功"); return ResultUtil.failed(s);
}else { }else {
excelLogService.save(new ExcelLog("导入成功", "", successFileNameList)); excelLogService.save(new ExcelLog("导入成功", "", successFileNameList));
return ResultUtil.success(excelVo, "导入成功!"); return ResultUtil.success(excelVo, "导入成功!");
...@@ -237,7 +243,7 @@ public class ExcelData { ...@@ -237,7 +243,7 @@ public class ExcelData {
* 预览 * 预览
* @return * @return
*/ */
public ResponseEntity compareRule(){ public ResponseEntity compareRule(int num, List<File> resultFiles, List<File> errorFiles){
this.percent = 0; this.percent = 0;
this.previewResult = null; this.previewResult = null;
if (path == null || rootPath == null){ if (path == null || rootPath == null){
...@@ -249,6 +255,8 @@ public class ExcelData { ...@@ -249,6 +255,8 @@ public class ExcelData {
if (rootPath == null){ if (rootPath == null){
rootPath = System.getProperty("user.dir") + "/"; rootPath = System.getProperty("user.dir") + "/";
} }
File rootFile = new File(rootPath);
rootFile.mkdirs();
} }
tmpPath = rootPath + "/research/tmp/"; tmpPath = rootPath + "/research/tmp/";
File tmp = new File(tmpPath); File tmp = new File(tmpPath);
...@@ -258,8 +266,6 @@ public class ExcelData { ...@@ -258,8 +266,6 @@ public class ExcelData {
List<ExcelErrorVo> excelVos = new ArrayList<>(); List<ExcelErrorVo> excelVos = new ArrayList<>();
List<Rule> rules = ruleService.findAll(); List<Rule> rules = ruleService.findAll();
int successNum = 0;//符合规则文件数量
int errorNum = 0;//不符合规则文件数据
int total = 0;//总文件数量 int total = 0;//总文件数量
boolean isSuccess = true; boolean isSuccess = true;
try { try {
...@@ -273,7 +279,8 @@ public class ExcelData { ...@@ -273,7 +279,8 @@ public class ExcelData {
File file = new File(path); File file = new File(path);
File[] dataFiles = file.listFiles(); File[] dataFiles = file.listFiles();
if (dataFiles == null || dataFiles.length == 0){ if (dataFiles == null || dataFiles.length == 0){
return ResultUtil.failed("文件夹为空,请检查" + path + "下是否有excel文件!"); this.percent = 1;
return ResultUtil.success(new ExcelVo(), "空文件夹");
} }
total = dataFiles.length; total = dataFiles.length;
...@@ -308,6 +315,7 @@ public class ExcelData { ...@@ -308,6 +315,7 @@ public class ExcelData {
Map<String, String> sheetNameAndContent = new HashMap<>(); Map<String, String> sheetNameAndContent = new HashMap<>();
Map<String, String> networkNameAndSheetName = new HashMap<>(); Map<String, String> networkNameAndSheetName = new HashMap<>();
for (int i = 0; i < wb.getNumberOfSheets(); i++){ for (int i = 0; i < wb.getNumberOfSheets(); i++){
Sheet sheet1 = wb.getSheetAt(i); Sheet sheet1 = wb.getSheetAt(i);
if ("引用表(请勿改动)".equals(sheet1.getSheetName()) || sheet1 ==null){ if ("引用表(请勿改动)".equals(sheet1.getSheetName()) || sheet1 ==null){
continue; continue;
...@@ -316,6 +324,9 @@ public class ExcelData { ...@@ -316,6 +324,9 @@ public class ExcelData {
String sameNetName = "";//网络名称别名 String sameNetName = "";//网络名称别名
Row row = null; Row row = null;
for (String key : indexMap.keySet()){ for (String key : indexMap.keySet()){
if (result.size() > num){
break;
}
//key=className.property,从模板文件中取该字段的位置 //key=className.property,从模板文件中取该字段的位置
int[] value = indexMap.get(key); int[] value = indexMap.get(key);
int dataRow = value[0]; int dataRow = value[0];
...@@ -428,34 +439,27 @@ public class ExcelData { ...@@ -428,34 +439,27 @@ public class ExcelData {
} }
} }
if (isSuccess){ if (isSuccess){
dataFile.renameTo(new File(tmpPath + fileName)); resultFiles.add(dataFile);
successNum = successNum + 1;
}else { }else {
// dataFile.renameTo(new File(errorPath + fileName));
errorNum = errorNum + 1;
excelVos.add(new ExcelErrorVo(fileName, unitName, tel, buildTel, result)); excelVos.add(new ExcelErrorVo(fileName, unitName, tel, buildTel, result));
errorFiles.add(dataFile);
} }
System.out.println("++++++++++++++++=");
System.out.println(successNum + errorNum);
}catch (Exception e){ }catch (Exception e){
// e.printStackTrace();
errorNum = errorNum + 1;
int k = result.size() + 1; int k = result.size() + 1;
result.add(k + "." + "不是模板文件或读文件失败"); result.add(k + "." + "不是模板文件或读文件失败");
excelVos.add(new ExcelErrorVo(fileName, unitName, tel, buildTel, result)); excelVos.add(new ExcelErrorVo(fileName, unitName, tel, buildTel, result));
errorFiles.add(dataFile);
}
if (num != 1){
this.percent = (float) (resultFiles.size() + errorFiles.size())/dataFiles.length;
} }
this.percent = (float) (successNum + errorNum)/dataFiles.length;
System.out.println(percent + " lll " + dataFiles.length);
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return ResultUtil.failed("预览失败!"); return ResultUtil.failed("预览失败!");
} }
File[] successFile = new File(tmpPath).listFiles(); ExcelVo excelVo = new ExcelVo(total,resultFiles.size(), errorFiles.size(), 0, new Date(), excelVos);
int importNum = successFile == null ? 0 : successFile.length;
ExcelVo excelVo = new ExcelVo(total,successNum, errorNum, importNum, new Date(), excelVos);
this.previewResult = excelVo; this.previewResult = excelVo;
return ResultUtil.success(excelVo, "预览成功"); return ResultUtil.success(excelVo, "预览成功");
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论