Excel导出工具类-EasyExcelUtils

第一步:引入依赖-easyexcel

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>easyexcel</artifactId>
   <version>2.1.6</version>
</dependency>

第二步:编写工具类-EasyExcelUtils

public class EasyExcelUtils {

    public static void writeExcel(HttpServletResponse response, List<? extends Object> data, String fileName, String sheetName, Class clazz) throws Exception {
        //表头样式
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        //设置表头居中对齐
        headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        //内容样式
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        //设置内容
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
        HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
        EasyExcel.write(getOutputStream(fileName, response), clazz).excelType(ExcelTypeEnum.XLSX).sheet(sheetName).registerWriteHandler(horizontalCellStyleStrategy).doWrite(data);
    }
    private static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception {
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        return response.getOutputStream();
    }
}

第三步:新建导出列表映射的对象-ActAdvanceExport


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@HeadRowHeight(value = 20)
@ContentRowHeight(18)//内容行高
@ColumnWidth(20)//列宽,可设置成员变量上
public class ActAdvanceExport  {

    /**
     * 序号
     */
    @ExcelProperty(value = "序号",index = 0)
    private String  rowNum;

    /**
     * 预告编号
     */
    @ExcelProperty(value = "预告编号",index = 1)
    private String advanceNumber;

    /**
     * 预告类型
     */
    @ExcelProperty(value = "预告类型",index = 2)
    private String advanceType;
    /**
     * 证券代码
     */
    @ExcelProperty(value = "证券代码",index = 3)
    private String securityCode;
    /**
     * 预告备注
     */
    @ExcelProperty(value = "预告备注",index = 4)
    private String advanceRemark;
    /**
     * 预告状态
     */
    @ExcelProperty(value = "预告状态",index = 5)
    private String advanceStatus;
    

    @ExcelProperty(value = "修改人",index = 6)
    private String updateUser;

    @ExcelProperty(value = "修改日期",index = 7)
    private String updateDate;


}

第四步:获取列表中list

controller层 

    /**
     * 导出
     */
    @RequestMapping(value="/export",method = RequestMethod.POST)
    public void batchExport(HttpServletResponse response,ActAdvanceVo advanceVo) throws Exception {
        actAdvanceService.export(response,advanceVo);
    }

 service层

    void export(HttpServletResponse response, ActAdvanceVo advanceVo) throws Exception;

service实现层

@Override
public void export(HttpServletResponse response, ActAdvanceVo advanceVo) throws Exception {

        QueryWrapper<ActAdvanceVo> queryWrapper = new QueryWrapper<ActAdvanceVo>();
        List<ActAdvanceExport> list = actAdvanceDao.selecExportList(queryWrapper);
        String fileName = "abc列表";
        String sheetName = "def列表";
        EasyExcelUtils.writeExcel(response, list , fileName, sheetName, ActAdvanceExport.class);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值