1. 程式人生 > 其它 >【SQL必知必會】第5課 高階過濾資料

【SQL必知必會】第5課 高階過濾資料

public abstract class BaseImportExcelBase {

/**
* 解析Excel檔案返回List資料集合
*
* @param file
* @return
*/
public List<List<Object>> importExcel(MultipartFile file) {
ExcelReader excelReader = ExcelFileUtil.getReader(file);
// 校驗列完整性
List<Object> columnTabNames = excelReader.readRow(BigDecimal.ZERO.intValue());
checkColumn(columnTabNames);
// 校驗資料
List<List<Object>> rows = excelReader.read(BigDecimal.ONE.intValue());
checkData(rows);

return rows;
}

/**
* 解析Excel檔案返回List資料Bean
*
* @param file
* @return
*/
public <T> List<T> importExcelBean(MultipartFile file, Class<T> beanType) throws Exception {
// 返回的資料
List<T> beanList = new ArrayList<>();
ExcelReader excelReader = ExcelFileUtil.getReader(file);
// 校驗列完整性
checkColumn(excelReader.readRow(BigDecimal.ZERO.intValue()));
// 讀入資料
List<List<Object>> rows = excelReader.read(BigDecimal.ONE.intValue());
checkData(rows);
for (int i = 0; i < rows.size(); i++) {
List<Object> row = rows.get(i);
// 每一行資料轉換
T bean = dataToBean(beanType, row,i);
beanList.add(bean);
}
return beanList;
}

/**
* 讀取大資料量的Excel
* 通過Sax方式讀取Excel,同時支援03和07格式
*
* @param file Excel檔案
* @param beanType bean型別
*/
public <T> List<T> importExcelRowHandlerBean(MultipartFile file, Class<T> beanType) {
// 返回的資料
List<T> beanList = new ArrayList<>();
ExcelUtil.readBySax(FileUtil.toFile(file), BigDecimal.ZERO.intValue(), (sheetIndex, rowIndex, rowList) -> {
if (rowIndex == BigDecimal.ZERO.intValue()) {
// 校驗列完整性
checkColumn(rowList);
} else {
boolean isBlank = rowList.stream().anyMatch(e -> ObjectUtil.isNotEmpty(e));
if (isBlank) {
// 每一行資料轉換
T bean = null;
try {
bean = dataToBean(beanType, rowList,(int)rowIndex);
} catch (Exception e) {
throw new BizException(e.getMessage());
}
beanList.add(bean);
}

}
});

return beanList;
}

/**
* 讀取大資料量的Excel
* 通過Sax方式讀取Excel,同時支援03和07格式
*
* @param file Excel檔案
* @param rowHandler 行處理器
*/
public void importExcelRowHandler(MultipartFile file, RowHandler rowHandler) {
ExcelUtil.readBySax(FileUtil.toFile(file), BigDecimal.ZERO.intValue(), (sheetIndex, rowIndex, rowList) -> {
if (rowIndex == BigDecimal.ZERO.intValue()) {
// 校驗列完整性
checkColumn(rowList);
} else {
// 校驗資料完整性
checkRowData(rowList);
rowHandler.handle(sheetIndex, rowIndex, rowList);
}
});
}

/**
* 檢驗表格列完整性
*
* @param columnTabNames 表頭資料
* @return
*/
protected abstract void checkColumn(List<Object> columnTabNames);

/**
* 校驗資料,使用importExcel方法時使用
*
* @param rows 所有行的資料
* @return
*/
protected abstract void checkData(List<List<Object>> rows);

/**
* 校驗資料,使用importExcelRowHandler方法時使用
*
* @param rows 每一行的資料
* @return
*/
protected abstract void checkRowData(List<Object> rows);

/**
* 將每一行的資料轉成bean
*
* @param beanType
* @param rowData
* @return
* @throws Exception
*/
private <T> T dataToBean(Class<T> beanType, List<Object> rowData,int rowIndex) throws Exception {
// 該bean的所有欄位
Field[] fields = beanType.getDeclaredFields();

// bean例項
T bean = beanType.newInstance();
for (Field field : fields) {
if (field.isAnnotationPresent(ExcelField.class)) {
field.setAccessible(true);
ExcelField annotation = field.getAnnotation(ExcelField.class);
// 列數
int column = annotation.column();
// 是否必填
boolean required = annotation.required();
// 錯誤資訊
String errorMsg = annotation.errorMsg();

// 每列資料
String columnData = "";
try {
columnData = Optional.ofNullable(rowData.get(column)).orElse("").toString();
} catch (Exception e) {
//throw new BizException(e.getMessage());
columnData="";
}

// 校驗資料
if (required && StrUtil.isBlank(columnData)) {
throw new BizException("[row index "+(rowIndex+1)+"]"+errorMsg);
}

field.set(bean, StrUtil.trim(columnData));
}
}

return bean;
}
}