格式如下:
HttpServletResponse response = RequestAndResponseUtils.getResponse();
HttpServletResponse response = RequestAndResponseUtils.getResponse();
ExcelUtils.setResponseInfo(response, FILE_NAME+ System.currentTimeMillis()+XLSX);
ServletOutputStream outputStream = response.getOutputStream();
ExcelWriter excelWriter = EasyExcelFactory
.write(outputStream)
.build();
WriteSheet writeSheet = null;
for (int i : 5) {
scProcesses.setType(key);
//获取头部
List<List<String>> head = new ArrayList<>();
//获取策略
List<WriteHandler> strategy = new ArrayList<>();
//数据
List data = new ArrayList<>() ;
ExcelWriterSheetBuilder writerSheetBuilder = EasyExcelFactory
.writerSheet(i, "sheet名称"+i)
.needHead(true)
.head(head);
//设置策略
if (!CollectionUtils.isEmpty(strategy)){
for (WriteHandler writeHandler : strategy) {
writerSheetBuilder.registerWriteHandler(writeHandler);
}
}
writeSheet =writerSheetBuilder.build();
//写入sheet
excelWriter.write(data),writeSheet);
}
@Service("entiretyExcelExportService")
@Slf4j
public class EntiretyExcelExportServiceImpl implements EntiretyExcelExportService {
@Resource
private ExcelExpHandleFactory factory;
/**
* 导出的excel文件名称
*/
private static final String FILE_NAME = "bia报表";
private static final String XLSX = ".xlsx";
/**
* @param planId 计划id
* @author zhuyijun
* @date 2021/12/20 9:47
* @return void
*/
@SuppressWarnings("unchecked")
@Override
public void doExcelExp(String planId) {
if (StringUtils.isEmpty(planId)){
throw new HatechException("计划id不能为空");
}
ExcelExpProcessDTO scProcesses = new ExcelExpProcessDTO();
scProcesses.setPlanId(planId);
HttpServletResponse response = RequestAndResponseUtils.getResponse();
ExcelUtils.setResponseInfo(response, FILE_NAME+ System.currentTimeMillis()+XLSX);
ExcelWriter excelWriter = null;
try(ServletOutputStream outputStream = response.getOutputStream()) {
ExcelExpContent<String, BaseExcelExpHandleService> excelExpContent = factory.getExcelExpContent();
Map<String, BaseExcelExpHandleService> serviceMap = excelExpContent.logHandleMap;
Set<String> keys = serviceMap.keySet();
Map<String,List<Object>> map = new ConcurrentHashMap<>(64);
for (String key : keys) {
scProcesses.setType(key);
List data = serviceMap.get(key).getData(scProcesses);
map.put(key,data);
}
excelWriter = EasyExcelFactory
.write(outputStream)
.build();
WriteSheet writeSheet = null;
Set<String> strings = map.keySet();
int i=0;
for (String key : strings) {
scProcesses.setType(key);
//获取头部
List<List<String>> head = serviceMap.get(key).head(scProcesses);
//获取策略
List<WriteHandler> strategy = serviceMap.get(key).getStrategy();
ExcelWriterSheetBuilder writerSheetBuilder = EasyExcelFactory
.writerSheet(i, ExcelExpHandleTypeEnum.getNameByCode(key))
.needHead(true)
.head(head);
//设置策略
if (!CollectionUtils.isEmpty(strategy)){
for (WriteHandler writeHandler : strategy) {
writerSheetBuilder.registerWriteHandler(writeHandler);
}
}
writeSheet =writerSheetBuilder.build();
//写入sheet
excelWriter.write(map.getOrDefault(key,null),writeSheet);
++i;
}
} catch (IOException e) {
log.error("导出失败", e);
}finally {
if (excelWriter != null){
excelWriter.finish();
}
}
}
}