当前位置:   article > 正文

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

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;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
@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);
	}

	//运算单元格处理
	FormulaEvaluator formulaEvaluator = workbook.getCreationHelper().createFormulaEvaluator();

	//创建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)));
			//运算单元格,不处理有值也会返回null			
			maintenanceSeedlingPlanList.setPlanPayOut(getNumericCellValueCalculate(row.getCell(7),formulaEvaluator));
			maintenanceSeedlingPlanList.setRealityReplanting(getNumericCellValue(row.getCell(8)));
			maintenanceSeedlingPlanList.setRealitySingle(getNumericCellValue(row.getCell(9)));
			//运算单元格,不处理有值也会返回null
			maintenanceSeedlingPlanList.setRealityPayOut(getNumericCellValueCalculate(row.getCell(10),formulaEvaluator));
			//日期格式处理,excel中输入的是YYYY/MM/DD格式,而后端实际接收到的是数值,需要转换成YYYY-MM-DD格式
			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日期格式 转换成 YYYY-MM-DD
 * @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;
}


/**
 * 单元格运算得到的结果
 * @param cell
 * @param formulaEvaluator
 * @return
 */
private Double getNumericCellValueCalculate(Cell cell, FormulaEvaluator formulaEvaluator) {
	return this.getCellValueAsNumericCalculateFunc(cell, formulaEvaluator);
}

/**
 * 处理单元格运算结果
 * @param cell
 * @param formulaEvaluator
 * @return
 */
public Double getCellValueAsNumericCalculateFunc(Cell cell, FormulaEvaluator formulaEvaluator) {
	if (cell == null) {
		return null;
	}
	if (cell.getCellTypeEnum() == CellType.NUMERIC) {
		return cell.getNumericCellValue();
	} else if (cell.getCellTypeEnum() == CellType.FORMULA) {
		if (formulaEvaluator != null) {
			CellValue cellValue = formulaEvaluator.evaluate(cell);
			return cellValue.getNumberValue();
		} else {
			throw new IllegalArgumentException("Formula cell found but formula evaluator is not provided.");
		}
	}
	return null;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/399320
推荐阅读
相关标签
  

闽ICP备14008679号