easy excel + 修改样式

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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值