|
|
@ -1,24 +1,31 @@
|
|
|
|
package com.gmh.controller;
|
|
|
|
package com.gmh.controller;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
import com.gmh.entity.GmhUser;
|
|
|
|
import com.gmh.entity.GmhUser;
|
|
|
|
import com.gmh.entity.R;
|
|
|
|
import com.gmh.entity.R;
|
|
|
|
|
|
|
|
import com.gmh.entity.SysBaijiaxing;
|
|
|
|
|
|
|
|
import com.gmh.entity.vo.ReadNameVo;
|
|
|
|
|
|
|
|
import com.gmh.service.SysBaijiaxingService;
|
|
|
|
import com.gmh.utils.POIExcelUtil;
|
|
|
|
import com.gmh.utils.POIExcelUtil;
|
|
|
|
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.Sheet;
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("/excel")
|
|
|
|
@RequestMapping("/excel")
|
|
|
|
public class ExcelController {
|
|
|
|
public class ExcelController {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public final ThreadLocal<Workbook> workbookThreadLocal = new ThreadLocal<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final String KEY_XINGMING = "姓名";
|
|
|
|
|
|
|
|
|
|
|
|
@RequestMapping("/t01")
|
|
|
|
@RequestMapping("/t01")
|
|
|
|
public R test01() {
|
|
|
|
public R test01() {
|
|
|
|
return R.ok("ok");
|
|
|
|
return R.ok("ok");
|
|
|
@ -27,58 +34,139 @@ public class ExcelController {
|
|
|
|
@RequestMapping("/readData")
|
|
|
|
@RequestMapping("/readData")
|
|
|
|
public R readSourceData(MultipartFile file) throws IOException {
|
|
|
|
public R readSourceData(MultipartFile file) throws IOException {
|
|
|
|
Workbook workbook = POIExcelUtil.readExcelFromInputStream(file.getInputStream(), file.getOriginalFilename());
|
|
|
|
Workbook workbook = POIExcelUtil.readExcelFromInputStream(file.getInputStream(), file.getOriginalFilename());
|
|
|
|
Integer nameCellIndex = null;
|
|
|
|
List<ReadNameVo> resultList = new ArrayList<>();
|
|
|
|
Map<String, List<GmhUser>> sheetNameList = new LinkedHashMap<>();
|
|
|
|
|
|
|
|
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
|
|
|
|
for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
|
|
|
|
List<GmhUser> nameList = new ArrayList<>();
|
|
|
|
ReadNameVo readNameVo = new ReadNameVo();
|
|
|
|
Sheet sheet = workbook.getSheetAt(i);
|
|
|
|
Sheet sheet = workbook.getSheetAt(i);
|
|
|
|
for (int r = 0; r <= sheet.getLastRowNum(); r++) {
|
|
|
|
List<Map<String, String>> excelMaps = POIExcelUtil.toListMap(sheet);
|
|
|
|
Row row = sheet.getRow(r);
|
|
|
|
if (excelMaps.isEmpty()) {
|
|
|
|
if (row == null) {
|
|
|
|
continue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<String, String> firstElement = excelMaps.get(0);
|
|
|
|
|
|
|
|
String xmKey = KEY_XINGMING;
|
|
|
|
|
|
|
|
for (String key : firstElement.keySet()) {
|
|
|
|
|
|
|
|
if (KEY_XINGMING.equals(firstElement.get(key))) {
|
|
|
|
|
|
|
|
xmKey = key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int lastCellNum = row.getLastCellNum();
|
|
|
|
}
|
|
|
|
for (int c = 0; c < lastCellNum; c++) {
|
|
|
|
if (firstElement.containsKey(xmKey)) {
|
|
|
|
Cell cell = row.getCell(c);
|
|
|
|
if (!KEY_XINGMING.equals(xmKey)) {
|
|
|
|
if (cell == null) {
|
|
|
|
excelMaps.remove(firstElement);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
String stringCellValue = POIExcelUtil.getStringCellValExcludeBlank(cell);
|
|
|
|
|
|
|
|
if (r == 0) {
|
|
|
|
|
|
|
|
// 第一行有姓名列
|
|
|
|
|
|
|
|
if ("姓名".equals(stringCellValue)) {
|
|
|
|
|
|
|
|
nameCellIndex = c;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 第一列没有姓名的情况
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (nameCellIndex != null) {
|
|
|
|
readNameVo = getReadNameDataForNameKey(excelMaps, xmKey);
|
|
|
|
Cell cell = row.getCell(nameCellIndex);
|
|
|
|
} else {
|
|
|
|
String stringCellValue = POIExcelUtil.getStringCellValExcludeBlank(cell);
|
|
|
|
Map<String, Integer> map = new HashMap<>();
|
|
|
|
if (!"姓名".equals(stringCellValue) && StrUtil.isNotBlank(stringCellValue)) {
|
|
|
|
for (Map<String, String> excelMap : excelMaps) {
|
|
|
|
GmhUser user = new GmhUser();
|
|
|
|
for (String key : excelMap.keySet()) {
|
|
|
|
user.setName(stringCellValue);
|
|
|
|
String value = excelMap.get(key);
|
|
|
|
nameList.add(user);
|
|
|
|
if (isChinaName(value)) {
|
|
|
|
|
|
|
|
Integer count = map.getOrDefault(key, 0);
|
|
|
|
|
|
|
|
map.put(key, ++count);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
System.out.println("未找到姓名列");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
List<Map.Entry<String,Integer>> list = new ArrayList<>(map.entrySet());
|
|
|
|
|
|
|
|
// list.sort(Comparator.comparingInt(Map.Entry::getValue)); //升序
|
|
|
|
|
|
|
|
list.sort((o1, o2) -> (o2.getValue() - o1.getValue()));
|
|
|
|
|
|
|
|
String key = list.get(0).getKey();
|
|
|
|
|
|
|
|
readNameVo = getReadNameDataForNameKey(excelMaps, key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!nameList.isEmpty()) {
|
|
|
|
readNameVo.setSheetName(sheet.getSheetName());
|
|
|
|
sheetNameList.put(sheet.getSheetName(), nameList);
|
|
|
|
resultList.add(readNameVo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return R.ok().setData(resultList);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ReadNameVo getReadNameDataForNameKey(List<Map<String, String>> excelMaps, final String key) {
|
|
|
|
|
|
|
|
ReadNameVo readNameVo = new ReadNameVo();
|
|
|
|
|
|
|
|
// 去重前
|
|
|
|
|
|
|
|
List<GmhUser> oldNameList = excelMaps.stream().map(map -> new GmhUser(map.get(key))).collect(Collectors.toList());
|
|
|
|
|
|
|
|
// 去重后
|
|
|
|
|
|
|
|
LinkedHashSet<GmhUser> newNameList = new LinkedHashSet<>(oldNameList);
|
|
|
|
|
|
|
|
readNameVo.setNameList(newNameList);
|
|
|
|
|
|
|
|
// 重复检测
|
|
|
|
|
|
|
|
int repeatCount = oldNameList.size() - newNameList.size();
|
|
|
|
|
|
|
|
if (repeatCount > 0) {
|
|
|
|
|
|
|
|
readNameVo.setHasRepeat(true);
|
|
|
|
|
|
|
|
readNameVo.setRepeatCount(repeatCount);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
readNameVo.setHasRepeat(false);
|
|
|
|
|
|
|
|
readNameVo.setRepeatCount(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return R.ok().setData(sheetNameList);
|
|
|
|
return readNameVo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@RequestMapping("/template")
|
|
|
|
@RequestMapping("/template")
|
|
|
|
public R templateUpload(MultipartFile file) throws IOException {
|
|
|
|
public R templateUpload(MultipartFile file) throws IOException {
|
|
|
|
Workbook workbook = POIExcelUtil.readExcelFromInputStream(file.getInputStream(), file.getOriginalFilename());
|
|
|
|
Workbook workbook = POIExcelUtil.readExcelFromInputStream(file.getInputStream(), file.getOriginalFilename());
|
|
|
|
|
|
|
|
List<Map<String, String>> maps = POIExcelUtil.toListMap(workbook.getSheetAt(0));
|
|
|
|
|
|
|
|
workbookThreadLocal.set(workbook);
|
|
|
|
|
|
|
|
return R.ok().setData(maps);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private SysBaijiaxingService baijiaxingService;
|
|
|
|
|
|
|
|
|
|
|
|
return R.ok();
|
|
|
|
private boolean isChinaName(String val) {
|
|
|
|
|
|
|
|
QueryWrapper<SysBaijiaxing> queryWrapper = new QueryWrapper<>();
|
|
|
|
|
|
|
|
queryWrapper.apply("{0} LIKE CONCAT(xingshi,'%')", val);
|
|
|
|
|
|
|
|
List<SysBaijiaxing> list = baijiaxingService.list(queryWrapper);
|
|
|
|
|
|
|
|
return !list.isEmpty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void oleCode() {
|
|
|
|
|
|
|
|
// Integer nameCellIndex = null;
|
|
|
|
|
|
|
|
// // Sheet:nameList
|
|
|
|
|
|
|
|
// List<ReadNameVo> readNameList = new ArrayList<>();
|
|
|
|
|
|
|
|
// for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
|
|
|
|
|
|
|
|
// List<GmhUser> nameList = new ArrayList<>();
|
|
|
|
|
|
|
|
// Sheet sheet = workbook.getSheetAt(i);
|
|
|
|
|
|
|
|
// for (int r = 0; r <= sheet.getLastRowNum(); r++) {
|
|
|
|
|
|
|
|
// Row row = sheet.getRow(r);
|
|
|
|
|
|
|
|
// if (row == null) {
|
|
|
|
|
|
|
|
// continue;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// List<List<String>> cellList = new ArrayList<>(row.getLastCellNum());
|
|
|
|
|
|
|
|
// for (int c = 0; c < row.getLastCellNum(); c++) {
|
|
|
|
|
|
|
|
// Cell cell = row.getCell(c);
|
|
|
|
|
|
|
|
// if (cell == null) {
|
|
|
|
|
|
|
|
// continue;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// String colString = CellReference.convertNumToColString(cell.getColumnIndex());
|
|
|
|
|
|
|
|
// System.out.println(colString);
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// String stringCellValue = POIExcelUtil.getStringCellValExcludeBlank(cell);
|
|
|
|
|
|
|
|
// if (r == 0) {
|
|
|
|
|
|
|
|
// // 第一行有姓名列
|
|
|
|
|
|
|
|
// if ("姓名".equals(stringCellValue)) {
|
|
|
|
|
|
|
|
// nameCellIndex = c;
|
|
|
|
|
|
|
|
// break;
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// // 第一行没有指明姓名列
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (nameCellIndex != null) {
|
|
|
|
|
|
|
|
// Cell cell = row.getCell(nameCellIndex);
|
|
|
|
|
|
|
|
// String stringCellValue = POIExcelUtil.getStringCellValExcludeBlank(cell);
|
|
|
|
|
|
|
|
// if (!"姓名".equals(stringCellValue) && StrUtil.isNotBlank(stringCellValue)) {
|
|
|
|
|
|
|
|
// GmhUser user = new GmhUser();
|
|
|
|
|
|
|
|
// user.setName(stringCellValue);
|
|
|
|
|
|
|
|
// nameList.add(user);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// System.out.println("未找到姓名列");
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (!nameList.isEmpty()) {
|
|
|
|
|
|
|
|
// ReadNameVo readNameVo = new ReadNameVo();
|
|
|
|
|
|
|
|
// readNameVo.setSheetName(sheet.getSheetName());
|
|
|
|
|
|
|
|
// readNameVo.setNameList(nameList);
|
|
|
|
|
|
|
|
// readNameList.add(readNameVo);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// return R.ok().setData(readNameList);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|