使用EasyPOI导出excel示例
package com.mtoliv.sps.controller; import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FilenameUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile; import com.mtoliv.sps.model.MapImportHanlder; import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.export.ExcelExportServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; @RestController
@RequestMapping(value = "/api/v1/excel")
@Api(tags = "导入导出相关操作参考实现 ")
public class ExcelController { private static final Logger logger = LoggerFactory.getLogger(ExcelController.class); @GetMapping(value = "/exportExcels", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
@ApiOperation(value = "导出数据")
public void exportExcels(HttpServletResponse response) throws IOException { List<ExcelExportEntity> entityList = new ArrayList<>();
entityList.add(new ExcelExportEntity("用户ID", "id", 15));
entityList.add(new ExcelExportEntity("用户名", "name", 15));
entityList.add(new ExcelExportEntity("用户年龄", "age", 15));
List<Map<String, String>> dataResult = getData(); ExcelExportServer server = new ExcelExportServer();
Workbook workbook = new HSSFWorkbook(); ExportParams exportParams = new ExportParams();
exportParams.setSheetName("用户列表");
server.createSheetForMap(workbook, exportParams, entityList, dataResult); response.setCharacterEncoding("UTF-8");
String filedisplay = "users.xls";
filedisplay = URLEncoder.encode(filedisplay, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay); OutputStream out = response.getOutputStream();
workbook.write(out);
out.close();
} @PostMapping(value = "/importUsers")
public void importUsers(@RequestPart(value = "file") MultipartFile file) { logger.info(file.getOriginalFilename()); String originalFilename = file.getOriginalFilename();
String extension = FilenameUtils.getExtension(originalFilename);
logger.info(extension);
if (!"xlsx".equals(extension) && !"xls".equals(extension)) return; ImportParams params = new ImportParams();
params.setDataHanlder(new MapImportHanlder());
try { List<Map<String, Object>> list = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, params);
logger.info(list.size() + "");
} catch (Exception e) { e.printStackTrace();
} } private List<Map<String, String>> getData() { List<Map<String, String>> dataResult = new ArrayList<>();
Map<String, String> u1 = new LinkedHashMap<>();
u1.put("id", "1");
u1.put("name", "cyf");
u1.put("age", "21");
Map<String, String> u2 = new LinkedHashMap<>();
u2.put("id", "2");
u2.put("name", "cy");
u2.put("age", "22");
dataResult.add(u1);
dataResult.add(u2);
return dataResult;
} }
注意:api部分:@GetMapping(value = "/api/v1/record/exportFireExcels", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE) 【转载:https://blog.csdn.net/zerobz/article/details/78962839】
使用EasyPOI导出excel示例的更多相关文章
- SpringBoot使用Easypoi导出excel示例
SpringBoot使用Easypoi导出excel示例 https://blog.csdn.net/justry_deng/article/details/84842111
- NPOI导出Excel示例
摘要:使用开源程序NPOI导出Excel示例.NPOI首页地址:http://npoi.codeplex.com/,NPOI示例博客:http://tonyqus.sinaapp.com/. 示例编写 ...
- EasyPoi导出Excel
这几天一直在忙工作中的事情,在工作中有一个问题,可能是因为刚开始接触这个EasyPoi,对其也没有太多的理解,在项目中就使用了,有一个需求,是要导出项目中所有的表格,今天就对这个需求进行分析和实现吧; ...
- 关于EasyPoi导出Excel
如果你觉得Easypoi不好用,喜欢用传统的poi,可以参考我的这篇博客:Springmvc导出Excel(maven) 当然了,万变不离其宗.Easypoi的底层原理还是poi.正如MyBatis ...
- 使用easypoi导出excel
EasyPOI是在jeecg的poi模块基础上,继续开发独立出来的,可以说是2.0版本,EasyPoi封装的目的和jeecg一致,争取让大家write less do more ,在这个思路上easy ...
- easyPOI导出excel报错
http-nio--exec- at :: - excel cell export error ,data is :com.jn.ssr.superrescue.web.qc.dto.Automati ...
- asp.net导出excel示例代码
asp.net导出excel的简单方法. excel的操作,最常用的就是导出和导入. 本例使用NPOI实现. 代码:/// <summary> ); ; ...
- EasyPoi 导出Excel(ExcelExportEntity生成表头)
[引入依赖] <!--easypoi--> <dependency> <groupId>cn.afterturn</groupId> <artif ...
- Vue+EasyPOI导出Excel(带图片)
一.前言 平时的工作中,Excel 导入导出功能是非常常见的功能,无论是前端 Vue (js-xlsx) 还是 后端 Java (POI),如果让大家手动编码实现的话,恐怕就很麻烦了,尤其是一些定制化 ...
随机推荐
- 微信扫描二维码安卓弹出默认浏览器(苹果打开App Store)打开下载链接
使用微信推广的用户经常都会遇到推广链接被拦截导致无法下载app的情况,此时用户在微信中打开会提示“ 已停止访问该网页 ”.这对于使用微信营销的商家来说就很不友好且损失非常大,因为用户是不知道为什么打不 ...
- Entity framework 绑定到Datagridview的添加删除修改
Entity framework 绑定到Datagridview的添加删除修改 using System; using System.Collections.Generic; using System ...
- linux重启Oracle服务
linux重启oracle服务命令(完整版) (1) 以oracle身份登录数据库,命令:su – oracle (2) 进入Sqlplus控制台,命令:sqlplus /nolog (3) 以系统管 ...
- C#通过Ado.net对连接数据库并进行添加删除等常规操作的代码
如下资料是关于C#通过Ado.net对连接数据库并进行添加删除等常规操作的内容. static string sqlcon = "server=.;database=;Integrated ...
- Git随笔 -- 初始化远程仓库
1. 新建文件夹(作为本地仓库与之远程仓库关联),进入文件夹空白处右键选择Git Bash(安装程序下载).[或者在开始菜单里找到Git Bash并打开,使用命令进入文件夹:cd 文件夹名称.] 2. ...
- 推荐!PlayGround:可视化神经网络
https://cloud.tencent.com/developer/news/190352 http://playground.tensorflow.org PlayGround是一个在线演示.实 ...
- bloc控制读写文件
import 'package:flutter/material.dart'; import 'dart:io'; import 'package:path_provider/path_provide ...
- Linux error numbers
Linux error numbers, straight from the horse's mouth. #define EPERM 1 /* Operation not permitted */ ...
- 对于SQL注入的理解
从网上搜索的资料,结合自己的理解整理了一下,网友们在查看时若有发现问题,还请不吝指正,谢谢! 1.什么是SQL注入? ——官方说法:把SQL命令插入到web表单验证的输入框中,提交到服务器,以达到越过 ...
- Linux 我的常用命令记录
Linux下复制粘贴快捷键 1. 在终端下: 复制命令:Ctrl + Shift + C 组合键. 粘贴命令:Ctrl + Shift + V 组合键. 2. 在控制台下: 复制命令:Ctrl + ...