首页 > 基础资料 博客日记
Java 使用multipartFile对象解析Execl
2024-05-29 11:00:06基础资料围观531次
文章Java 使用multipartFile对象解析Execl分享给大家,欢迎收藏Java资料网,专注分享技术知识
1.需要使用 multipartFile 包 package org.springframework.web.multipart;
2.数据校验
public String exportVehicleViol(MultipartFile multipartFile) {
try {
//对前端传递的文件进行校验
if (multipartFile == null && multipartFile.getSize() == 0) {
return "文件上传错误,重新上传";
}
//获取文件名称 判断文件是否为 Execl
String filename = multipartFile.getOriginalFilename();
if (!(filename.endsWith(".xls") || filename.endsWith(".xlsx"))) {
return "文件上传格式有误,请重新上传";
}
List<EhicleViolation> ehicleViolations = null;
InputStream inputStream = multipartFile.getInputStream();
//根据文件格式 对应不同的api解析
if (filename.endsWith(".xlsx")) {
ehicleViolations = readXlsx(inputStream);
} else {
ehicleViolations = readXls(inputStream);
}
//数据保存
saveBatch(ehicleViolations);
} catch (IOException e) {
e.printStackTrace();
}
return JsonResult.Success("导入成功");
}
3.主要解析的业务逻辑
①解析xls
//解析xls
private List<EhicleViolation> readXls(InputStream inputStream) throws IOException {
HSSFWorkbook sheets = new HSSFWorkbook(inputStream);
//读取第一张sheet
HSSFSheet sheetAt = sheets.getSheetAt(0);
List<EhicleViolation> ehicleViolatsion = new ArrayList<>();
//rowNum = 3 从第三行开始获取值
for (int rowNum = 3; rowNum < sheetAt.getLastRowNum(); rowNum++) {
EhicleViolation ehicleViolation = new EhicleViolation();
HSSFRow row = sheetAt.getRow(rowNum);
if (row != null) {
//使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
//所以先使用setCellType()方法先将该单元格的类型设置为STRING
//然后poi会根据字符串读取它
row.getCell(0).setCellType(CellType.STRING);
row.getCell(1).setCellType(CellType.STRING);
row.getCell(2).setCellType(CellType.STRING);
row.getCell(3).setCellType(CellType.STRING);
row.getCell(4).setCellType(CellType.STRING);
row.getCell(5).setCellType(CellType.STRING);
row.getCell(6).setCellType(CellType.STRING);
row.getCell(7).setCellType(CellType.STRING);
row.getCell(8).setCellType(CellType.STRING);
String stringCellValue0 = row.getCell(0).getStringCellValue();
String stringCellValue1 = row.getCell(1).getStringCellValue();
if (StringUtils.isNotBlank(stringCellValue1)) {
ehicleViolation.setUserDept(stringCellValue1);
}
//根据自己需要 获取表格中的数据
String stringCellValue2 = row.getCell(2).getStringCellValue();
if (StringUtils.isNotBlank(stringCellValue2)) {
ehicleViolation.setVehicleNumber(stringCellValue2);
} else {
continue;
}
}
EehicleViolations.add(EehicleViolation);
}
return EehicleViolations;
}
②解析xlsx
//解析xlsx
private List<VmsVehicleViolation> readXlsx(InputStream inputStream) throws IOException {
XSSFWorkbook sheets1 = new XSSFWorkbook(inputStream);
XSSFSheet sheetAt1 = sheets1.getSheetAt(0);
List<EhicleViolation> ehicleViolatsion = new ArrayList<>();
//rowNum = 3 从第三行开始获取值
for (int rowNum = 3; rowNum < sheetAt.getLastRowNum(); rowNum++) {
EhicleViolation ehicleViolation = new EhicleViolation();
HSSFRow row = sheetAt.getRow(rowNum);
if (row != null) {
//使用了getStringCellValue()方法来获取值,POI会判断单元格的类型,如果非字符串类型就会抛出上面的异常。
//所以先使用setCellType()方法先将该单元格的类型设置为STRING
//然后poi会根据字符串读取它
row.getCell(0).setCellType(CellType.STRING);
row.getCell(1).setCellType(CellType.STRING);
row.getCell(2).setCellType(CellType.STRING);
row.getCell(3).setCellType(CellType.STRING);
row.getCell(4).setCellType(CellType.STRING);
row.getCell(5).setCellType(CellType.STRING);
row.getCell(6).setCellType(CellType.STRING);
row.getCell(7).setCellType(CellType.STRING);
row.getCell(8).setCellType(CellType.STRING);
String stringCellValue0 = row.getCell(0).getStringCellValue();
String stringCellValue1 = row.getCell(1).getStringCellValue();
if (StringUtils.isNotBlank(stringCellValue1)) {
ehicleViolation.setUserDept(stringCellValue1);
}
//根据自己需要 获取表格中的数据
String stringCellValue2 = row.getCell(2).getStringCellValue();
if (StringUtils.isNotBlank(stringCellValue2)) {
ehicleViolation.setVehicleNumber(stringCellValue2);
} else {
continue;
}
}
EehicleViolations.add(EehicleViolation);
}
return EehicleViolations;
}
注意:对于不同的Execl Java提供了不同的解析对象
xls使用HSSFWorkbook 对象进行解析
xlsx使用XSSWorkbook 对象进行解析
文章来源:https://blog.csdn.net/m0_66572126/article/details/130122136
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
相关文章
最新发布
- springboot~3.x项目中使用集成测试
- Java测试类、工具类与JavaBean对比解析
- SpringBoot-日志
- springboot~http2的支持
- 解疑释惑 - 日志体系之 slf4j + logback 组合(一)
- Web server failed to start. Port 8080 was already in use. 端口被占用
- Springboot 项目配置多数据源
- 伙伴匹配系统(移动端 H5 网站(APP 风格)基于Spring Boot 后端 + Vue3 - 05
- 剑指offer-23、搜索⼆叉树的后序遍历序列
- 一个表示金额的数字是 100000000L,这是多少米?