com.pig4cloud.plugin.excel.handler.ManySheetWriteHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of excel-spring-boot-starter Show documentation
Show all versions of excel-spring-boot-starter Show documentation
easy and high performance excel
package com.pig4cloud.plugin.excel.handler;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.pig4cloud.plugin.excel.annotation.ResponseExcel;
import com.pig4cloud.plugin.excel.config.ExcelConfigProperties;
import com.pig4cloud.plugin.excel.kit.ExcelException;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* @author lengleng
* @author L.cm
* @date 2020/3/29
*/
@RequiredArgsConstructor
public class ManySheetWriteHandler extends AbstractSheetWriteHandler {
private final ExcelConfigProperties configProperties;
private final ObjectProvider>> converterProvider;
/**
* 当且仅当List不为空且List中的元素也是List 才返回true
*
* @param obj 返回对象
* @return
*/
@Override
public boolean support(Object obj) {
if (obj instanceof List) {
List objList = (List) obj;
return !objList.isEmpty() && objList.get(0) instanceof List;
} else {
throw new ExcelException("@ResponseExcel 返回值必须为List类型");
}
}
@Override
@SneakyThrows
public void write(Object obj, HttpServletResponse response, ResponseExcel responseExcel) {
List objList = (List) obj;
List eleList = (List) objList.get(0);
ExcelWriter excelWriter = getExcelWriter(response, responseExcel, eleList, configProperties.getTemplatePath());
String[] sheets = responseExcel.sheet();
for (int i = 0; i < sheets.length; i++) {
//创建sheet
WriteSheet sheet;
if (StringUtils.hasText(responseExcel.template())) {
sheet = EasyExcel.writerSheet(i).build();
} else {
sheet = EasyExcel.writerSheet(i, sheets[i]).build();
}
// 写入sheet
excelWriter.write((List) objList.get(i), sheet);
}
excelWriter.finish();
}
@Override
public void registerCustomConverter(ExcelWriterBuilder builder) {
converterProvider.ifAvailable(converters -> converters.forEach(builder::registerConverter));
}
}