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

[excel导入]添加备注模板

上级 696cd8b3
...@@ -30,7 +30,7 @@ public class ExcelController { ...@@ -30,7 +30,7 @@ public class ExcelController {
@PostMapping("/import") @PostMapping("/import")
public ResponseEntity importExcel(){ public ResponseEntity importExcel(){
//@RequestParam int id //@RequestParam int id
int id = 14; int id = 15;
return excelData.getExcelData(id); return excelData.getExcelData(id);
} }
......
...@@ -10,7 +10,10 @@ import java.util.List; ...@@ -10,7 +10,10 @@ import java.util.List;
@NoArgsConstructor @NoArgsConstructor
@Data @Data
public class ExcelErrorVo { public class ExcelErrorVo {
private String fileName; private String fileName;//文件名字
List<String> result; private String uintName;//本单位名字
private String tel;//本单位联系方式
private String buildTel;//承建单位联系方式
List<String> result;//预览有问题的文件结果
} }
...@@ -12,6 +12,7 @@ public class ExcelVo { ...@@ -12,6 +12,7 @@ public class ExcelVo {
private Integer total; private Integer total;
private Integer successNum; private Integer successNum;
private Integer errorNum; private Integer errorNum;
private Integer importNum;
private List<ExcelErrorVo> list; private List<ExcelErrorVo> list;
} }
...@@ -81,34 +81,38 @@ public class ExcelData { ...@@ -81,34 +81,38 @@ public class ExcelData {
return ResultUtil.failed("文件路径不是文件夹!"); return ResultUtil.failed("文件路径不是文件夹!");
} }
File[] dataFiles = file.listFiles(); File[] dataFiles = file.listFiles();
if (dataFiles == null || dataFiles.length == 0){ if (dataFiles == null || dataFiles.length == 0){
excelLogService.save(new ExcelLog("导入失败", "文件路径下没有文件", "")); excelLogService.save(new ExcelLog("导入失败", "文件路径下没有文件", ""));
return ResultUtil.failed("文件路径下没有文件!"); return ResultUtil.failed("没有待导入文件!");
} }
//找到需要判断唯一值的规则
List<Rule> ruleList = ruleService.findAll().stream().filter(rule1 -> rule1.getCompare() == 2).collect(Collectors.toList()); List<Rule> ruleList = ruleService.findAll().stream().filter(rule1 -> rule1.getCompare() == 2).collect(Collectors.toList());
Rule rule = new Rule(); List<String> ruleKeyList = ruleList.stream().map(Rule::getRuleKey).collect(Collectors.toList());
if (ruleList.size() > 0){
rule = ruleList.get(0);
}
String successFileNameList = ""; String successFileNameList = "";
String errorFileNameList = ""; String errorFileNameList = "";
String errorReason = ""; String errorReason = "";
int total = dataFiles.length; int total = 0;
percent =0;
for (File dataFile : dataFiles){ for (File dataFile : dataFiles){
List<Map<String, Object>> mapList = new ArrayList<>(); List<Map<String, Object>> mapList = new ArrayList<>();
String fileName = dataFile.getName();
try {
//取模板,如果有模板文件传过来就解析模板文件并保存,否则就读本地模板 //取模板,如果有模板文件传过来就解析模板文件并保存,否则就读本地模板
Map<String, int[]> indexMap = getIndenxMap(null); Template template = new Template();
String tem = template.getTemplate("template.txt");
Map<String, int[]> indexMap = JSON.parseObject(tem, new TypeReference<Map<String, int[]>>(){});
if (indexMap.isEmpty()){ if (indexMap.isEmpty()){
excelLogService.save(new ExcelLog("导入失败", "没有模板文件", "")); excelLogService.save(new ExcelLog("导入失败", "没有模板文件", ""));
return ResultUtil.failed("没有模板文件导入失败!"); return ResultUtil.failed("没有模板文件导入失败!");
} }
String remarkTemplateString = template.getTemplate("remarksTemplate.txt");
List<ExcelRemarks> remarksTemplateList = JSON.parseObject(remarkTemplateString, new TypeReference<List<ExcelRemarks>>(){});
//取数据 //取数据
FileInputStream dataFis = new FileInputStream(dataFile); FileInputStream dataFis = new FileInputStream(dataFile);
Workbook dataWb = null; Workbook dataWb = null;
String fileName = dataFile.getName();
if(fileName.endsWith("xls")) { if(fileName.endsWith("xls")) {
dataWb = new HSSFWorkbook(dataFis); dataWb = new HSSFWorkbook(dataFis);
} else if(fileName.endsWith("xlsx")) { } else if(fileName.endsWith("xlsx")) {
...@@ -126,22 +130,25 @@ public class ExcelData { ...@@ -126,22 +130,25 @@ public class ExcelData {
} }
Map<String, Object> objectMap = new HashMap<>(); Map<String, Object> objectMap = new HashMap<>();
String className = null; String className = null;
//取模板位置数据
for (String key : indexMap.keySet()){ for (String key : indexMap.keySet()){
int[] value = indexMap.get(key); int[] value = indexMap.get(key);
int dataRow = value[0]; int dataRow = value[0];
int dataCell = value[1]; int dataCell = value[1];
String[] classArray = key.split("[.]"); String[] classArray = key.split("[.]");
className = classArray[0]; className = classArray[0];
Row row = sheet1.getRow(dataRow);
row.getCell(dataCell).setCellType(CellType.STRING);
if (dataMap.get(className) != null && dataMap.get(className).keySet().contains(classArray[1])){ if (dataMap.get(className) != null && dataMap.get(className).keySet().contains(classArray[1])){
objectMap.put(classArray[1], sheet1.getRow(dataRow).getCell(dataCell).getStringCellValue()); objectMap.put(classArray[1], row.getCell(dataCell).getStringCellValue());
} }
if (Objects.equals(key, rule.getRuleKey())){ if (ruleKeyList.contains(key)){
//查询数据库是否有相同网络名称,有就更新 //查询数据库是否有相同网络名称,有就更新
List<QueryCondition> queryConditions = new ArrayList<>(); List<QueryCondition> queryConditions = new ArrayList<>();
QueryCondition queryCondition = new QueryCondition(); QueryCondition queryCondition = new QueryCondition();
queryCondition.setName(classArray[1]); queryCondition.setName(classArray[1]);
queryCondition.setType("="); queryCondition.setType("=");
queryCondition.setValue(sheet1.getRow(dataRow).getCell(dataCell).getStringCellValue()); queryCondition.setValue(row.getCell(dataCell).getStringCellValue());
queryConditions.add(queryCondition); queryConditions.add(queryCondition);
List<Map<String, Object>> list = modelService.complexQuery(className, null, queryConditions); List<Map<String, Object>> list = modelService.complexQuery(className, null, queryConditions);
if (list != null && list.size() > 0){ if (list != null && list.size() > 0){
...@@ -150,6 +157,25 @@ public class ExcelData { ...@@ -150,6 +157,25 @@ public class ExcelData {
} }
} }
} }
//取备注模板数据,并判断是否需要替换原数据
for (ExcelRemarks remark : remarksTemplateList){
int[] value = remark.getLocation();
int dataRow = value[0];
int dataCell = value[1];
Row row = sheet1.getRow(dataRow);
row.getCell(dataCell).setCellType(CellType.STRING);
String[] classArray = remark.getClassName().split("[.]");
Object templateData = objectMap.get(classArray[1]);
if (String.valueOf(templateData).contains(remark.getRemark())){
String remarkData = row.getCell(dataCell).getStringCellValue();
if ("append".equals(remark.getType())){
Object temData = objectMap.get(classArray[1]) == null ? "" : objectMap.get(classArray[1]);
remarkData = temData + remark.getJointMark() + remarkData;
objectMap.put(classArray[1], temData);
}
objectMap.put(classArray[1], remarkData);
}
}
if (!objectMap.isEmpty()){ if (!objectMap.isEmpty()){
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
result.put(className, objectMap); result.put(className, objectMap);
...@@ -167,7 +193,13 @@ public class ExcelData { ...@@ -167,7 +193,13 @@ public class ExcelData {
errorReason = "不是excel文件或保存失败"; errorReason = "不是excel文件或保存失败";
dataFile.renameTo(new File(errorPath + fileName)); dataFile.renameTo(new File(errorPath + fileName));
} }
total = total -1; total = total + 1;
}catch (Exception e){
e.printStackTrace();
errorFileNameList = errorFileNameList + "[" + fileName + "]";
continue;
}
} }
percent = total/dataFiles.length * 1.0; percent = total/dataFiles.length * 1.0;
if (!"".equals(errorFileNameList)){ if (!"".equals(errorFileNameList)){
...@@ -205,12 +237,18 @@ public class ExcelData { ...@@ -205,12 +237,18 @@ public class ExcelData {
int total = 0; int total = 0;
boolean isSuccess = true; boolean isSuccess = true;
try { try {
Map<String, int[]>indexMap = getIndenxMap(null); Template template = new Template();
String tem = template.getTemplate("template.txt");
Map<String, int[]> indexMap = JSON.parseObject(tem, new TypeReference<Map<String, int[]>>(){});
if (indexMap.isEmpty()){ if (indexMap.isEmpty()){
return ResultUtil.failed("没有excle模板!"); return ResultUtil.failed("没有excle模板!");
} }
File file = new File(path); File file = new File(path);
File[] dataFiles = file.listFiles(); File[] dataFiles = file.listFiles();
if (dataFiles == null){
return ResultUtil.failed("文件夹为空!");
}
total = dataFiles.length; total = dataFiles.length;
//用于判断excel内容是否符合选项 //用于判断excel内容是否符合选项
...@@ -222,17 +260,25 @@ public class ExcelData { ...@@ -222,17 +260,25 @@ public class ExcelData {
//取所有字段的引用map<字段名,引用列表> //取所有字段的引用map<字段名,引用列表>
Map<String, List<Quote>> he = modelHelper.getQuoteList(className); Map<String, List<Quote>> he = modelHelper.getQuoteList(className);
for (File dataFile : dataFiles){ for (File dataFile : dataFiles){
String fileName = dataFile.getName();
isSuccess = true;
String tel = null;//联系方式
String unitName = null;//联系方式
String buildTel = null;//承建单位
List<String> result = new ArrayList<>();
try {
FileInputStream dataFis = new FileInputStream(dataFile); FileInputStream dataFis = new FileInputStream(dataFile);
Workbook wb = null; Workbook wb = null;
String fileName = dataFile.getName();
if(fileName.endsWith("xls")) { if(fileName.endsWith("xls")) {
wb = new HSSFWorkbook(dataFis); wb = new HSSFWorkbook(dataFis);
} else if(fileName.endsWith("xlsx")) { } else if(fileName.endsWith("xlsx")) {
wb = new XSSFWorkbook(dataFis); wb = new XSSFWorkbook(dataFis);
} }
Map<String, List<String>> map = new HashMap<>(); Map<String, List<String>> map = new HashMap<>();
Map<String, List<String>> sheetNameMap = new HashMap<>();
Map<String, String> content = new HashMap<>(); Map<String, String> content = new HashMap<>();
List<String> result = new ArrayList<>();
Map<String, String> networkName = new HashMap<>(); Map<String, String> networkName = 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);
...@@ -243,13 +289,27 @@ public class ExcelData { ...@@ -243,13 +289,27 @@ public class ExcelData {
continue; continue;
} }
String sameNet = null; String sameNet = null;
String sanmeNetName = "";
Row row = null;
for (String key : indexMap.keySet()){ for (String key : indexMap.keySet()){
//key=className.property //key=className.property
int[] value = indexMap.get(key); int[] value = indexMap.get(key);
int dataRow = value[0]; int dataRow = value[0];
int dataCell = value[1]; int dataCell = value[1];
String data = sheet1.getRow(dataRow).getCell(dataCell).getStringCellValue(); row = sheet1.getRow(dataRow);
row.getCell(dataCell).setCellType(CellType.STRING);
String data = row.getCell(dataCell).getStringCellValue();
// TODO: 21-5-21
if ("diaoyan.unit_contact".equals(key) && tel == null){
tel = data;
}
if ("diaoyan.contractor_contact_information".equals(key) && buildTel == null){
buildTel = data;
}
if ("diaoyan.name_of_user".equals(key) && unitName == null){
unitName = data;
}
//判断是否是两个相同的sheet //判断是否是两个相同的sheet
if (content.get(sheet1.getSheetName()) == null || content.get(sheet1.getSheetName()).isEmpty()) { if (content.get(sheet1.getSheetName()) == null || content.get(sheet1.getSheetName()).isEmpty()) {
content.put(sheet1.getSheetName(), data); content.put(sheet1.getSheetName(), data);
...@@ -272,13 +332,10 @@ public class ExcelData { ...@@ -272,13 +332,10 @@ public class ExcelData {
} }
if (isQuote){ if (isQuote){
int k = result.size() + 1; int k = result.size() + 1;
System.out.println(); result.add(k + ".\"" + sheet1.getSheetName() + "\"第" + (dataRow + 1) + "行第" + (dataCell + 1) + "列不符合规则");
System.out.println(JSON.toJSONString(quotes));
result.add(k + "." + sheet1.getSheetName() + data + "不符合引用规则");
isSuccess = false; isSuccess = false;
} }
} }
//正则规则判断 //正则规则判断
List<Rule> ruleList = rules.stream().filter(rule1 -> rule1.getRuleKey().equals(key)).collect(Collectors.toList()); List<Rule> ruleList = rules.stream().filter(rule1 -> rule1.getRuleKey().equals(key)).collect(Collectors.toList());
if (!ruleList.isEmpty()) { if (!ruleList.isEmpty()) {
...@@ -288,15 +345,21 @@ public class ExcelData { ...@@ -288,15 +345,21 @@ public class ExcelData {
if (map.get(rule.getName())== null || map.get(rule.getName()).isEmpty()) { if (map.get(rule.getName())== null || map.get(rule.getName()).isEmpty()) {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
list.add(data); list.add(data);
List<String> list1 = new ArrayList<>();
list1.add(sheet1.getSheetName());
map.put(rule.getName(), list); map.put(rule.getName(), list);
sheetNameMap.put(rule.getName(), list1);
} else { } else {
if (!map.get(rule.getName()).contains(data)){
map.get(rule.getName()).add(data); map.get(rule.getName()).add(data);
} }
}
}else if (rule.getCompare() == 2){ }else if (rule.getCompare() == 2){
//判断统一文件中该字段是否唯一 //判断统一文件中该字段是否唯一
if (networkName.keySet().contains(data)) { if (networkName.keySet().contains(data)) {
sameNet = networkName.get(data); sameNet = networkName.get(data);
} }
sanmeNetName = rule.getName();
networkName.put(data, sheet1.getSheetName()); networkName.put(data, sheet1.getSheetName());
} }
Pattern p = Pattern.compile(rule.getRule()); Pattern p = Pattern.compile(rule.getRule());
...@@ -304,7 +367,7 @@ public class ExcelData { ...@@ -304,7 +367,7 @@ public class ExcelData {
if (!m.matches()) { if (!m.matches()) {
//不符合 //不符合
int k = result.size() + 1; int k = result.size() + 1;
result.add(k + "." + sheet1.getSheetName() + rule.getName() + "不符合正则规则"); result.add(k + ".\"" + sheet1.getSheetName() + "\"第" + (dataRow + 1) + "行第" + (dataCell + 1) + "列不符合规则");
isSuccess = false; isSuccess = false;
} }
...@@ -316,22 +379,25 @@ public class ExcelData { ...@@ -316,22 +379,25 @@ public class ExcelData {
String last = content.get(sameNet); String last = content.get(sameNet);
int k = result.size() + 1; int k = result.size() + 1;
if (Objects.equals(now, last)){ if (Objects.equals(now, last)){
result.add(k + "." + sameNet + "与" + sheet1.getSheetName() + "存在相同表"); result.add(k + ".\"" + sameNet + "\"与\"" + sheet1.getSheetName() + "\"存在内容相同表");
}else { }else {
result.add(k + "." + sameNet + "与" + sheet1.getSheetName() + "不同网络之间,网络名重名"); result.add(k + ".\"" + sameNet + "\"与\"" + sheet1.getSheetName() + "\"的\"" + sanmeNetName + "\"重名");
} }
isSuccess = false; isSuccess = false;
} }
} }
if (!map.isEmpty()){ if (!map.isEmpty() && map.size() > 1){
for (String key : map.keySet()){ for (String key : map.keySet()){
List<String> list = map.get(key); List<String> list = sheetNameMap.get(key);
if (1 < new HashSet<Object>(list).size()){ String sheetName = "";
for (String s : list){
sheetName = "\"" + s + "\"";
}
//不是全部相同 //不是全部相同
int k = result.size() + 1; int k = result.size() + 1;
result.add(k + "." + key + "在同一文件里数据不同"); result.add(k + "." + sheetName + "中\"" + key + "\"的数据不同");
isSuccess = false; isSuccess = false;
}
} }
} }
if (isSuccess){ if (isSuccess){
...@@ -341,16 +407,24 @@ public class ExcelData { ...@@ -341,16 +407,24 @@ public class ExcelData {
}else { }else {
// dataFile.renameTo(new File(errorPath + fileName)); // dataFile.renameTo(new File(errorPath + fileName));
errorNum = errorNum + 1; errorNum = errorNum + 1;
excelVos.add(new ExcelErrorVo(fileName, result)); excelVos.add(new ExcelErrorVo(fileName, unitName, tel, buildTel, result));
}
}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));
} }
} }
} catch (IOException 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,successNum, errorNum, excelVos); int importNum = successFile == null ? 0 : successFile.length;
ExcelVo excelVo = new ExcelVo(total,successNum, errorNum, importNum, excelVos);
return ResultUtil.success(excelVo, "预览成功"); return ResultUtil.success(excelVo, "预览成功");
} }
...@@ -361,6 +435,7 @@ public class ExcelData { ...@@ -361,6 +435,7 @@ public class ExcelData {
*/ */
public Map<String, int[]> getIndenxMap(MultipartFile file) throws IOException { public Map<String, int[]> getIndenxMap(MultipartFile file) throws IOException {
Map<String, int[]> indexMap = new HashMap<>(); Map<String, int[]> indexMap = new HashMap<>();
List<ExcelRemarks> remarksList = new ArrayList<>();
if (file != null){ if (file != null){
String fileName = file.getResource().getFilename(); String fileName = file.getResource().getFilename();
InputStream fis = null; InputStream fis = null;
...@@ -378,27 +453,43 @@ public class ExcelData { ...@@ -378,27 +453,43 @@ public class ExcelData {
row = sheet.getRow(index); row = sheet.getRow(index);
for(int i = row.getFirstCellNum(); i < row.getLastCellNum(); ++i) { for(int i = row.getFirstCellNum(); i < row.getLastCellNum(); ++i) {
row.getCell(i).setCellType(CellType.STRING); row.getCell(i).setCellType(CellType.STRING);
String data = row.getCell(i).getStringCellValue();
if (row.getCell(i) != null && data.contains("$")) {
if (row.getCell(i) != null && row.getCell(i).getStringCellValue().contains("$")) {
//{className.propertyName=[1,2]}
String type = row.getCell(i).getStringCellValue().substring(1);
int[] idenx = new int[2]; int[] idenx = new int[2];
idenx[0] = row.getRowNum(); idenx[0] = row.getRowNum();
idenx[1] = i; idenx[1] = i;
indexMap.put(type, idenx); String type = null;
if (data.contains("*")){
//备注内容,data=$A.name*备注*insert*其他
String[] re = data.split("[*]");
if (re.length > 3){
type = re[0].substring(1);
remarksList.add(new ExcelRemarks(type, re[1], re[2], re[3], idenx));
} }
}else {
//{className.propertyName=[1,2]},data=$A.name
type = data.substring(1);
indexMap.put(type.trim(), idenx);
} }
} }
Template template = new Template();
template.saveTemplate(JSON.toJSONString(indexMap));
}else {
Template template = new Template();
String tem = template.getTemplate();
if (tem.isEmpty()){
return indexMap;
} }
indexMap = JSON.parseObject(tem, new TypeReference<Map<String, int[]>>(){});
} }
Template template = new Template();
template.saveTemplate(JSON.toJSONString(indexMap), "template.txt");
template.saveTemplate(JSON.toJSONString(remarksList), "remarksTemplate.txt");
}
// else {
// Template template = new Template();
// String tem = template.getTemplate();
// if (tem.isEmpty()){
// return indexMap;
// }
// indexMap = JSON.parseObject(tem, new TypeReference<Map<String, int[]>>(){});
// }
return indexMap; return indexMap;
} }
...@@ -436,7 +527,7 @@ public class ExcelData { ...@@ -436,7 +527,7 @@ public class ExcelData {
} }
public ResponseEntity findPrecen(){ public ResponseEntity findPrecen(){
return ResultUtil.success(percent,"保存成功"); return ResultUtil.success(percent,"查询成功");
} }
} }
package com.tykj.model_layer.excel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
public class ExcelRemarks {
@ApiModelProperty("表名.字段名")
private String className;
@ApiModelProperty("备注标识")
private String remark;
@ApiModelProperty("连接方式")
private String type;
@ApiModelProperty("连接字符")
private String jointMark;
@ApiModelProperty("备注位置")
private int[] location;
}
...@@ -4,14 +4,13 @@ import java.io.*; ...@@ -4,14 +4,13 @@ import java.io.*;
import java.util.Properties; import java.util.Properties;
public class Template { public class Template {
private String path = System.getProperty("user.dir") + "/template.txt";
/** /**
* 把模板数据保存在txt文件中 * 把模板数据保存在txt文件中
* @param data 模板json数据;{className.propertyName:[1,2]} * @param data 模板json数据;{className.propertyName:[1,2]}
* key为类名.属性名,value为该属性在模板excel中的位置[行,列] * key为类名.属性名,value为该属性在模板excel中的位置[行,列]
*/ */
public void saveTemplate(String data){ public void saveTemplate(String data, String fileName){
String path = System.getProperty("user.dir") + "/" + fileName;
Properties properties = new Properties(); Properties properties = new Properties();
try { try {
System.out.println(path); System.out.println(path);
...@@ -34,8 +33,8 @@ public class Template { ...@@ -34,8 +33,8 @@ public class Template {
* 前端没有传模板的时候,读取本地模板 * 前端没有传模板的时候,读取本地模板
* @return * @return
*/ */
public String getTemplate(){ public String getTemplate(String fileName){
String pathname = path; String pathname = System.getProperty("user.dir") + "/" + fileName;
try{ try{
String result = ""; String result = "";
FileReader fileReader = null; FileReader fileReader = null;
......
...@@ -4,6 +4,16 @@ spring: ...@@ -4,6 +4,16 @@ spring:
password: Huang123+ password: Huang123+
url: jdbc:mysql://47.106.142.73:3306/dataTest?useSSL=false&serverTimezone=GMT%2b8&characterEncoding=utf-8&nullCatalogMeansCurrent=true url: jdbc:mysql://47.106.142.73:3306/dataTest?useSSL=false&serverTimezone=GMT%2b8&characterEncoding=utf-8&nullCatalogMeansCurrent=true
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
read-only: false
connection-timeout: 60000
idle-timeout: 60000
validation-timeout: 3000
max-lifetime: 60000
login-timeout: 5
maximum-pool-size: 60
minimum-idle: 10
jpa: jpa:
show-sql: true show-sql: true
hibernate: hibernate:
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论