IntelligentRecognition/ah-jjsp-service/.svn/pristine/97/97904a98ba73f60ef044ad797fe...

309 lines
11 KiB
Plaintext
Raw Normal View History

2024-05-24 16:09:40 +08:00
package com.sercurityControl.proteam.util;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.*;
/**
* @author HeiZi
*/
public class ImportExcelHelper {
static int cell1=1;
static int cell2=2;
static int cell3=3;
static int cell4=4;
static int cell5=5;
static int cell6=6;
public static List<?> readExcel(MultipartFile file, Class<?> mClass) throws Exception {
String fix=".xls";
String fix2=".xlsx";
String fileName = file.getOriginalFilename();
System.out.println("OriginalFilename" + fileName);
if (!StringUtils.endsWithAny(fileName, fix,fix2)) {
throw new RuntimeException("不支持excel以外的文件导入");
}
InputStream inputStream = file.getInputStream();
String className = mClass.getSimpleName();
System.out.println("className" + className);
//根据指定的文件输入流导入Excel从而产生Workbook对象
Workbook workbook = null;
Sheet sheet = null;
if (fileName!=null && fileName.endsWith(fix)) {
workbook = new HSSFWorkbook(inputStream);
//获取Excel文档中的第一个表单
sheet = workbook.getSheetAt(0);
if (checkModal(sheet, className)) {
throw new RuntimeException("模板错误,请重新选择模板!");
}
} else if (fileName!=null && fileName.endsWith(fix2)) {
workbook = new XSSFWorkbook(inputStream);
//获取Excel文档中的第一个表单
sheet = workbook.getSheetAt(0);
if (checkModal(sheet, className)) {
throw new RuntimeException("模板错误,请重新选择模板!");
}
}
switch (className) {
case "VoiInfoEntity":
System.err.println("-----违章依据导入------");
break;
case "BackboneScoreEntity":
System.err.println("-----骨干成绩导入------");
break;
case "BidTowerVo":
System.err.println("-----标段杆塔导入------");
break;
case "TowerVo":
System.err.println("-----杆塔导入------");
break;
case "SupeConsUserVo":
System.err.println("-----监理/施工单位导入------");
break;
default:
break;
}
int sheetCount = 0;
if (workbook != null) {
workbook.getNumberOfSheets();
}
System.out.println("Sheet(表单)数量:" + sheetCount);
//获得最后一条记录得的行号从0开始
int totalRowNum = 0;
if (sheet != null) {
totalRowNum = sheet.getLastRowNum();
}
System.out.println("总记录数:" + (totalRowNum + 1));
return createBean(sheet, mClass);
}
public static boolean checkModal(Sheet sheet, String className) {
int colNum = sheet.getRow(0).getLastCellNum();
switch (className) {
case "VoiInfoEntity":
return colNum != 2;
case "BackboneScoreEntity":
return colNum != 12;
case "BidTowerVo":
return colNum != 7;
case "TowerVo":
case "SupeConsUserVo":
return colNum != 4;
default:
break;
}
return true;
}
private static List<JSONObject> createBean(Sheet sheet, Class<?> mClass) {
if (sheet == null || sheet.getLastRowNum() < 0) {
return null;
}
List<JSONObject> list = new ArrayList<>();
// 总列数
int last = sheet.getRow(0).getLastCellNum();
System.out.println("列数:" + last);
try {
//对Sheet中的每一行进行迭代
for (Row row : sheet) {
if (row == null){
continue;
}
// 第一行是标题栏
switch (mClass.getSimpleName()) {
case "BackboneScoreEntity":
case "BidTowerVo":
case "TowerVo":
case "SupeConsUserVo":
if (row.getRowNum() < 2){
continue;
}
break;
default:
if (row.getRowNum() < 1) {
continue;
}
break;
}
boolean isBlankRow = true;
for (Cell c : row) {
if (c.getCellType() != CellType.BLANK) {
isBlankRow = false;
break;
}
}
if (isBlankRow) {
// throw new RuntimeException("模板中含有无效的空行,请删除多余的空行后再导入!");
continue;
}
JSONObject obj = new JSONObject();
obj.put("rowNo", row.getRowNum() + 1);
switch (mClass.getSimpleName()) {
case "VoiInfoEntity":
satelliting(2, row);
if (row.getCell(1) != null){
// 违章依据
obj.put("notiInfo", row.getCell(1).getStringCellValue());
}
break;
case "BackboneScoreEntity":
backboneScoreEntity(obj,row);
break;
case "BidTowerVo":
bidTowerVo(obj,row);
break;
case "TowerVo":
satelliting(4, row);
if (row.getCell(1) != null){
// 杆塔编码名称
obj.put("towerName", row.getCell(1).getStringCellValue());
}
if (row.getCell(2) != null){
// 经度{
obj.put("lon", row.getCell(2).getStringCellValue());
}
// 纬度
if (row.getCell(3) != null){
obj.put("lat", row.getCell(3).getStringCellValue());
}
break;
case "SupeConsUserVo":
sueConsUser(obj,row);
break;
default:
break;
}
list.add(obj);
}
} catch (IllegalStateException e) {
e.printStackTrace();
throw new RuntimeException("模板中含有单元格数据格式不正确");
}
return list;
}
public static void sueConsUser(JSONObject obj, Row row){
satelliting(4, row);
if (row.getCell(1) != null){
// 单位名称
obj.put("name", row.getCell(1).getStringCellValue());
}
if (row.getCell(2) != null){
// 单位编码
obj.put("code", row.getCell(2).getStringCellValue());
}
if (row.getCell(3) != null){
// 备注
obj.put("remarks", row.getCell(3).getStringCellValue());
}
}
public static void backboneScoreEntity(JSONObject obj, Row row){
satelliting(12, row);
if (row.getCell(1) != null){
// 人员编码
obj.put("userCode", row.getCell(1).getStringCellValue());
}
if (row.getCell(2) != null){
// 姓名
obj.put("userName", row.getCell(2).getStringCellValue());
}
if (row.getCell(3) != null){
// 身份证号
obj.put("idNumber", row.getCell(3).getStringCellValue());
}
if (row.getCell(4) != null){
// 所属单位
obj.put("unit", row.getCell(4).getStringCellValue());
}
if (row.getCell(5) != null){
// 所属部门
obj.put("part", row.getCell(5).getStringCellValue());
}
if (row.getCell(6) != null){
// 试卷成绩
obj.put("score", row.getCell(6).getStringCellValue());
}
if (row.getCell(7) != null){
// 合格
obj.put("qualified", row.getCell(7).getStringCellValue());
}
if (row.getCell(8) != null){
// 考试时间
obj.put("testTime", row.getCell(8).getStringCellValue());
}
if (row.getCell(9) != null){
// 有效期
obj.put("effectiveTime", row.getCell(9).getStringCellValue());
}
if (row.getCell(10) != null){
// 工种
obj.put("workType", row.getCell(10).getStringCellValue());
}
if (row.getCell(11) != null){
// 人员
obj.put("peopleType", row.getCell(11).getStringCellValue());
}
}
public static void bidTowerVo(JSONObject obj, Row row){
satelliting(7, row);
if (row.getCell(cell1) != null){
// 标段工程编码
obj.put("bidNo", row.getCell(cell1).getStringCellValue());
}
if (row.getCell(cell2) != null){
// 工程编码
obj.put("proNo", row.getCell(cell2).getStringCellValue());
}
if (row.getCell(cell3) != null){
// 标段工程名称
obj.put("proName", row.getCell(cell3).getStringCellValue());
}
if (row.getCell(cell4) != null){
// 杆塔编码名称)
obj.put("towerName", row.getCell(cell4).getStringCellValue());
}
if (row.getCell(cell5) != null){
// 经度
obj.put("lon", row.getCell(cell5).getStringCellValue());
}
if (row.getCell(cell6) != null){
// 纬度
obj.put("lat", row.getCell(cell6).getStringCellValue());
}
}
private static void satelliting(int j, Row row) {
for (int i = 0; i < j; i++) {
if (row.getCell(i) != null) {
row.getCell(i).setCellType(CellType.STRING);
}
}
}
}