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示例的更多相关文章

  1. SpringBoot使用Easypoi导出excel示例

    SpringBoot使用Easypoi导出excel示例 https://blog.csdn.net/justry_deng/article/details/84842111

  2. NPOI导出Excel示例

    摘要:使用开源程序NPOI导出Excel示例.NPOI首页地址:http://npoi.codeplex.com/,NPOI示例博客:http://tonyqus.sinaapp.com/. 示例编写 ...

  3. EasyPoi导出Excel

    这几天一直在忙工作中的事情,在工作中有一个问题,可能是因为刚开始接触这个EasyPoi,对其也没有太多的理解,在项目中就使用了,有一个需求,是要导出项目中所有的表格,今天就对这个需求进行分析和实现吧; ...

  4. 关于EasyPoi导出Excel

    如果你觉得Easypoi不好用,喜欢用传统的poi,可以参考我的这篇博客:Springmvc导出Excel(maven) 当然了,万变不离其宗.Easypoi的底层原理还是poi.正如MyBatis ...

  5. 使用easypoi导出excel

    EasyPOI是在jeecg的poi模块基础上,继续开发独立出来的,可以说是2.0版本,EasyPoi封装的目的和jeecg一致,争取让大家write less do more ,在这个思路上easy ...

  6. easyPOI导出excel报错

    http-nio--exec- at :: - excel cell export error ,data is :com.jn.ssr.superrescue.web.qc.dto.Automati ...

  7. asp.net导出excel示例代码

    asp.net导出excel的简单方法. excel的操作,最常用的就是导出和导入. 本例使用NPOI实现. 代码:/// <summary> );             ;       ...

  8. EasyPoi 导出Excel(ExcelExportEntity生成表头)

    [引入依赖] <!--easypoi--> <dependency> <groupId>cn.afterturn</groupId> <artif ...

  9. Vue+EasyPOI导出Excel(带图片)

    一.前言 平时的工作中,Excel 导入导出功能是非常常见的功能,无论是前端 Vue (js-xlsx) 还是 后端 Java (POI),如果让大家手动编码实现的话,恐怕就很麻烦了,尤其是一些定制化 ...

随机推荐

  1. java 字符串笔记

    java字符串当中有三个关于字符串对象的类. String 首先谈论下他们各自的含义: 1.String含义为引用数据类型,是字符串常量.是不可变的对象,(显然线程安全)在每次对string类型进行改 ...

  2. zigbee组网函数的一些用法

    1.NLME_PermitJoiningRequest(0)  :(1)值0x00:表示禁止加入网络  (2)值0x01-0xFE:表示允许链接的秒数 (3) 值0xff:表示启用网络 同时此函数:是 ...

  3. phpstudy-5.6.27-nts 安装redis扩展

    redis扩展安装流程 第一步: 首先直接查看一下phpinfo()的信息 找到下面两条信息 Architecture x86 PHP Extension Build API20131226,NTS, ...

  4. Vue-Router路由 Vue-CLI脚手架和模块化开发 之 使用路由对象获取参数

    使用路由对象$route获取参数: 1.params: 参数获取:使用$route.params获取参数: 参数传递: URL传参:例 <route-linke to : "/food ...

  5. python coroutine

    1. Python Async/Await入门指南 2. 用 Python 3 的 async / await 做异步编程 3.

  6. maven中引入oracle驱动报错Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0

    转自https://blog.csdn.net/lovesomnus/article/details/49512777 在maven中央库中查找ojdbc,发现最新的版本是10.2.0.4.0,然后在 ...

  7. VS2110。VC++编译错误"error LNK2005: 已经在 XXX.obj 中定义的问题"

    有时候我们会在头文件当中定义一些全局变量或者全局函数,这种做法会比较方便,但有时候会出现“编译错误"error LNK2005: 已经在 XXX.obj 中定义的问题"的链接问题. ...

  8. webdriver之富文本,Firefox配置加载

    chrome驱动下载:http://chromedriver.storage.googleapis.com/index.html 加载浏览器配置,需要用FirefoxProfile(profile_d ...

  9. ready

    // 定义一个动物类 function Animal (name) { // 属性 this.name = name || 'Animal'; // 实例方法 this.sleep = functio ...

  10. 【Contest Hunter 5302】金字塔

    [原题链接]传送门 [题解思路] 1.考虑如何将序列与树相对应,想到类似dfs序和欧拉序,同一个子树对应序列连续 2.暴力分子树过于复杂,考虑简化(划重点: 若当前区间为[l,r],考虑第一颗子树所在 ...