java实现excel导出(列表导出)easyexcel

本文讲述的是从数据库查出数据并且导出excel。

1、所用依赖(easyexcel):


   com.alibaba
   easyexcel
   3.2.1

2、根据文件所需要字段定义一个实体类:

package com.biao.vo.excelVo;

import com.alibaba.excel.annotation.ExcelProperty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author xxx
 */
@Data
@ApiModel(value = "xxx导出对象", description = "xxx导出对象")
public class ExportInfoVo {

    @ApiModelProperty(value = "id")
    private String id;

    @ApiModelProperty(value = "项目名称")
    @ExcelProperty(value = "项目名称")
    private String projectName;

    @ApiModelProperty(value = "项目属性名称")
    @ExcelProperty(value = "项目属性名称")
    private String projectTypeName;

    ......

}

3、根据接口检索条件从数据库中查出数据:

List infoVos = this.baseMapper.selecInfo(req);
// 文件名
String fileName = "xxx_" + DateUtils.FormatDate(new Date(), DateUtils.DATE_FORMAT_YYYYMMDDHHMMSS);

4、导出为excel:

ExcelUtil.exportExcel(response, infoVos, ExportInfoVo.class, fileName);

5、公共方法: 

/**
 * 导出EXCEL
 *
 * @param list      List
 * @param head      Class
 * @param sheetName String
 * @param        
 */
public static  void exportExcel(HttpServletResponse response, List list, Class head, String sheetName) {

    try {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" +
                URLEncoder.encode(sheetName, "UTF-8").replace("\\+", "%20") + ".xlsx");
        /*设置样式*/
        // 头的策略
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            // 背景设置
            headWriteCellStyle.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());
            WriteFont headWriteFont = new WriteFont();
            headWriteFont.setFontHeightInPoints((short)11);
            headWriteCellStyle.setWriteFont(headWriteFont);
            // 内容的策略
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
            contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
            WriteFont contentWriteFont = new WriteFont();
            // 字体大小
            contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
            contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
            contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
            contentWriteCellStyle.setLeftBorderColor((short) 0);
            contentWriteCellStyle.setRightBorderColor((short) 0);
            contentWriteCellStyle.setBottomBorderColor((short) 0);

            contentWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE1.getIndex());
            contentWriteFont.setFontHeightInPoints((short)10);
            contentWriteCellStyle.setWriteFont(contentWriteFont);
            // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
            HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                    new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

            try (ExcelWriter excelWriter = EasyExcelFactory.write(response.getOutputStream(), head)
                    .excelType(ExcelTypeEnum.XLSX).build()) {
                Set excludeColumnFiledNames = new HashSet<>();
                excludeColumnFiledNames.add("id");
                WriteSheet writeSheet = EasyExcelFactory.writerSheet(0, sheetName)
                        .excludeColumnFieldNames(excludeColumnFiledNames)
                        .registerWriteHandler(horizontalCellStyleStrategy)
                        .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                        .build();
            excelWriter.write(list, writeSheet);
        }
    } catch (Exception e) {
        log.info("导出Excel失败 {}", e.getMessage());
        throw new RuntimeException();
    }

}

你可能感兴趣的:(excel,java,开发语言)