EasyExcelUtils
提供常见的文件同步和异步读取、文件导出、模板填充、自定义表头、自定义内容、从指定行开始读取数据、多Sheet导出等方法
public class EasyExcelUtils {
/**
* 同步无模型读(默认读取sheet0,从第2行开始读)
*
* @param filePath excel文件的绝对路径
*/
public static List<Map<Integer, String>> syncRead(String filePath) {
return EasyExcelFactory.read(filePath).sheet().doReadSync();
}
/**
* 同步无模型读(默认读取sheet0,从第2行开始读)
*
* @param inputStream excel文件的输入流
*/
public static List<Map<Integer, String>> syncRead(InputStream inputStream) {
return EasyExcelFactory.read(inputStream).sheet().doReadSync();
}
/**
* 同步无模型读(默认读取sheet0,从第2行开始读)
*
* @param file excel文件
*/
public static List<Map<Integer, String>> syncRead(File file) {
return EasyExcelFactory.read(file).sheet().doReadSync();
}
/**
* 同步无模型读(自定义读取sheetX,从第2行开始读)
*
* @param filePath excel文件的绝对路径
* @param sheetNo sheet页号,从0开始
*/
public static List<Map<Integer, String>> syncRead(String filePath, Integer sheetNo) {
return EasyExcelFactory.read(filePath).sheet(sheetNo).doReadSync();
}
/**
* 同步无模型读(自定义读取sheetX,从第2行开始读)
*
* @param inputStream excel文件的输入流
* @param sheetNo sheet页号,从0开始
*/
public static List<Map<Integer, String>> syncRead(InputStream inputStream, Integer sheetNo) {
return EasyExcelFactory.read(inputStream).sheet(sheetNo).doReadSync();
}
/**
* 同步无模型读(自定义读取sheetX,从第2行开始读)
*
* @param file excel文件
* @param sheetNo sheet页号,从0开始
*/
public static List<Map<Integer, String>> syncRead(File file, Integer sheetNo) {
return EasyExcelFactory.read(file).sheet(sheetNo).doReadSync();
}
/**
* 同步无模型读(指定sheet和表头占的行数)
*
* @param filePath
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
*/
public static List<Map<Integer, String>> syncRead(String filePath, Integer sheetNo, Integer headRowNum) {
return EasyExcelFactory.read(filePath).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();
}
/**
* 同步无模型读(指定sheet和表头占的行数)
*
* @param inputStream
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
*/
public static List<Map<Integer, String>> syncRead(InputStream inputStream, Integer sheetNo, Integer headRowNum) {
return EasyExcelFactory.read(inputStream).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();
}
/**
* 同步无模型读(指定sheet和表头占的行数)
*
* @param file
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
*/
public static List<Map<Integer, String>> syncRead(File file, Integer sheetNo, Integer headRowNum) {
return EasyExcelFactory.read(file).sheet(sheetNo).headRowNumber(headRowNum).doReadSync();
}
/**
* 同步按模型读(默认读取sheet0,从第2行开始读)
*
* @param filePath
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
*/
public static <T> List<T> syncReadModel(String filePath, Class clazz) {
return EasyExcelFactory.read(filePath).sheet().head(clazz).doReadSync();
}
/**
* 同步按模型读(默认读取sheet0,从第2行开始读)
*
* @param inputStream
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
*/
public static <T> List<T> syncReadModel(InputStream inputStream, Class clazz) {
return EasyExcelFactory.read(inputStream).sheet().head(clazz).doReadSync();
}
/**
* 同步按模型读(默认读取sheet0,从第2行开始读)
*
* @param file
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
*/
public static <T> List<T> syncReadModel(File file, Class clazz) {
return EasyExcelFactory.read(file).sheet().head(clazz).doReadSync();
}
/**
* 同步按模型读(默认表头占一行,从第2行开始读)
*
* @param filePath
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
*/
public static <T> List<T> syncReadModel(String filePath, Class clazz, Integer sheetNo) {
return EasyExcelFactory.read(filePath).sheet(sheetNo).head(clazz).doReadSync();
}
/**
* 同步按模型读(默认表头占一行,从第2行开始读)
*
* @param inputStream
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
*/
public static <T> List<T> syncReadModel(InputStream inputStream, Class clazz, Integer sheetNo) {
return EasyExcelFactory.read(inputStream).sheet(sheetNo).head(clazz).doReadSync();
}
/**
* 同步按模型读(默认表头占一行,从第2行开始读)
*
* @param file
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
*/
public static <T> List<T> syncReadModel(File file, Class clazz, Integer sheetNo) {
return EasyExcelFactory.read(file).sheet(sheetNo).head(clazz).doReadSync();
}
/**
* 同步按模型读(指定sheet和表头占的行数)
*
* @param filePath
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
*/
public static <T> List<T> syncReadModel(String filePath, Class clazz, Integer sheetNo, Integer headRowNum) {
return EasyExcelFactory.read(filePath).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync();
}
/**
* 同步按模型读(指定sheet和表头占的行数)
*
* @param inputStream
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
*/
public static <T> List<T> syncReadModel(InputStream inputStream, Class clazz, Integer sheetNo, Integer headRowNum) {
return EasyExcelFactory.read(inputStream).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync();
}
/**
* 同步按模型读(指定sheet和表头占的行数)
*
* @param file
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
*/
public static <T> List<T> syncReadModel(File file, Class clazz, Integer sheetNo, Integer headRowNum) {
return EasyExcelFactory.read(file).sheet(sheetNo).headRowNumber(headRowNum).head(clazz).doReadSync();
}
/**
* 异步无模型读(默认读取sheet0,从第2行开始读)
*
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param filePath 表头占的行数,从0开始(如果要连表头一起读出来则传0)
*/
public static <T> void asyncRead(String filePath, AnalysisEventListener<T> excelListener) {
EasyExcelFactory.read(filePath, excelListener).sheet().doRead();
}
/**
* 异步无模型读(默认读取sheet0,从第2行开始读)
*
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param inputStream 表头占的行数,从0开始(如果要连表头一起读出来则传0)
*/
public static <T> void asyncRead(InputStream inputStream, AnalysisEventListener<T> excelListener) {
EasyExcelFactory.read(inputStream, excelListener).sheet().doRead();
}
/**
* 异步无模型读(默认读取sheet0,从第2行开始读)
*
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param file 表头占的行数,从0开始(如果要连表头一起读出来则传0)
*/
public static <T> void asyncRead(File file, AnalysisEventListener<T> excelListener) {
EasyExcelFactory.read(file, excelListener).sheet().doRead();
}
/**
* 异步无模型读(默认表头占一行,从第2行开始读)
*
* @param filePath 表头占的行数,从0开始(如果要连表头一起读出来则传0)
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param sheetNo sheet页号,从0开始
*/
public static <T> void asyncRead(String filePath, AnalysisEventListener<T> excelListener, Integer sheetNo) {
EasyExcelFactory.read(filePath, excelListener).sheet(sheetNo).doRead();
}
/**
* 异步无模型读(默认表头占一行,从第2行开始读)
*
* @param inputStream 表头占的行数,从0开始(如果要连表头一起读出来则传0)
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param sheetNo sheet页号,从0开始
*/
public static <T> void asyncRead(InputStream inputStream, AnalysisEventListener<T> excelListener, Integer sheetNo) {
EasyExcelFactory.read(inputStream, excelListener).sheet(sheetNo).doRead();
}
/**
* 异步无模型读(默认表头占一行,从第2行开始读)
*
* @param file 表头占的行数,从0开始(如果要连表头一起读出来则传0)
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param sheetNo sheet页号,从0开始
*/
public static <T> void asyncRead(File file, AnalysisEventListener<T> excelListener, Integer sheetNo) {
EasyExcelFactory.read(file, excelListener).sheet(sheetNo).doRead();
}
/**
* 异步无模型读(指定sheet和表头占的行数)
*
* @param filePath
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
* @return
*/
public static <T> void asyncRead(String filePath, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum) {
EasyExcelFactory.read(filePath, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
}
/**
* 异步无模型读(指定sheet和表头占的行数)
*
* @param inputStream
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
*/
public static <T> void asyncRead(InputStream inputStream, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum) {
EasyExcelFactory.read(inputStream, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
}
/**
* 异步无模型读(指定sheet和表头占的行数)
*
* @param file
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
*/
public static <T> void asyncRead(File file, AnalysisEventListener<T> excelListener, Integer sheetNo, Integer headRowNum) {
EasyExcelFactory.read(file, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
}
/**
* 异步按模型读取(默认读取sheet0,从第2行开始读)
*
* @param filePath
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
*/
public static <T> void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz) {
EasyExcelFactory.read(filePath, clazz, excelListener).sheet().doRead();
}
/**
* 异步按模型读取(默认读取sheet0,从第2行开始读)
*
* @param inputStream
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
*/
public static <T> void asyncReadModel(InputStream inputStream, AnalysisEventListener<T> excelListener, Class clazz) {
EasyExcelFactory.read(inputStream, clazz, excelListener).sheet().doRead();
}
/**
* 异步按模型读取(默认读取sheet0,从第2行开始读)
*
* @param file
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
*/
public static <T> void asyncReadModel(File file, AnalysisEventListener<T> excelListener, Class clazz) {
EasyExcelFactory.read(file, clazz, excelListener).sheet().doRead();
}
/**
* 异步按模型读取(默认表头占一行,从第2行开始读)
*
* @param filePath
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
*/
public static <T> void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo) {
EasyExcelFactory.read(filePath, clazz, excelListener).sheet(sheetNo).doRead();
}
/**
* 异步按模型读取(默认表头占一行,从第2行开始读)
*
* @param inputStream
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
*/
public static <T> void asyncReadModel(InputStream inputStream, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo) {
EasyExcelFactory.read(inputStream, clazz, excelListener).sheet(sheetNo).doRead();
}
/**
* 异步按模型读取(默认表头占一行,从第2行开始读)
*
* @param file
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
*/
public static <T> void asyncReadModel(File file, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo) {
EasyExcelFactory.read(file, clazz, excelListener).sheet(sheetNo).doRead();
}
/**
* 异步按模型读取
*
* @param filePath
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
*/
public static <T> void asyncReadModel(String filePath, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum) {
EasyExcelFactory.read(filePath, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
}
/**
* 异步按模型读取
*
* @param inputStream
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
*/
public static <T> void asyncReadModel(InputStream inputStream, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum) {
EasyExcelFactory.read(inputStream, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
}
/**
* 异步按模型读取
*
* @param file
* @param excelListener 监听器,在监听器中可以处理行数据LinkedHashMap,表头数据,异常处理等
* @param clazz 模型的类类型(excel数据会按该类型转换成对象)
* @param sheetNo sheet页号,从0开始
* @param headRowNum 表头占的行数,从0开始(如果要连表头一起读出来则传0)
*/
public static <T> void asyncReadModel(File file, AnalysisEventListener<T> excelListener, Class clazz, Integer sheetNo, Integer headRowNum) {
EasyExcelFactory.read(file, clazz, excelListener).sheet(sheetNo).headRowNumber(headRowNum).doRead();
}
/**
* 无模板写文件
*
* @param filePath
* @param head 表头数据
* @param data 表内容数据
*/
public static void write(String filePath, List<List<String>> head, List<List<Object>> data) {
EasyExcel.write(filePath).head(head).sheet().doWrite(data);
}
/**
* 无模板写文件
*
* @param outputStream
* @param head 表头数据
* @param data 表内容数据
*/
public static void write(OutputStream outputStream, List<List<String>> head, List<List<Object>> data) {
EasyExcel.write(outputStream).head(head).sheet().doWrite(data);
}
/**
* 无模板写文件
*
* @param filePath
* @param head 表头数据
* @param data 表内容数据
* @param sheetNo sheet页号,从0开始
*/
public static void write(String filePath, List<List<String>> head, List<List<Object>> data, Integer sheetNo) {
EasyExcel.write(filePath).head(head).sheet(sheetNo).doWrite(data);
}
/**
* 无模板写文件
*
* @param outputStream
* @param head 表头数据
* @param data 表内容数据
* @param sheetNo sheet页号,从0开始
*/
public static void write(OutputStream outputStream, List<List<String>> head, List<List<Object>> data, Integer sheetNo) {
EasyExcel.write(outputStream).head(head).sheet(sheetNo).doWrite(data);
}
/**
* 无模板写文件
*
* @param filePath
* @param head 表头数据
* @param data 表内容数据
* @param sheetNo sheet页号,从0开始
* @param sheetName sheet名称
*/
public static void write(String filePath, List<List<String>> head, List<List<Object>> data, Integer sheetNo, String sheetName) {
EasyExcel.write(filePath).head(head).sheet(sheetNo, sheetName).doWrite(data);
}
/**
* 无模板写文件
*
* @param outputStream
* @param head 表头数据
* @param data 表内容数据
* @param sheetNo sheet页号,从0开始
* @param sheetName sheet名称
*/
public static void write(OutputStream outputStream, List<List<String>> head, List<List<Object>> data, Integer sheetNo, String sheetName) {
EasyExcel.write(outputStream).head(head).sheet(sheetNo, sheetName).doWrite(data);
}
/**
* 根据excel模板文件写入文件
*
* @param filePath
* @param templateFileName
* @param data
*/
public static void writeTemplate(String filePath, String templateFileName, List data) {
EasyExcel.write(filePath).withTemplate(templateFileName).sheet().doFill(data);
}
/**
* 根据excel模板文件写入文件
*
* @param outputStream
* @param templateFileName
* @param data
*/
public static void writeTemplate(OutputStream outputStream, String templateFileName, List data) {
EasyExcel.write(outputStream).withTemplate(templateFileName).sheet().doFill(data);
}
/**
* 根据excel模板文件写入文件
*
* @param file
* @param templateFileName
* @param data
*/
public static void writeTemplate(File file, String templateFileName, List data) {
EasyExcel.write(file).withTemplate(templateFileName).sheet().doFill(data);
}
/**
* 根据excel模板文件写入文件
*
* @param filePath
* @param templateFileName
* @param headClazz
* @param data
*/
public static void writeTemplate(String filePath, String templateFileName, Class headClazz, List data) {
EasyExcel.write(filePath, headClazz).withTemplate(templateFileName).sheet().doFill(data);
}
/**
* 根据excel模板文件写入文件
*
* @param outputStream
* @param templateFileName
* @param headClazz
* @param data
*/
public static void writeTemplate(OutputStream outputStream, String templateFileName, Class headClazz, List data) {
EasyExcel.write(outputStream, headClazz).withTemplate(templateFileName).sheet().doFill(data);
}
/**
* 根据excel模板文件写入文件
*
* @param file
* @param templateFileName
* @param headClazz
* @param data
*/
public static void writeTemplate(File file, String templateFileName, Class headClazz, List data) {
EasyExcel.write(file, headClazz).withTemplate(templateFileName).sheet().doFill(data);
}
/**
* 按模板写文件
*
* @param filePath
* @param headClazz 表头模板
* @param data 数据
*/
public static void write(String filePath, Class headClazz, List data) {
EasyExcel.write(filePath, headClazz).sheet().doWrite(data);
}
/**
* 按模板写文件
*
* @param outputStream
* @param headClazz 表头模板
* @param data 数据
*/
public static void write(OutputStream outputStream, Class headClazz, List data) {
EasyExcel.write(outputStream, headClazz).sheet().doWrite(data);
}
/**
* 按模板写文件
*
* @param file
* @param headClazz 表头模板
* @param data 数据
*/
public static void write(File file, Class headClazz, List data) {
EasyExcel.write(file, headClazz).sheet().doWrite(data);
}
/**
* 按模板写文件
*
* @param filePath
* @param headClazz 表头模板
* @param data 数据
* @param sheetNo sheet页号,从0开始
*/
public static void write(String filePath, Class headClazz, List data, Integer sheetNo) {
EasyExcel.write(filePath, headClazz).sheet(sheetNo).doWrite(data);
}
/**
* 按模板写文件
*
* @param outputStream
* @param headClazz 表头模板
* @param data 数据
* @param sheetNo sheet页号,从0开始
*/
public static void write(OutputStream outputStream, Class headClazz, List data, Integer sheetNo) {
EasyExcel.write(outputStream, headClazz).sheet(sheetNo).doWrite(data);
}
/**
* 按模板写文件
*
* @param file
* @param headClazz 表头模板
* @param data 数据
* @param sheetNo sheet页号,从0开始
*/
public static void write(File file, Class headClazz, List data, Integer sheetNo) {
EasyExcel.write(file, headClazz).sheet(sheetNo).doWrite(data);
}
/**
* 按模板写文件
*
* @param filePath
* @param headClazz 表头模板
* @param data 数据
* @param sheetNo sheet页号,从0开始
* @param sheetName sheet名称
*/
public static void write(String filePath, Class headClazz, List data, Integer sheetNo, String sheetName) {
EasyExcel.write(filePath, headClazz).sheet(sheetNo, sheetName).doWrite(data);
}
/**
* 按模板写文件
*
* @param outputStream
* @param headClazz 表头模板
* @param data 数据
* @param sheetNo sheet页号,从0开始
* @param sheetName sheet名称
*/
public static void write(OutputStream outputStream, Class headClazz, List data, Integer sheetNo, String sheetName) {
EasyExcel.write(outputStream, headClazz).sheet(sheetNo, sheetName).doWrite(data);
}
/**
* 按模板写文件
*
* @param file
* @param headClazz 表头模板
* @param data 数据
* @param sheetNo sheet页号,从0开始
* @param sheetName sheet名称
*/
public static void write(File file, Class headClazz, List data, Integer sheetNo, String sheetName) {
EasyExcel.write(file, headClazz).sheet(sheetNo, sheetName).doWrite(data);
}
/**
* 按模板写文件
*
* @param filePath
* @param headClazz 表头模板
* @param data 数据
* @param writeHandler 自定义的处理器,比如设置table样式,设置超链接、单元格下拉框等等功能都可以通过这个实现(需要注册多个则自己通过链式去调用)
* @param sheetNo sheet页号,从0开始
* @param sheetName sheet名称
*/
public static void write(String filePath, Class headClazz, List data, WriteHandler writeHandler, Integer sheetNo, String sheetName) {
EasyExcel.write(filePath, headClazz).registerWriteHandler(writeHandler).sheet(sheetNo, sheetName).doWrite(data);
}
/**
* 按模板写文件(包含某些字段)
*
* @param filePath
* @param headClazz 表头模板
* @param data 数据
* @param includeCols 包含字段集合,根据字段名称显示
* @param sheetNo sheet页号,从0开始
* @param sheetName sheet名称
*/
public static void writeInclude(String filePath, Class headClazz, List data, Set<String> includeCols, Integer sheetNo, String sheetName) {
EasyExcel.write(filePath, headClazz).includeColumnFieldNames(includeCols).sheet(sheetNo, sheetName).doWrite(data);
}
/**
* 按模板写文件(排除某些字段)
*
* @param filePath
* @param headClazz 表头模板
* @param data 数据
* @param excludeCols 过滤排除的字段,根据字段名称过滤
* @param sheetNo sheet页号,从0开始
* @param sheetName sheet名称
*/
public static void writeExclude(String filePath, Class headClazz, List data, Set<String> excludeCols, Integer sheetNo, String sheetName) {
EasyExcel.write(filePath, headClazz).excludeColumnFieldNames(excludeCols).sheet(sheetNo, sheetName).doWrite(data);
}
/**
* 多个sheet页的数据链式写入
* ExcelUtil.writeWithSheets(outputStream)
* .writeModel(ExcelModel.class, excelModelList, "sheetName1")
* .write(headData, data,"sheetName2")
* .finish();
*
* @param outputStream
*/
public static EasyExcelWriterFactory writeWithSheets(OutputStream outputStream) {
EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(outputStream);
return excelWriter;
}
/**
* 多个sheet页的数据链式写入
* ExcelUtil.writeWithSheets(file)
* .writeModel(ExcelModel.class, excelModelList, "sheetName1")
* .write(headData, data,"sheetName2")
* .finish();
*
* @param file
*/
public static EasyExcelWriterFactory writeWithSheets(File file) {
EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(file);
return excelWriter;
}
/**
* 多个sheet页的数据链式写入
* ExcelUtil.writeWithSheets(filePath)
* .writeModel(ExcelModel.class, excelModelList, "sheetName1")
* .write(headData, data,"sheetName2")
* .finish();
*
* @param filePath
*/
public static EasyExcelWriterFactory writeWithSheets(String filePath) {
EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(filePath);
return excelWriter;
}
/**
* 多个sheet页的数据链式写入(失败了会返回一个有部分数据的Excel)
* ExcelUtil.writeWithSheets(response, exportFileName)
* .writeModel(ExcelModel.class, excelModelList, "sheetName1")
* .write(headData, data,"sheetName2")
* .finish();
*
* @param response
* @param exportFileName 导出的文件名称
*/
public static EasyExcelWriterFactory writeWithSheetsWeb(HttpServletResponse response, String exportFileName) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码
String fileName = URLEncoder.encode(exportFileName, "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcelWriterFactory excelWriter = new EasyExcelWriterFactory(response.getOutputStream());
return excelWriter;
}
}
EasyExcelWriterFactory
文件导出和模板填充实现多Sheet写入(链式使用)
public class EasyExcelWriterFactory {
private int sheetNo = 0;
private ExcelWriter excelWriter = null;
public EasyExcelWriterFactory(OutputStream outputStream) {
excelWriter = EasyExcel.write(outputStream).build();
}
public EasyExcelWriterFactory(File file) {
excelWriter = EasyExcel.write(file).build();
}
public EasyExcelWriterFactory(String filePath) {
excelWriter = EasyExcel.write(filePath).build();
}
/**
* 链式模板表头写入
*
* @param headClazz 表头格式
* @param data 数据 List<ExcelModel> 或者List<List<Object>>
* @return
*/
public EasyExcelWriterFactory writeModel(Class headClazz, List data) {
excelWriter.write(data, EasyExcel.writerSheet(this.sheetNo++).head(headClazz).build());
return this;
}
/**
* 链式模板表头写入
*
* @param headClazz 表头格式
* @param data 数据 List<ExcelModel> 或者List<List<Object>>
* @return
*/
public EasyExcelWriterFactory writeModel(Class headClazz, List data, String sheetName) {
excelWriter.write(data, EasyExcel.writerSheet(this.sheetNo++, sheetName).head(headClazz).build());
return this;
}
/**
* 链式自定义表头写入
*
* @param head
* @param data 数据 List<ExcelModel> 或者List<List<Object>>
* @param sheetName
* @return
*/
public EasyExcelWriterFactory write(List<List<String>> head, List data, String sheetName) {
excelWriter.write(data, EasyExcel.writerSheet(this.sheetNo++, sheetName).head(head).build());
return this;
}
/**
* 使用此类结束后,一定要关闭流
*/
public void finish() {
excelWriter.finish();
}
}
ExcelListener
文件读取大数据和多Sheet导入监听器(使用线程池和批量插入方法)
public class ExcelListener extends AnalysisEventListener<T> {
Logger log = LoggerFactory.getLogger(getClass());
private static final Integer BATCH_SIZE = 1000;
private Integer sheetNo;
private Executor executor;
private List<T> dataList = new ArrayList<>();
public ExcelListener(Integer sheetNo, Executor executor) {
this.sheetNo = sheetNo;
this.executor = executor;
}
@Override
public void invoke(T data, AnalysisContext analysisContext) {
log.info("解析到一条数据:{}", JSON.toJSONString(data));
dataList.add(data);
if (dataList.size() >= BATCH_SIZE) {
CompletableFuture.runAsync(() -> {
// 业务操作
// saveToDB(dataList);
}, executor);
dataList.clear();
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
log.info("已解析完所有数据!");
if (!dataList.isEmpty()) {
CompletableFuture.runAsync(() -> {
// 业务操作
// saveToDB(dataList);
}, executor);
dataList.clear();
}
}
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
if (exception instanceof ExcelDataConvertException) {
ExcelDataConvertException convertException = (ExcelDataConvertException) exception;
Integer row = convertException.getRowIndex();
log.error("sheetNo:{},第{}行数据转换失败,异常信息:{}", sheetNo, row, exception.getMessage());
} else {
log.error("导入其他异常信息:{}", exception.getMessage());
}
}
}
使用案例
@GetMapping("/download1")
public void download1(HttpServletResponse response) {
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\+", "%20");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
User user = new User();
user.setUserId(123);
user.setName("as");
user.setPhone("15213");
user.setEmail("5456");
user.setCreateTime(new Date());
EasyExcelUtils.write(response.getOutputStream(), User.class, Arrays.asList(user));
} catch (Exception e) {
e.printStackTrace();
}
}
@GetMapping("/download2")
public void download2(HttpServletResponse response) {
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\+", "%20");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
User user = new User();
user.setUserId(123);
user.setName("as");
user.setPhone("15213");
user.setEmail("5456");
user.setCreateTime(new Date());
EasyExcelUtils.write(response.getOutputStream(), User.class, Arrays.asList(user), 2);
} catch (Exception e) {
e.printStackTrace();
}
}
@GetMapping("/download3")
public void download3(HttpServletResponse response) {
try {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\+", "%20");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
User user = new User();
user.setUserId(123);
user.setName("as");
user.setPhone("15213");
user.setEmail("5456");
user.setCreateTime(new Date());
EasyExcelUtils.writeWithSheets(response.getOutputStream())
.writeModel(User.class, Arrays.asList(user))
.writeModel(User.class, Arrays.asList(user))
.finish();
} catch (Exception e) {
e.printStackTrace();
}
}
原文始发于微信公众号(爱编程的小生):Easyexcel(8-通用工具类)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/314679.html