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. 对接 第三方物流APP 手机版

    昨天因为bibi项目要对接 物流信息 开始找了快递鸟文档,但是要填写申请APP,必须要注册公司才可以,这样非常麻烦.下面的第三方物理接口,绝对让你满意. https://m.kuaidi100.com ...

  2. 2018-2019-20175205 实验三敏捷开发与XP实践《Java开发环境的熟悉》实验报告

    2018-2019-20175205 实验三敏捷开发与XP实践<Java开发环境的熟悉>实验报告 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)>&l ...

  3. token鉴权的一种实现方式图解

  4. mysql时间相加函数DATE_ADD()

    mysql中的DATE_ADD()函数 1.定义:函数向日期添加指定的时间间隔. 2.语法:DATE_ADD(date,INTERVAL expr type) date 参数是合法的日期表达式.exp ...

  5. 【Diary】期中考+春游记

    4.22-4.24 期中考 周一 早上 赶来三楼电教厅自习,疯狂复习历史.想到下午考语文历史两大文科,不由得有点慌吧. 突然,沙雕体委说了一句: 下午考政治,你不背诵吗 下午 语文感觉正常发挥. 政治 ...

  6. 创建局域网yum服务器

    首先需要安装createrepo这个软件包,使用yum安装即可 创建软件库文件夹,比如mkdir -p /var/www/html/myrepo/x86_64, 将软件包拷贝到文件夹下. cd到该文件 ...

  7. 非常全的VsCode快捷键

    按 Press 功能 Function Ctrl + Shift + P,F1 显示命令面板 Show Command Palette Ctrl + P 快速打开 Quick Open Ctrl + ...

  8. GMap获取可视范围内四个角的坐标

    原理: 先获取控件的四个顶点,逐一将其转换成经纬度坐标. private void GetBonds() { //左上↖ PointLatLng pLeftTop = map1.FromLocalTo ...

  9. Java链接MySQL数据库的用配置文件和不用配置文件的代码

    1.利用配置文件(db.properties)链接MySQL数据库 package tool; import java.io.FileInputStream;import java.sql.Conne ...

  10. ip xfrm命令是做什么的?

    答: 设置xfrm.xfrm(transform configuration)是一个IP框架,用来转换数据包的格式,也就是使用算法来加密数据包,该框架用作IPsec协议的一部分 ip xfrm sta ...