package com.poloniex.spot.task.util;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.poloniex.spot.task.model.dto.ExcelSheetDto;
import lombok.extern.slf4j.Slf4j;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.List;
/**
* @author: bert.li 李艳强 2022/8/17
* description:
*/
@Slf4j
public class ExcelUtils {
private static String projectRootPath;
static {
Class<ExcelUtils> excelUtilsClass = ExcelUtils.class;
URL xmlPath = excelUtilsClass.getClassLoader().getResource("");
assert xmlPath != null;
projectRootPath = xmlPath.getPath();
}
public static File createExcelInProjectRootPath(List<ExcelSheetDto> excelSheetDtoList, String fileName){
ExcelWriter excelWriter = null;
FileOutputStream output = null;
try {
File file = new File(projectRootPath + fileName + ".xls");
if(!file.exists()){
file.createNewFile();
}
output = new FileOutputStream(file);
excelWriter = EasyExcel.write(output).build();
for (ExcelSheetDto excelSheetDto : excelSheetDtoList) {
WriteSheet writeSheet;
writeSheet = EasyExcel.writerSheet(excelSheetDto.getSheetIndex(), excelSheetDto.getSheetName())
.head(excelSheetDto.getBaseSheetDtoClass()).build();
excelWriter.write(excelSheetDto.getBaseSheetDtoList(), writeSheet);
}
return file;
}catch (Exception e){
log.error("createExcelInProjectRootPath occur error e:{}", e);
return null;
}finally {
if (excelWriter != null){
excelWriter.finish();
}
if (output != null){
try {
output.flush();
} catch (IOException e) {
log.error("createExcelInProjectRootPath output occur error e:{}", e);
}
}
}
}
}
//内容样式策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//垂直居中,水平居中
contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
//设置 自动换行
// contentWriteCellStyle.setWrapped(true);
// 字体策略
WriteFont contentWriteFont = new WriteFont();
// 字体大小
contentWriteFont.setFontHeightInPoints((short) 12);
contentWriteFont.setFontName("宋体");
contentWriteCellStyle.setWriteFont(contentWriteFont);
//头策略使用默认 设置字体大小
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
WriteFont headWriteFont = new WriteFont();
headWriteFont.setFontHeightInPoints((short) 12);
headWriteFont.setFontName("宋体");
headWriteCellStyle.setWriteFont(headWriteFont);
headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.index);
// String fileName="数据汇总表"+getNowDate();
String fileName= LocalDateTime.now().toString();
//将文件名称转码再使用
fileName = URLEncoder.encode(fileName, "UTF-8");
// response.setHeader("Content-Disposition", "attachment;filename*= UTF-8''"+ URLEncoder.encode(fileName,"UTF-8"));
response.setHeader("Content-Disposition", "attachment;filename=" + fileName+".xlsx") ;
OutputStream out=response.getOutputStream();
ExcelWriter excelWriter = EasyExcel.write(out).registerWriteHandler(new CustomCellWriteHandler()).
registerWriteHandler(new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle))
.registerWriteHandler(new SimpleRowHeightStyleStrategy((short)22,(short)22))
// .registerWriteHandler(new CustomCellWriteHandler())
.build();
//导入多个sheet
excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new CustomCellWriteHandler()).build();
//创建一个sheet
WriteSheet writeSheet = EasyExcel.writerSheet(0, "模板1").registerWriteHandler(horizontalCellStyleStrategy).build();
excelWriter.write(list, writeSheet);
//创建一个新的sheet
writeSheet = EasyExcel.writerSheet(1, "模板2").registerWriteHandler(horizontalCellStyleStrategy).build();
excelWriter.write(list, writeSheet);