首先是公共方法
private String getCellValueAsString(Cell cell) {
if (cell == null) {
return "";
}
String value = "";
switch (cell.getCellType()) {
case STRING:
value = cell.getStringCellValue();
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
value = cell.getDateCellValue().toString();
} else {
value = Double.toString(cell.getNumericCellValue());
}
break;
case BOOLEAN:
value = Boolean.toString(cell.getBooleanCellValue());
break;
case FORMULA:
value = cell.getCellFormula();
break;
default:
return "";
}
// 移除所有换行符
return value.replaceAll("\\r\\n|\\n|\\r", "");
}
/**
* 解析联锁表表格的公共方法
* @param firstSheet 表格
* @param clazz 解析对象
* @param mapper
* @param passTitle 需要跳过的表头行数
* @param <T>
*/
public <T> void importJlLsb(Sheet firstSheet, Class<T> clazz, BiFunction<T, Cell, T> mapper, Integer passTitle) throws IOException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
// String filePath = "D:/project/联锁表格式.xlsx"; //这三行是解析本地文件的
// FileInputStream inputStream = new FileInputStream(new File(filePath));
// Workbook workbook = new XSSFWorkbook(inputStream);
Iterator<Row> iterator = firstSheet.iterator();
List<T> jlLSBList = new ArrayList<>();
// 跳过标题行
for (int i = 0; i < passTitle; i++) {
iterator.next();
}
while (iterator.hasNext()) {
Row nextRow = iterator.next();
T jlLSB = clazz.getDeclaredConstructor().newInstance();
// 遍历列
for (int i = 0; i < nextRow.getLastCellNum(); i++) {
Cell cell = nextRow.getCell(i);
if (cell == null || cell.getCellType() == CellType.BLANK) {
// 检查是否在合并单元格中
for (CellRangeAddress range : firstSheet.getMergedRegions()) {
if (range.isInRange(nextRow.getRowNum(), i)) {
// 获取合并单元格的第一个单元格的值
Row firstRow = firstSheet.getRow(range.getFirstRow());
Cell firstCell = firstRow.getCell(range.getFirstColumn());
// 设置对象的属性
jlLSB = mapper.apply(jlLSB, firstCell);
break;
}
}
} else {
// 设置对象的属性
jlLSB = mapper.apply(jlLSB, cell);
}
}
// 输出对象的属性
System.out.println(jlLSB.toString());
jlLSBList.add(jlLSB);
}
//workbook.close();
//inputStream.close();
}
然后是是调用方法,这里使用了两个对象,对应两个表格
public void ImportLcJlLsb(MultipartFile file) throws IOException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Workbook workbook = new XSSFWorkbook(file.getInputStream());
Sheet sheet = workbook.getSheet("列车进路");
importJlLsb(sheet, LcJlLSB.class, (jlLSB, cell) -> {
LcJlLSB lcJlLSB = (LcJlLSB) jlLSB;
setLcJlLSBProperty(lcJlLSB, cell, cell.getColumnIndex());
return lcJlLSB;
}, 2);
workbook.close();
}
private void setLcJlLSBProperty(LcJlLSB lcJlLSB, Cell cell, int columnIndex) {
// 根据列索引设置对象的属性
switch (columnIndex) {
case 0:
lcJlLSB.setDirectionOne(getCellValueAsString(cell));
break;
case 1:
lcJlLSB.setDirectionTow(getCellValueAsString(cell));
break;
case 3:
lcJlLSB.setJlNumber(getCellValueAsString(cell));
break;
// ... 其他字段
}
}
public void ImportYxJlLsb(MultipartFile file) throws IOException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Workbook workbook = new XSSFWorkbook(file.getInputStream());
Sheet sheet = workbook.getSheet("延续进路");
importJlLsb(sheet, YxJlLSB.class, (jlLSB, cell) -> {
YxJlLSB yxJlLSB = (YxJlLSB) jlLSB;
setYxJlLSBProperty(yxJlLSB, cell, cell.getColumnIndex());
return yxJlLSB;
}, 2);
workbook.close();
}
private void setYxJlLSBProperty(YxJlLSB yxJlLSB, Cell cell, int columnIndex) {
// 根据列索引设置对象的属性
switch (columnIndex) {
case 0:
yxJlLSB.setDirectionOne(getCellValueAsString(cell));
break;
case 1:
yxJlLSB.setDirectionTow(getCellValueAsString(cell));
break;
case 3:
yxJlLSB.setJlNumber(getCellValueAsString(cell));
break;
// ... 其他字段
}
}