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

[excel导入]修改百分比BUG

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