SpringBoot项目中集成EasyExcel实现Excel文件的下载

文章介绍了如何在SpringBoot应用中使用EasyExcel库处理Excel文件上传和下载,包括Controller类的API定义、实体类的使用以及与数据库服务的交互,展示了下载Excel数据并设置样式的过程。
摘要由CSDN通过智能技术生成

2、Controller 类

package com.riemann.springbootdemo.controller;

import com.alibaba.excel.EasyExcel;

import com.alibaba.excel.ExcelReader;

import com.alibaba.excel.read.metadata.ReadSheet;

import com.alibaba.excel.write.metadata.style.WriteCellStyle;

import com.alibaba.excel.write.metadata.style.WriteFont;

import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;

import com.riemann.springbootdemo.listener.UploadEasyExcelListener;

import com.riemann.springbootdemo.model.ReturnT;

import com.riemann.springbootdemo.model.UploadEasyExcelData;

import com.riemann.springbootdemo.service.UploadEasyExcelService;

import io.swagger.annotations.Api;

import io.swagger.annotations.ApiImplicitParam;

import io.swagger.annotations.ApiOperation;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpStatus;

import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

import java.io.Closeable;

import java.io.IOException;

import java.io.InputStream;

import java.net.URLEncoder;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.List;

/**

  • @author riemann

  • @date 2019/12/19 22:10

*/

@RestController

public class EasyExcelController {

private static final Logger LOGGER = LoggerFactory.getLogger(EasyExcelController.class);

@Autowired

private UploadEasyExcelService ueeService;

@GetMapping(value = “/downloadEasyExcel”,produces = “application/json;charset=UTF-8”)

public void downloadEasyExcel(HttpServletResponse response) throws IOException {

SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd-HH:mm:ss”);

String datetime = sdf.format(new Date());

String fileName = URLEncoder.encode(“下载excel服务”, “UTF-8”) + datetime;

response.setContentType(“application/vnd.ms-excel”);

response.setCharacterEncoding(“utf-8”);

response.setHeader(“Content-disposition”, “attachment;filename=” + fileName + “.xlsx”);

// excel头策略

WriteCellStyle headWriteCellStyle = new WriteCellStyle();

WriteFont headWriteFont = new WriteFont();

headWriteFont.setFontHeightInPoints((short) 11);

headWriteFont.setBold(false);

headWriteCellStyle.setWriteFont(headWriteFont);

// excel内容策略

WriteCellStyle contentWriteCellStyle = new WriteCellStyle();

WriteFont contentWriteFont = new WriteFont();

contentWriteFont.setFontHeightInPoints((short)11);

contentWriteCellStyle.setWriteFont(contentWriteFont);

// 设置handler

HorizontalCellStyleStrategy styleStrategy =

new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

List uploadEasyExcelData = ueeService.selectAll();

EasyExcel.write(response.getOutputStream(), UploadEasyExcelData.class)

.sheet(“下载excel服务”)

.registerWriteHandler(styleStrategy)

.doWrite(uploadEasyExcelData);

}

}

3、实体类 UploadEasyExcelData

package com.riemann.springbootdemo.model;

import com.alibaba.excel.annotation.ExcelProperty;

import lombok.Data;

import lombok.Getter;

import lombok.NoArgsConstructor;

import lombok.Setter;

/**

  • @author riemann

  • @date 2019/12/19 23:08

*/

@NoArgsConstructor

@Data

@Getter

@Setter

public class UploadEasyExcelData {

@ExcelProperty(value = “name”, index = 0)

private String name;

@ExcelProperty(value = “sex”, index = 1)

private String sex;

@ExcelProperty(value = “age”, index = 2)

private Integer age;

@ExcelProperty(value = “address”, index = 3)

private String address;

@ExcelProperty(value = “phone”, index = 4)

private String phone;

}

4、接口、实现类、Dao层

UploadEasyExcelService

package com.riemann.springbootdemo.service;

import com.riemann.springbootdemo.model.UploadEasyExcelData;

import java.util.List;

/**

  • @author riemann

  • @date 2019/12/19 23:31

*/

public interface UploadEasyExcelService {

/**

*查询所有

  • @return

*/

List selectAll();

}

UploadEasyExcelServiceImpl

package com.riemann.springbootdemo.service.impl;

import com.riemann.springbootdemo.dao.EasyExcelDao;

import com.riemann.springbootdemo.model.UploadEasyExcelData;
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

针对最近很多人都在面试,我这边也整理了相当多的面试专题资料,也有其他大厂的面经。希望可以帮助到大家。

最新整理面试题
在这里插入图片描述

上述的面试题答案都整理成文档笔记。也还整理了一些面试资料&最新2021收集的一些大厂的面试真题

最新整理电子书

在这里插入图片描述

最新整理大厂面试文档

在这里插入图片描述

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
…(img-wsxkZFE7-1713415654826)]

上述的面试题答案都整理成文档笔记。也还整理了一些面试资料&最新2021收集的一些大厂的面试真题

最新整理电子书

[外链图片转存中…(img-sax7o0U7-1713415654827)]

最新整理大厂面试文档

[外链图片转存中…(img-p4GVw7DY-1713415654827)]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值