com.swak.excel.ExcelReadHandler Maven / Gradle / Ivy
package com.swak.excel;
import com.alibaba.excel.read.listener.ReadListener;
import com.swak.excel.builder.SwakExcelReadBuilder;
import com.swak.excel.metadata.ExcelSheetData;
import com.swak.excel.metadata.ReadExcelParams;
import com.swak.excel.validation.BeanDataValidator;
import com.swak.excel.validation.BizRowDataValidator;
import com.swak.excel.validation.RowDataValidator;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.List;
import java.util.function.Function;
/**
* ExcelReadHandler.java
*
* @author colley.ma
* @since 3.0.0
**/
public class ExcelReadHandler {
private SwakExcelReadBuilder excelReadBuilder;
private ReadExcelParams readExcelParams = new ReadExcelParams();
public ExcelReadHandler(SwakExcelReadBuilder excelReadBuilder) {
this.excelReadBuilder = excelReadBuilder;
}
public static ExcelReadHandler read(MultipartFile file) {
return new ExcelReadHandler(SwakExcelReadBuilder.read(file));
}
public static ExcelReadHandler read(MultipartFile file, Class> head) {
return new ExcelReadHandler(SwakExcelReadBuilder.read(file, head));
}
public static ExcelReadHandler read(InputStream inputStream) {
return new ExcelReadHandler(SwakExcelReadBuilder.read(inputStream));
}
public static ExcelReadHandler read(InputStream inputStream, Class> head) {
return new ExcelReadHandler(SwakExcelReadBuilder.read(inputStream, head));
}
public ExcelReadHandler readExcelParams(ReadExcelParams readExcelParams) {
this.readExcelParams = readExcelParams;
return this;
}
public ExcelReadHandler head(Class> head) {
excelReadBuilder.head(head);
return this;
}
public ExcelReadHandler multiHead(Class>... headList) {
excelReadBuilder.multiHead(headList);
return this;
}
public ExcelReadHandler addBizValidator(BizRowDataValidator bizValidator) {
excelReadBuilder.addBizValidator(bizValidator);
return this;
}
ExcelReadHandler registerReadListener(ReadListener readListener) {
excelReadBuilder.registerReadListener(readListener);
return this;
}
public ExcelReadHandler addDataValidator(RowDataValidator> bizValidator) {
excelReadBuilder.addDataValidator(bizValidator);
return this;
}
public R doRead(Function, R> handler) {
List listData = doRead();
return handler.apply(listData);
}
public R doReadAll(Function handler) {
return handler.apply(doReadAll());
}
public ExcelSheetData doReadAll() {
return excelReadBuilder.doReadAllSheet(readExcelParams);
}
public List doRead() {
// 增加到第一位校验
return excelReadBuilder.addFirstValidator(BeanDataValidator.newDataValidator(readExcelParams.getGroups()))
.defaultReadHandler().doRead();
}
}