用EasyExcel将数据写入流、导出工具类,设置样式说明

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author lsx
 * @Classname EasyExcelUtils
 * @Description TODO
 * @Date 2021-05-25 11:17
 */
public class EasyExcelUtils {
	
	@HeadRowHeight:标注在类上
	@ContentRowHeight:标注在类上
	@ColumnWidth:标注在类、字段上,列宽
	@HeadStyle:标注在类、字段上
	@HeadFontStyle:标注在类、字段上
	
	@Data
	@HeadRowHeight(40)
	@ContentRowHeight(20)
	public class Fruit {
	
	   @ColumnWidth(10)
	   private Integer id;
	
	   @ContentStyle(fillPatternType = FillPatternType.SOLID_FOREGROUND ,fillForegroundColor = 6)
	   private String name;
	
	   @HeadFontStyle(color = 20)
	   private Double price;
	}


    /**
     * @Author lsx
     * @Description //TODO 将数据写入流
     * @Date  2021-06-04  16:23
     * @param out
     * @param data
     * @param classz
     * @return void
    */
    public static void createExcel(ByteArrayOutputStream out, List data,Class classz) throws IOException {
        try {
            //设置只导出的字段
            Set<String> includeColumnFiledNames = new HashSet<>();
            includeColumnFiledNames.add("publishedServiceName");
            includeColumnFiledNames.add("billNumber");
            includeColumnFiledNames.add("billDeadline");
            includeColumnFiledNames.add("actualPayment");
            includeColumnFiledNames.add("paymentTime");
            EasyExcel.write(out, classz).includeColumnFiledNames(includeColumnFiledNames).sheet().registerWriteHandler(getStyleStrategy()).doWrite(data);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }


    /**
     * 导出excel核心方法
     *
     * @param
     */
    public static void export(HttpServletResponse response, Class head, List data, String sheetName) throws IOException {

        //设置只导出的字段
        Set<String> includeColumnFiledNames = new HashSet<>();
        includeColumnFiledNames.add("id");
        includeColumnFiledNames.add("i18nKey");
        includeColumnFiledNames.add("i18nValue");
        includeColumnFiledNames.add("language");
        includeColumnFiledNames.add("type");
        includeColumnFiledNames.add("appName");
        //给定导出实体类
        EasyExcel.write(response.getOutputStream(), head)
                .includeColumnFiledNames(includeColumnFiledNames)
                //给定工作表名称
                .sheet(sheetName)
                //给定样式
                .registerWriteHandler(getStyleStrategy())
                //给定导出数据
                .doWrite(data);
    }

    /**
     * 设置请求头、文件名
     *
     * @param fileName excel文件名
     */
    public static void setResponse(HttpServletResponse response, String fileName) throws UnsupportedEncodingException {
        //编码设置成UTF-8,excel文件格式为.xlsx
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        // 这里URLEncoder.encode可以防止中文乱码 和easyexcel本身没有关系
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
    }

    /**
     * 设置生成excel样式 去除默认表头样式及设置内容居中,如有必要可重载该方法给定参数配置不同样式
     *
     * @return HorizontalCellStyleStrategy
     */
    public static HorizontalCellStyleStrategy getStyleStrategy() {
        //内容样式策略
        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);
        //头策略使用默认
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
        return new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
    }

部分转载来源 (https://www.freesion.com/article/33511064066/)

你可能感兴趣的:(用EasyExcel将数据写入流、导出工具类,设置样式说明)