com.github.cosycode.ext.fileimport.excel.ExcelResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of extend-mod Show documentation
Show all versions of extend-mod Show documentation
扩展模块, 用于存放一些非常用的工具或模块的扩展类, 例如在poi基础上扩展的excel的导入模块, 模拟按键模块
The newest version!
package com.github.cosycode.ext.fileimport.excel;
import com.github.cosycode.common.lang.BaseRuntimeException;
import com.github.cosycode.ext.fileimport.base.ExcelType;
import com.github.cosycode.ext.fileimport.base.FieldMapping;
import com.github.cosycode.ext.fileimport.base.RecordMapping;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;
/**
* Description : 解析Excel的工具类
*
* created in 2019/8/15
*
*
* @author CPF
**/
@Slf4j
public class ExcelResolver {
/**
* 判断Excel的版本,获取Workbook
*/
public static Workbook getMatchWorkbook(@NonNull ExcelType excelType, InputStream in) throws IOException {
if (excelType == ExcelType.XLS) {
return new HSSFWorkbook(in);
} else if (excelType == ExcelType.XLSX) {
return new XSSFWorkbook(in);
}
throw new BaseRuntimeException("Unsupported file type");
}
/**
* 使用规定的解析器解析excel文件, 并将解析的结果返回
*
* @param is excel 文件流
* @param excelType excel 文件类型
* @param adapterClassSet 适配器 Set
* @return Excel 解析后的 Map>
* @throws IOException 文件流异常
* @throws InstantiationException class.newInstants 异常
* @throws IllegalAccessException map转Bean异常
* @throws InvocationTargetException map转Bean异常
*/
public static List> resolveRecord(@NonNull InputStream is, @NonNull ExcelType excelType,
@NonNull Set>> adapterClassSet)
throws IOException, InstantiationException, IllegalAccessException, InvocationTargetException {
// 初始化适配器
List> adapters = adapterClassSet.stream().map(it -> {
AbstractSheetBeanMappingAdapter> abstractSheetBeanMappingAdapter = null;
try {
abstractSheetBeanMappingAdapter = it.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
log.error("new Instance error", e);
}
return abstractSheetBeanMappingAdapter;
}).collect(Collectors.toList());
return resolveRecord(is, excelType, adapters);
}
/**
* 使用规定的解析器解析excel文件, 并将解析的结果返回
*
* @param is excel 文件流
* @param excelType excel 文件类型
* @param adapters 适配器 adapters
* @return Excel 解析后的 Map>
* @throws IOException 文件流异常
* @throws InstantiationException class.newInstants 异常
* @throws IllegalAccessException map转Bean异常
* @throws InvocationTargetException map转Bean异常
*/
public static List> resolveRecord(@NonNull InputStream is, @NonNull ExcelType excelType, List> adapters)
throws IOException, InstantiationException, IllegalAccessException, InvocationTargetException {
// 结果集
List> resultList = new ArrayList<>();
try (Workbook workbook = getMatchWorkbook(excelType, is)) {
for (int index = 0, len = workbook.getNumberOfSheets(); index < len; index++) {
Sheet sheet = workbook.getSheetAt(index);
if (sheet == null) {
continue;
}
// 根据 sheet名称来查找指定的适配器
String sheetName = sheet.getSheetName();
List> collect = adapters.stream().filter(it -> it.isMatchSheetName(sheetName)).collect(Collectors.toList());
if (collect.isEmpty()) {
continue;
}
if (collect.size() > 1) {
throw new BaseRuntimeException("Function call error, AbstractSheetBeanMappingAdapter conflict, resolveQuoteExcel found multiple matching SheetBeanMappings during parsing");
}
AbstractSheetBeanMappingAdapter> relateAdapter = collect.get(0);
SheetInfo sheetInfo = new SheetInfo();
sheetInfo.setSheetName(sheetName);
log.info("resolveQuoteExcel start!!! -> sheetName : {}", sheetName);
RecordMapping recordMapping = relateAdapter.getSheetBeanMapping();
// 解析sheet, 封装为 List