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开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
![img](https://img-blog.csdnimg.cn/img_convert/c0307416e09faa7d510a2d093d0bafe8.jpeg)
最后
针对最近很多人都在面试,我这边也整理了相当多的面试专题资料,也有其他大厂的面经。希望可以帮助到大家。
最新整理面试题
上述的面试题答案都整理成文档笔记。也还整理了一些面试资料&最新2021收集的一些大厂的面试真题
最新整理电子书
最新整理大厂面试文档
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
…(img-wsxkZFE7-1713415654826)]
上述的面试题答案都整理成文档笔记。也还整理了一些面试资料&最新2021收集的一些大厂的面试真题
最新整理电子书
[外链图片转存中…(img-sax7o0U7-1713415654827)]
最新整理大厂面试文档
[外链图片转存中…(img-p4GVw7DY-1713415654827)]
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!