Excel格式为表头加上值的格式
service接口层:
Map<String, Object> analysisExcel(MultipartFile file);
实现层(impl)
package com.yxf.util.service.impl;
import com.yxf.util.service.ExcelOperateService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class ExcelOperateServiceImpl implements ExcelOperateService {
@Override
public Map<String, Object> analysisExcel(MultipartFile file) {
List<String> titles = getTheFirstRow(file);
if (titles.size() > 0) {
List<Map<String, String>> valueMapList = getExcelValue(file, titles);
Map<String, Object> valueMap = new HashMap<>();
valueMap.put("title", titles);
valueMap.put("value", valueMapList);
return valueMap;
}
return null;
}
private List<Map<String, String>> getExcelValue(MultipartFile file, List<String> titles) {
List<Map<String, String>> valueMapLis = new ArrayList<>();
Sheet sheetAt = readExcel(file);
if (sheetAt != null) {
for (int i = 1; i <= sheetAt.getLastRowNum(); i++) {
Row row = sheetAt.getRow(i);
if (row != null) {
Map<String, String> valueMap = new HashMap<>();
for (int n = 0; n < titles.size(); n++) {
String value = getTheValue(row.getCell(n));
valueMap.put(titles.get(n), value);
}
valueMapLis.add(valueMap);
}
}
}
return valueMapLis;
}
private List<String> getTheFirstRow(MultipartFile file) {
List<String> titles = new ArrayList<>();
Sheet sheetAt = readExcel(file);
if (sheetAt != null) {
Row row = sheetAt.getRow(0);
if (row != null) {
for (Cell cell : row) {
String title = getTheValue(cell);
titles.add(title);
}
}
}
return titles;
}
private String getTheValue(Cell cell) {
String value;
if (cell == null) {
return "";
} else if (cell.getCellType().equals(CellType.FORMULA)) {
switch (cell.getCachedFormulaResultType()) {
case STRING:
value = cell.getStringCellValue();
break;
case NUMERIC:
NumberFormat nf = NumberFormat.getInstance();
nf.setGroupingUsed(false);
value = String.valueOf(nf.format(cell.getNumericCellValue()));
break;
case BOOLEAN:
value = String.valueOf(cell.getBooleanCellValue());
break;
default:
value = cell.getCellFormula();
}
} else if (cell.getCellType().equals(CellType.NUMERIC)) {
if (DateUtil.isCellDateFormatted(cell)) {
value = new SimpleDateFormat("yyyy-MM-dd").format(cell.getDateCellValue());
} else {
value = new BigDecimal(cell.toString()).stripTrailingZeros().toPlainString();
}
} else {
value = cell.toString() == null ? "" : cell.toString();
}
return value;
}
private Sheet readExcel(MultipartFile file) {
try {
return WorkbookFactory.create(file.getInputStream()).getSheetAt(0);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
出参样式:
{
"title": [
"排班工号",
"员工姓名",
"可用小时",
"开始日期",
"失效日期",
"日程类型",
"备注"
],
"value": [
{
"员工姓名": "张三",
"失效日期": "2022-9-31",
"开始日期": "2022-3-8",
"可用小时": "4",
"备注": "",
"排班工号": "1002658",
"日程类型": "市场监管局12315|调休"
},
{
"员工姓名": "李四",
"失效日期": "2022-9-31",
"开始日期": "2022-3-8",
"可用小时": "4",
"备注": "",
"排班工号": "1000239",
"日程类型": "市场监管局12315|调休"
},
{
"员工姓名": "王五",
"失效日期": "2022-9-31",
"开始日期": "2022-3-8",
"可用小时": "4",
"备注": "",
"排班工号": "1000508",
"日程类型": "市场监管局12315|调休"
}
]
}
|