Spring Boot中处理前端传过来的MultipartFile类型的Excel文件,数值匹配String或Numeric,判断是否存在空行,排除空行

在这里插入图片描述

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

@Override
public List<GreenMaintenanceSeedlingPlanList> exportExcel(MultipartFile file) throws IOException {

	//excel 日期格式 YYYY-MM-DD
	LocalDate epochStart = LocalDate.of(1899, 12, 30);

	//可处理.xls 或 .xlsx后缀的excel文件
	Workbook workbook;
	try (InputStream inputStream = file.getInputStream()) {
		workbook = WorkbookFactory.create(inputStream);
	} catch (InvalidFormatException e) {
		throw new RuntimeException(e);
	}
	//创建List存储数据
	List<GreenMaintenanceSeedlingPlanList> greenMaintenanceSeedlingPlanList = new ArrayList<>();
	Sheet sheetAt = workbook.getSheetAt(0);
	//获取最后一行
	int lastRowNum = sheetAt.getLastRowNum();
	//从第三行
	for (int i = 2; i <= lastRowNum; i++) {
		GreenMaintenanceSeedlingPlanList maintenanceSeedlingPlanList = new GreenMaintenanceSeedlingPlanList();
		Row row = sheetAt.getRow(i);
		if (row != null && !isRowEmpty(row)) { // 判断row是否为null && 判断某行是否为空行
			maintenanceSeedlingPlanList.setSeedlingName(getCellValueAsString(row.getCell(0)));
			maintenanceSeedlingPlanList.setSpecification(getCellValueAsString(row.getCell(1)));
			maintenanceSeedlingPlanList.setContractArea(getNumericCellValue(row.getCell(2)));
			maintenanceSeedlingPlanList.setDensity(getNumericCellValue(row.getCell(3)));
			maintenanceSeedlingPlanList.setDensityUnit(getCellValueAsString(row.getCell(4)));
			maintenanceSeedlingPlanList.setPredictReplanting(getNumericCellValue(row.getCell(5)));
			maintenanceSeedlingPlanList.setPredictSingle(getNumericCellValue(row.getCell(6)));
			maintenanceSeedlingPlanList.setPlanPayOut(getNumericCellValue(row.getCell(7)));
			maintenanceSeedlingPlanList.setRealityReplanting(getNumericCellValue(row.getCell(8)));
			maintenanceSeedlingPlanList.setRealitySingle(getNumericCellValue(row.getCell(9)));
			maintenanceSeedlingPlanList.setRealityPayOut(getNumericCellValue(row.getCell(10)));
			maintenanceSeedlingPlanList.setRepairDate(getRepairDate(row.getCell(11), epochStart));
			greenMaintenanceSeedlingPlanList.add(maintenanceSeedlingPlanList);
		}
	}
	return greenMaintenanceSeedlingPlanList;
}

/**
 * 判断某一行是否全部为空
 * @param row
 * @return
 */
private boolean isRowEmpty(Row row) {
	if (row == null) {
		return true;
	}
	for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++) {
		Cell cell = row.getCell(i, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL);
		if (cell != null && cell.getCellTypeEnum() != CellType.BLANK) {
			return false;
		}
	}
	return true;
}

/**
 * 处理String类型格式
 * @param cell
 * @return
 */
private String getCellValueAsString(Cell cell) {
	if (cell == null) {
		return null;
	}
	switch (cell.getCellTypeEnum()) {
		case STRING:
			return cell.getStringCellValue();
		case NUMERIC:
			return String.valueOf(cell.getNumericCellValue());
		default:
			return null;
	}
}

/**
 * 处理Double(Number)类型格式
 * @param cell
 * @return
 */
private Double getNumericCellValue(Cell cell) {
	if (cell == null) {
		return null;
	}
	if (cell.getCellTypeEnum() == CellType.STRING) {
		return Double.valueOf(cell.getStringCellValue());
	}
	if (cell.getCellTypeEnum() == CellType.NUMERIC) {
		return cell.getNumericCellValue();
	}
	return null;
}

/**
 * 处理LocalDate日期格式
 * @param cell
 * @param epochStart
 * @return
 */
private LocalDate getRepairDate(Cell cell, LocalDate epochStart) {
	if (cell == null) {
		return null;
	}
	if (cell.getCellTypeEnum() == CellType.NUMERIC) {
		double repairDateValue = cell.getNumericCellValue();
		return epochStart.plus((long) repairDateValue, ChronoUnit.DAYS);
	}
	return null;
}

在这里插入图片描述

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-09 23:10:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-09 23:10:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-04-09 23:10:02       82 阅读
  4. Python语言-面向对象

    2024-04-09 23:10:02       91 阅读

热门阅读

  1. lodash 使用

    2024-04-09 23:10:02       35 阅读
  2. OceanBase V4.X中常用的SQL(一)

    2024-04-09 23:10:02       31 阅读
  3. CentOS运行级别

    2024-04-09 23:10:02       35 阅读
  4. 用GCC把C语言文件编译成Intel语法的汇编代码

    2024-04-09 23:10:02       29 阅读
  5. PHP基础

    PHP基础

    2024-04-09 23:10:02      35 阅读
  6. Linux 配置dotnet 程序服务

    2024-04-09 23:10:02       34 阅读
  7. 【iOS ARKit】USDZ文件

    2024-04-09 23:10:02       34 阅读
  8. ES6语法笔记

    2024-04-09 23:10:02       39 阅读
  9. 计算机网络技术-TCP传输连接的建立和释放过程;

    2024-04-09 23:10:02       40 阅读
  10. 津津的储蓄计划

    2024-04-09 23:10:02       41 阅读
  11. HBase详解(4)

    2024-04-09 23:10:02       35 阅读
  12. Git汇总

    2024-04-09 23:10:02       30 阅读