开发基础
开发基础笔记
mybatis-plus通用的service方法
通过jenkins配置前后端自动打包及发布
mybatis-plus实现分页的方式
Java极客技术公众号PDF
狂神说SpringBoot
Hashids java 版使用
SpringBoot
1、Web快速开发
2、结果集的封装
3、集成MyBatis实现数据库操作
4、Springboot @Validated参数校验
5、SpringBoot全局异常处理
6、拦截器HandlerInterceptor
7、集成Swagger实现API自动生成
8、集成knife4j实现API自动生成
9、Springboot集成MyBatis-Plus快速入门
10、springboot自定义注解及AOP切面使用
11、使用Shiro实现登陆和权限认证,基于MyBatis
12、集成SpringSecurity实现授权认证
13、SpringBoot集成EasyExcel实现数据导入与导出
14、Spring Task定时任务的实现
15、Quartz快速上手与实践
16、如何用代码实现Spring IOC
17、SpringBoot集成JWT,实现接口的鉴权交互
SpringCloud
Nacos作为服务注册中心
seata1.6.1 结合springcloud实现分布锁的技术笔记
一些技术博客推荐
前端面试相关
看这一篇就够了
java.util包常用的类和接口
CountDownLatch介绍与使用
Common-lang3使用入门
Hutool简单使用入门
lombok 介绍及基本使用方法
git项目统计成员代码行数和提交的次数
mysql 逗号分隔的数据 like查询
使用sonar进行代码质量检查
线上使用jmeter进行压测的时候,使用Arthas诊断工具排查响应慢的接口
php结合phpstudy8、vscode开启xdebug进行代码调试
node-red使用入门
本文档使用 MrDoc 发布
-
+
首页
13、SpringBoot集成EasyExcel实现数据导入与导出
### EasyExcel简介: EasyExcel是一款阿里开源的Excel导入导出工具,具有处理快速、占用内存小、使用方便的特点,在Github上已有22k+收藏,可见其非常流行。 EasyExcel读取75M(46W行25列)的Excel,仅需使用64M内存,耗时20s,极速模式还可以更快! ### 使用场景说明  类似上面这样的一个博客的表格 第一个需求:导出的时候,在代码里准备了一个List<Blog>集合,把这些数据导出成xlsx文件,表头使用中文名。 第二个需求:把导出的表格 ,重新导回来,在控制台打印读取到的Excel数据。 ### 引入坐标: ```xml <!--EasyExcel相关依赖--> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.0.5</version> </dependency> ``` 因为使用到了 Swagger测试导入文件的上传。所以导入Swagger组件 。 ~~~xml <!-- swagger包这里2.9.2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> ~~~ 关于 Swagger部分的使用参照之前的章节。 ### 导出实现 在controller层添加以下代码: ~~~java /** * 导出数据 * @param response */ @ApiOperation(value = "导出博客列表Excel") @RequestMapping(value = "/exportList", method = RequestMethod.GET) public void exportList(HttpServletResponse response) throws IOException { setExcelRespProp(response, "博客"); List<Blog> blogList = new ArrayList<>(); for(int i=0;i<10;i++){ Blog blog=new Blog(); blog.setId(i); blog.setBlogTitle("标题"); blog.setBlogUrl("http://www.baidu.com/"); blog.setBlogAuthor("admin"); blogList.add(blog); } EasyExcel.write(response.getOutputStream()) .head(Blog.class) .excelType(ExcelTypeEnum.XLSX) .sheet("博客列表") .doWrite(blogList); } /** * 设置excel下载响应头属性 */ public void setExcelRespProp(HttpServletResponse response, String rawFileName) throws UnsupportedEncodingException { response.setContentType( "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode(rawFileName, "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); } ~~~ 启动服务 访问URL:http://localhost:8080/exportList ,会自动下载一个xlsx的表格文件。 ### 导入实现 代码如下: ~~~java /** * 导入 * @param file * @return */ @ApiOperation("从Excel导入列表") @RequestMapping(value = "/importList", method = RequestMethod.POST) @ResponseBody public void importList(@RequestPart("file") MultipartFile file) throws IOException { List<Blog> blogList = EasyExcel.read(file.getInputStream()) .head(Blog.class) .sheet() .doReadSync(); for (Blog blog : blogList){ System.out.println("blog.getBlogTitle() = " + blog.getBlogTitle()); } } ~~~ 导入后,会在 日志后台 把博客的标题依次打印出来。 启动服务,访问 http://localhost:8080/swagger-ui.html  提交后在控制台打印的日志: 
superadmin
2023年11月3日 17:20
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码