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

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

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;

最后

现在正是金三银四的春招高潮,前阵子小编一直在搭建自己的网站,并整理了全套的**【一线互联网大厂Java核心面试题库+解析】:包括Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等**

image
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
EasyExcelData;

最后

现在正是金三银四的春招高潮,前阵子小编一直在搭建自己的网站,并整理了全套的**【一线互联网大厂Java核心面试题库+解析】:包括Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等**

[外链图片转存中…(img-MpSuJJH3-1714671554184)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值