Browse Source

对excel版式进行粗略校验

main
maochaoying 2 years ago
parent
commit
9fe06a6b89
  1. 15
      pom.xml
  2. 27
      src/main/java/com/iflytop/nuclear/controller/UploadController.java
  3. 16
      src/main/java/com/iflytop/nuclear/service/UploadService.java
  4. 38
      src/main/java/com/iflytop/nuclear/service/impl/UploadServiceImpl.java
  5. 162
      src/main/java/com/iflytop/nuclear/utils/ExcelUtils.java
  6. BIN
      src/main/resources/public/a.xlsx
  7. BIN
      uploadfiles/xlsx/20230628/堆芯模版.xlsx

15
pom.xml

@ -37,6 +37,21 @@
<version>2.0.26</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>

27
src/main/java/com/iflytop/nuclear/controller/UploadController.java

@ -1,6 +1,10 @@
package com.iflytop.nuclear.controller;
import com.alibaba.fastjson2.JSONObject;
import com.iflytop.nuclear.service.UploadService;
import com.iflytop.nuclear.utils.ResponseData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
@ -24,13 +28,21 @@ import java.util.Set;
@RestController
@CrossOrigin
public class UploadController {
@Autowired
UploadService uploadService;
@PostMapping("/upload")
public String uploadToLocal(MultipartFile file){
public ResponseData uploadToLocal(MultipartFile file) throws IOException {
// 对当前文件中内容格式进行校验
boolean isValid = uploadService.verifyFileFormat(file);
if (!isValid) {
return ResponseData.fail("上传文件内容未按照标准版式排版");
}
// 获取文件原本的名字
String originName = file.getOriginalFilename();
// 判断文件是否是pdf文件
Set<String> set = new HashSet<>();
set.add(".xls");
set.add(".xlsx");
// 取出文件的后缀
int count = 0;
@ -43,10 +55,9 @@ public class UploadController {
String endName = originName.substring(count); //取出文件类型
String fileType = originName.substring(count + 1); //文件类型
if(!set.contains(endName)){
return new String("上传的文件类型错误,只能上传xls,xlsx类型的文件");
return ResponseData.fail("上传的文件类型错误,只能上传xlsx类型的文件");
}
// 创建保存路径
//日期格式
// 创建保存路径 后期使用保存在数据库中的id进行取名
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String format = sdf.format(new Date());
String savePath = System.getProperty("user.dir") + "/" + "uploadfiles" + "/" + fileType + "/" + format;
@ -60,9 +71,11 @@ public class UploadController {
try {
file.transferTo(new File(folder,saveName));
String filePath = savePath + "\\" + saveName;
return new String("文件路径为:" + filePath);
JSONObject res = new JSONObject();
res.put("path", filePath);
return ResponseData.success(res);
} catch (IOException e){
return new String(e.getMessage());
return ResponseData.fail(e.getMessage());
}
}
}

16
src/main/java/com/iflytop/nuclear/service/UploadService.java

@ -0,0 +1,16 @@
package com.iflytop.nuclear.service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/**
* @author cool
* @date 2023/6/28 14:20
*/
@Transactional
public interface UploadService {
boolean verifyFileFormat(MultipartFile file) throws IOException;
}

38
src/main/java/com/iflytop/nuclear/service/impl/UploadServiceImpl.java

@ -0,0 +1,38 @@
package com.iflytop.nuclear.service.impl;
import com.iflytop.nuclear.service.UploadService;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
/**
* @author cool
* @date 2023/6/28 14:21
*/
@Service
public class UploadServiceImpl implements UploadService {
@Override
public boolean verifyFileFormat(MultipartFile file) throws IOException {
InputStream inputStream = file.getInputStream();
Workbook wb = new XSSFWorkbook(inputStream);
//默认会跳过第一行标题
// 得到第一个shell
Sheet sheet = wb.getSheetAt(0);
// 得到Excel的行数
int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
int totalCells = 0;
// 得到Excel的列数(前提是有行数)
if (physicalNumberOfRows > 1 && sheet.getRow(1) != null) {
totalCells = sheet.getRow(1).getPhysicalNumberOfCells();
}
if (physicalNumberOfRows == 41 && totalCells == 14) {
return true;
}
return false;
}
}

162
src/main/java/com/iflytop/nuclear/utils/ExcelUtils.java

@ -0,0 +1,162 @@
// package com.iflytop.nuclear.utils;
//
// import com.iflytop.nuclear.model.NuclearCoreConfig;
// import org.apache.poi.hssf.usermodel.HSSFCell;
// import org.apache.poi.hssf.usermodel.HSSFWorkbook;
// import org.apache.poi.ss.usermodel.*;
// import org.apache.poi.xssf.usermodel.XSSFWorkbook;
// import org.springframework.web.multipart.MultipartFile;
//
// import java.io.IOException;
// import java.io.InputStream;
// import java.text.DecimalFormat;
// import java.time.LocalDate;
// import java.time.LocalDateTime;
// import java.time.format.DateTimeFormatter;
// import java.util.ArrayList;
// import java.util.List;
//
// /**
// * @author xjt
// * @version 1.0
// */
// public class ExcelUtils {
// //总行数
// private static int totalRows = 0;
// //总条数
// private static int totalCells = 0;
// //错误信息接收器
// private static String errorMsg;
//
// /**
// * 读EXCEL文件获取信息集合
// * @return
// */
// public static List<NuclearCoreConfig> getExcelInfo(MultipartFile mFile) {
// String fileName = mFile.getOriginalFilename();//获取文件名
// try {
// if (!validateExcel(fileName)) {// 验证文件名是否合格
// return null;
// }
// boolean isExcel2003 = true;// 根据文件名判断文件是2003版本还是2007版本
// if (isExcel2007(fileName)) {
// isExcel2003 = false;
// }
// List<NuclearCoreConfig> userList = createExcel(mFile.getInputStream(), isExcel2003);
// return userList;
// } catch (Exception e) {
// e.printStackTrace();
// }
// return null;
// }
// /**
// * 根据excel里面的内容读取客户信息
// * @param is 输入流
// * @param isExcel2003 excel是2003还是2007版本
// * @return
// * @throws IOException
// */
// public static List<NuclearCoreConfig> createExcel(InputStream is, boolean isExcel2003) {
// try{
// Workbook wb = null;
// if (isExcel2003) {// 当excel是2003时,创建excel2003
// wb = new HSSFWorkbook(is);
// } else {// 当excel是2007时,创建excel2007
// wb = new XSSFWorkbook(is);
// }
// List<NuclearCoreConfig> userList = readExcelValue(wb);// 读取Excel里面客户的信息
// return userList;
// } catch (IOException e) {
// e.printStackTrace();
// }
// return null;
// }
// /**
// * 读取Excel里面客户的信息
// * @param wb
// * @return
// */
// private static List<NuclearCoreConfig> readExcelValue(Workbook wb) {
// //默认会跳过第一行标题
// // 得到第一个shell
// Sheet sheet = wb.getSheetAt(0);
// // 得到Excel的行数
// totalRows = sheet.getPhysicalNumberOfRows();
// // 得到Excel的列数(前提是有行数)
// if (totalRows > 1 && sheet.getRow(0) != null) {
// totalCells = sheet.getRow(0).getPhysicalNumberOfCells();
// }
// List<BuiPatientInfo> userList = new ArrayList<BuiPatientInfo>();
// // 循环Excel行数
// for (int r = 1; r < totalRows; r++) {
// Row row = sheet.getRow(r);
// if (row == null){
// continue;
// }
// BuiPatientInfo user = new BuiPatientInfo();
// // 循环Excel的列
// for (int c = 0; c < totalCells-1; c++) {
// Cell cell = row.getCell(c);
// if (null != cell) {
// if (c == 0) { //第一列
// //如果是纯数字,将单元格类型转为String
// if(cell.getCellTypeEnum() == CellType.NUMERIC){
// cell.setCellType(CellType.STRING);
// }
// user.setPatientName(cell.getStringCellValue());//将单元格数据赋值给user
// }
// else if (c == 1){
// if(cell.getCellTypeEnum() == CellType.NUMERIC){
// cell.setCellType(CellType.STRING);
// }
// user.setPatientIdentity(cell.getStringCellValue());
// }
// else if (c == 2){
// if(cell.getCellTypeEnum() == CellType.NUMERIC){
// cell.setCellType(CellType.STRING);
// }
// String stringCellValue = cell.getStringCellValue();
// user.setHealingId(stringCellValue);
// }
// else if (c == 3){
// if(cell.getCellTypeEnum() == CellType.NUMERIC){
// cell.setCellType(CellType.STRING);
// }
// user.setElseInfo(String.valueOf(cell.getStringCellValue()));
// }
// }
// }
// //将excel解析出来的数据赋值给对象添加到list中
// user.setUpdateTime(LocalDateTime.now());
// user.setPatientBirthdate(IdentityUtil.getPatientBirth(user.getPatientIdentity())); //拿到身份中好通过已经写好的通过身份证信息获取出生年月工具类
// user.setPatientSex(IdentityUtil.getPatientSex(user.getPatientIdentity())); //通过省份证号获取男女信息工具类
// user.setPatientState(1); //当前实体类字段是固定的不是excel数据中的每个对象都可以再遍历完后增加固定属性值
// user.setCreateId(2L);
// user.setCreateTime(LocalDateTime.now());
// // 添加到list
// userList.add(user);
// }
// return userList;
// }
// /**
// * 验证EXCEL文件
// *
// * @param filePath
// * @return
// */
// public static boolean validateExcel(String filePath) {
// if (filePath == null || !(isExcel2003(filePath) || isExcel2007(filePath))) {
// errorMsg = "文件名不是excel格式";
// return false;
// }
// return true;
// }
// // @描述是否是2003的excel返回true是2003
// public static boolean isExcel2003(String filePath) {
// return filePath.matches("^.+\\.(?i)(xls)$");
// }
// //@描述是否是2007的excel返回true是2007
// public static boolean isExcel2007(String filePath) {
// return filePath.matches("^.+\\.(?i)(xlsx)$");
// }
// }

BIN
src/main/resources/public/a.xlsx

BIN
uploadfiles/xlsx/20230628/堆芯模版.xlsx

Loading…
Cancel
Save