mysql预留字段easypoi_SpringBoot 导出数据生成excel文件返回方式

本文主要是介绍mysql预留字段easypoi_SpringBoot 导出数据生成excel文件返回方式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、基于框架

1.IDE

IntelliJ IDEA

2.软件环境

Spring boot

mysql

mybatis

org.apache.poi

二、环境集成

1.创建spring boot项目工程

略过

2.maven引入poi

org.apache.poi

poi

3.17

org.apache.poi

poi-ooxml

3.17

org.apache.poi

poi-ooxml-schemas

3.17

三、代码实现

此处以导出云端mysql数据中的用户表为例(数据为虚假数据)

1.配置xls表格表头

此处我创建一个class(ColumnTitleMap)来维护需要导出的mysql表和xls表头显示的关系

代码注释已经清晰明了,就不再赘述

/**

* @desc:数据导出,生成excel文件时的列名称集合

* @author: chao

* @time: 2018.6.11

*/

public class ColumnTitleMap {

private Map columnTitleMap = new HashMap();

private ArrayList titleKeyList = new ArrayList ();

public ColumnTitleMap(String datatype) {

switch (datatype) {

case "userinfo":

initUserInfoColu();

initUserInfoTitleKeyList();

break;

default:

break;

}

}

/**

* mysql用户表需要导出字段--显示名称对应集合

*/

private void initUserInfoColu() {

columnTitleMap.put("id", "ID");

columnTitleMap.put("date_create", "注册时间");

columnTitleMap.put("name", "名称");

columnTitleMap.put("mobile", "手机号");

columnTitleMap.put("email", "邮箱");

columnTitleMap.put("pw", "密码");

columnTitleMap.put("notice_voice", "语音通知开关");

columnTitleMap.put("notice_email", "邮箱通知开关");

columnTitleMap.put("notice_sms", "短信通知开关");

columnTitleMap.put("notice_push", "应用通知开关");

}

/**

* mysql用户表需要导出字段集

*/

private void initUserInfoTitleKeyList() {

titleKeyList.add("id");

titleKeyList.add("date_create");

titleKeyList.add("name");

titleKeyList.add("mobile");

titleKeyList.add("email");

titleKeyList.add("pw");

titleKeyList.add("notice_voice");

titleKeyList.add("notice_email");

titleKeyList.add("notice_sms");

titleKeyList.add("notice_push");

}

public Map getColumnTitleMap() {

return columnTitleMap;

}

public ArrayList getTitleKeyList() {

return titleKeyList;

}

}

2.controller

提供对外接口,ExportDataController.java

package com.mcrazy.apios.controller;

import com.mcrazy.apios.service.ExportDataService;

import com.mcrazy.apios.service.UserInfoService;

import com.mcrazy.apios.util.datebase.columntitle.ColumnTitleMap;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

/**

* @desc:数据导出api控制器

* @author: chao

* @time: 2018.6.11

*/

@Controller

@RequestMapping(value = "/exportdata")

public class ExportDataController {

@Autowired

UserInfoService userInfoService;

@Autowired

ExportDataService exportDataService;

/**

* @api: /apios/exportdata/excel/

* @method: GET

* @desc: 导出数据,生成xlsx文件

* @param response 返回对象

* @param date_start 筛选时间,开始(预留,查询时并未做筛选数据处理)

* @param date_end 筛选时间,结束(预留,查询时并未做筛选数据处理)

*/

@GetMapping(value = "/excel")

public void getUserInfoEx(

HttpServletResponse response,

@RequestParam String date_start,

@RequestParam String date_end

) {

try {

List> userList = userInfoService.queryUserInfoResultListMap();

ArrayList titleKeyList= new ColumnTitleMap("userinfo").getTitleKeyList();

Map titleMap = new ColumnTitleMap("userinfo").getColumnTitleMap();

exportDataService.exportDataToEx(response, titleKeyList, titleMap, userList);

} catch (Exception e) {

//

System.out.println(e.toString());

}

}

}

3.service

(1).用户表数据

UserInfoMapper.java

package com.mcrazy.apios.mapper;

import com.mcrazy.apios.model.UserInfo;

import org.apache.ibatis.annotations.Mapper;

import java.util.List;

import java.util.Map;

@Mapper

public interface UserInfoMapper {

/**

* @desc 查询所有用户信息

* @return 返回多个用户List

* */

List> queryUserInfoResultListMap();

}

UserInfoMapper.xml

select * from user_info

UserInfoService.java

package com.mcrazy.apios.service;

import com.mcrazy.apios.mapper.UserInfoMapper;

import com.mcrazy.apios.model.UserInfo;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

import java.util.Map;

@Service

public class UserInfoService {

@Autowired

UserInfoMapper userInfoMapper;

/**

* @desc 查询所有用户信息

* @return 返回多个用户List

* */

public List> queryUserInfoResultListMap() {

List> list = userInfoMapper.queryUserInfoResultListMap();

return list;

}

}

(2). 生成excel文件和导出

ExportDataService.java

package com.mcrazy.apios.service;

import com.mcrazy.apios.util.datebase.ExportExcelUtil;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

/**

* @desc:数据导出服务

* @author: chao

* @time: 2018.6.11

*/

@Service

public class ExportDataService {

@Autowired

ExportExcelUtil exportExcelUtil;

/*导出用户数据表*/

public void exportDataToEx(HttpServletResponse response, ArrayList titleKeyList, Map titleMap, List> src_list) {

try {

exportExcelUtil.expoerDataExcel(response, titleKeyList, titleMap, src_list);

} catch (Exception e) {

System.out.println("Exception: " + e.toString());

}

}

}

