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. RSA加解密算法以及密钥格式

    RSA算法: 有个文章关于RSA原理讲的不错: https://blog.csdn.net/dbs1215/article/details/48953589 http://www.ruanyifeng ...

  2. oracle 两张关联表执行更新update

    UPDATE T_ASN_DTL ad1 SET ad1.cf03=( SELECT ac.TH003 FROM "T_ASN_DTL_copy" ac WHERE ac.udf0 ...

  3. flutter packages.

    connectivity This plugin allows Flutter apps to discover network connectivity and configure themselv ...

  4. ASUS RT-N16 使用OpenWrt 安装 ss记录

    本文用于记录一下使用ASUS RT-N16 使用OpenWrt 安装 shadowsocks的过程. 前后一共折腾了一个星期,原先使用的是tomato固件,但是在配置iptables的过程中,执行 r ...

  5. elasticsearch+logstash_jdbc 实现mysql数据实时同步至es

    jdk安装1.8版本,es.ls.ik.kibana版本一致我这里使用的6.6.2版本 安装es tar xf elasticsearch-6.6.2.tar.gz mv elasticsearch- ...

  6. 制作用户登录界面(JAVA实现)

    设计实现如图所示的个人信息注册.包含单选按钮.多选按钮.下拉框事件. Zuoye类: package example02; import java.awt.event.ActionEvent; imp ...

  7. vueRouter 子路由嵌套

    router.js import Vue from 'vue' import Router from 'vue-router' import contractPage from './views/co ...

  8. FL Studio中的音频设置

    在FL Studio中,有一步很关键的设置需要我们详细熟悉了解,它就是音频设置,什么是音频设置呢?它就是需要我们选择音频设备驱动程序并优化设置.在了解音频设备之前,我们先来看看什么是音频设备. 我们的 ...

  9. java如何编写多线程

    1.如何实现多线程 1.1实现Runnable接口,实现run()方法. public class Main4 implements Runnable { public static void mai ...

  10. 类的综合运用-complex的实现

    实验要求: 定义一个复数类Complex,使得下面的代码能够工作: Complex c1(3,5);     //用复数3+5i初始化c1: Compex c2=4.5;      //用实数4.5初 ...