导出工具封装,ExportExcelUtil.java

package com.mcrazy.apios.util.datebase;

import com.mcrazy.apios.util.object.DateUtils;

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

/**

* @desc:数据导出,生成excel文件

* @author: chao

* @time: 2018.6.12

*/

@Service

public class ExportExcelUtil {

public void expoerDataExcel(HttpServletResponse response, ArrayList titleKeyList, Map titleMap, List> src_list) throws IOException {

String xlsFile_name = DateUtils.currtimeToString14() + ".xlsx"; //输出xls文件名称

//内存中只创建100个对象

Workbook wb = new SXSSFWorkbook(100); //关键语句

Sheet sheet = null; //工作表对象

Row nRow = null; //行对象

Cell nCell = null; //列对象

int rowNo = 0; //总行号

int pageRowNo = 0; //页行号

for (int k=0;k

Map srcMap = src_list.get(k);

//写入300000条后切换到下个工作表

if(rowNo%300000==0){

wb.createSheet("工作簿"+(rowNo/300000));//创建新的sheet对象

sheet = wb.getSheetAt(rowNo/300000); //动态指定当前的工作表

pageRowNo = 0; //新建了工作表,重置工作表的行号为0

// -----------定义表头-----------

nRow = sheet.createRow(pageRowNo++);

// 列数 titleKeyList.size()

for(int i=0;i

Cell cell_tem = nRow.createCell(i);

cell_tem.setCellValue(titleMap.get(titleKeyList.get(i)));

}

rowNo++;

// ---------------------------

}

rowNo++;

nRow = sheet.createRow(pageRowNo++); //新建行对象

// 行,获取cell值

for(int j=0;j

nCell = nRow.createCell(j);

if (srcMap.get(titleKeyList.get(j)) != null) {

nCell.setCellValue(srcMap.get(titleKeyList.get(j)).toString());

} else {

nCell.setCellValue("");

}

}

}

response.setContentType("application/vnd.ms-excel;charset=utf-8");

response.setHeader("Content-disposition", "attachment;filename=" + xlsFile_name);

response.flushBuffer();

OutputStream outputStream = response.getOutputStream();

wb.write(response.getOutputStream());

wb.close();

outputStream.flush();

outputStream.close();

}

}

三、运行

至此,所有代码工作已经做完,把程序运行起来,在浏览器调用接口,会自动下载到电脑中

浏览器打开:

http://192.168.1.70:8080/apios/exportdata/excel/?time_start=2018-12-19&end_start=2018-12-19

效果

3bc8aac8e492a7e711a043fea88967b7.png

6c6a740b7c2fdbc3c68d14ada7e658e2.png

得到xlsx文件,查看数据

32d8dc67d90825f8839b25404ee93120.png

以上这篇SpringBoot 导出数据生成excel文件返回方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

这篇关于mysql预留字段easypoi_SpringBoot 导出数据生成excel文件返回方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/weixin_28843015/article/details/113393803
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/223608

相关文章

Spring LDAP目录服务的使用示例

《SpringLDAP目录服务的使用示例》本文主要介绍了SpringLDAP目录服务的使用示例... 目录引言一、Spring LDAP基础二、LdapTemplate详解三、LDAP对象映射四、基本LDAP操作4.1 查询操作4.2 添加操作4.3 修改操作4.4 删除操作五、认证与授权六、高级特性与最佳

Spring Shell 命令行实现交互式Shell应用开发

《SpringShell命令行实现交互式Shell应用开发》本文主要介绍了SpringShell命令行实现交互式Shell应用开发,能够帮助开发者快速构建功能丰富的命令行应用程序,具有一定的参考价... 目录引言一、Spring Shell概述二、创建命令类三、命令参数处理四、命令分组与帮助系统五、自定义S

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

SpringSecurity JWT基于令牌的无状态认证实现

《SpringSecurityJWT基于令牌的无状态认证实现》SpringSecurity中实现基于JWT的无状态认证是一种常见的做法,本文就来介绍一下SpringSecurityJWT基于令牌的无... 目录引言一、JWT基本原理与结构二、Spring Security JWT依赖配置三、JWT令牌生成与

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL

如何配置Spring Boot中的Jackson序列化

《如何配置SpringBoot中的Jackson序列化》在开发基于SpringBoot的应用程序时,Jackson是默认的JSON序列化和反序列化工具,本文将详细介绍如何在SpringBoot中配置... 目录配置Spring Boot中的Jackson序列化1. 为什么需要自定义Jackson配置?2.

Java中使用Hutool进行AES加密解密的方法举例

《Java中使用Hutool进行AES加密解密的方法举例》AES是一种对称加密,所谓对称加密就是加密与解密使用的秘钥是一个,下面:本文主要介绍Java中使用Hutool进行AES加密解密的相关资料... 目录前言一、Hutool简介与引入1.1 Hutool简介1.2 引入Hutool二、AES加密解密基础

MySQL中FIND_IN_SET函数与INSTR函数用法解析

《MySQL中FIND_IN_SET函数与INSTR函数用法解析》:本文主要介绍MySQL中FIND_IN_SET函数与INSTR函数用法解析,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一... 目录一、功能定义与语法1、FIND_IN_SET函数2、INSTR函数二、本质区别对比三、实际场景案例分

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用