SpringBoot 導(dǎo)出數(shù)據(jù)生成excel文件返回方式
一、基于框架
1.IDE
IntelliJ IDEA
2.軟件環(huán)境
Spring boot
mysql
mybatis
org.apache.poi
二、環(huán)境集成
1.創(chuàng)建spring boot項目工程
略過
2.maven引入poi
<!--數(shù)據(jù)導(dǎo)出依賴 excel--> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml-schemas --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.17</version> </dependency> <!--數(shù)據(jù)導(dǎo)出依賴 End excel-->
三、代碼實現(xiàn)
此處以導(dǎo)出云端mysql數(shù)據(jù)中的用戶表為例(數(shù)據(jù)為虛假數(shù)據(jù))
1.配置xls表格表頭
此處我創(chuàng)建一個class(ColumnTitleMap)來維護需要導(dǎo)出的mysql表和xls表頭顯示的關(guān)系
代碼注釋已經(jīng)清晰明了,就不再贅述
/**
* @desc:數(shù)據(jù)導(dǎo)出,生成excel文件時的列名稱集合
* @author: chao
* @time: 2018.6.11
*/
public class ColumnTitleMap {
private Map<String, String> columnTitleMap = new HashMap<String, String>();
private ArrayList<String> titleKeyList = new ArrayList<String> ();
public ColumnTitleMap(String datatype) {
switch (datatype) {
case "userinfo":
initUserInfoColu();
initUserInfoTitleKeyList();
break;
default:
break;
}
}
/**
* mysql用戶表需要導(dǎo)出字段--顯示名稱對應(yīng)集合
*/
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", "語音通知開關(guān)");
columnTitleMap.put("notice_email", "郵箱通知開關(guān)");
columnTitleMap.put("notice_sms", "短信通知開關(guān)");
columnTitleMap.put("notice_push", "應(yīng)用通知開關(guān)");
}
/**
* mysql用戶表需要導(dǎo)出字段集
*/
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<String, String> getColumnTitleMap() {
return columnTitleMap;
}
public ArrayList<String> 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:數(shù)據(jù)導(dǎo)出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: 導(dǎo)出數(shù)據(jù),生成xlsx文件
* @param response 返回對象
* @param date_start 篩選時間,開始(預(yù)留,查詢時并未做篩選數(shù)據(jù)處理)
* @param date_end 篩選時間,結(jié)束(預(yù)留,查詢時并未做篩選數(shù)據(jù)處理)
*/
@GetMapping(value = "/excel")
public void getUserInfoEx(
HttpServletResponse response,
@RequestParam String date_start,
@RequestParam String date_end
) {
try {
List<Map<String,Object>> userList = userInfoService.queryUserInfoResultListMap();
ArrayList<String> titleKeyList= new ColumnTitleMap("userinfo").getTitleKeyList();
Map<String, String> titleMap = new ColumnTitleMap("userinfo").getColumnTitleMap();
exportDataService.exportDataToEx(response, titleKeyList, titleMap, userList);
} catch (Exception e) {
//
System.out.println(e.toString());
}
}
}
3.service
(1).用戶表數(shù)據(jù)
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<Map<String,Object>> queryUserInfoResultListMap();
}
UserInfoMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.mcrazy.apios.mapper.UserInfoMapper"> <select id="queryUserInfoResultListMap" resultType="HashMap"> select * from user_info </select> </mapper>
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<Map<String,Object>> queryUserInfoResultListMap() {
List<Map<String,Object>> list = userInfoMapper.queryUserInfoResultListMap();
return list;
}
}
(2). 生成excel文件和導(dǎo)出
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:數(shù)據(jù)導(dǎo)出服務(wù)
* @author: chao
* @time: 2018.6.11
*/
@Service
public class ExportDataService {
@Autowired
ExportExcelUtil exportExcelUtil;
/*導(dǎo)出用戶數(shù)據(jù)表*/
public void exportDataToEx(HttpServletResponse response, ArrayList<String> titleKeyList, Map<String, String> titleMap, List<Map<String,Object>> src_list) {
try {
exportExcelUtil.expoerDataExcel(response, titleKeyList, titleMap, src_list);
} catch (Exception e) {
System.out.println("Exception: " + e.toString());
}
}
}
導(dǎo)出工具封裝,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:數(shù)據(jù)導(dǎo)出,生成excel文件
* @author: chao
* @time: 2018.6.12
*/
@Service
public class ExportExcelUtil {
public void expoerDataExcel(HttpServletResponse response, ArrayList<String> titleKeyList, Map<String, String> titleMap, List<Map<String,Object>> src_list) throws IOException {
String xlsFile_name = DateUtils.currtimeToString14() + ".xlsx"; //輸出xls文件名稱
//內(nèi)存中只創(chuàng)建100個對象
Workbook wb = new SXSSFWorkbook(100); //關(guān)鍵語句
Sheet sheet = null; //工作表對象
Row nRow = null; //行對象
Cell nCell = null; //列對象
int rowNo = 0; //總行號
int pageRowNo = 0; //頁行號
for (int k=0;k<src_list.size();k++) {
Map<String,Object> srcMap = src_list.get(k);
//寫入300000條后切換到下個工作表
if(rowNo%300000==0){
wb.createSheet("工作簿"+(rowNo/300000));//創(chuàng)建新的sheet對象
sheet = wb.getSheetAt(rowNo/300000); //動態(tài)指定當前的工作表
pageRowNo = 0; //新建了工作表,重置工作表的行號為0
// -----------定義表頭-----------
nRow = sheet.createRow(pageRowNo++);
// 列數(shù) titleKeyList.size()
for(int i=0;i<titleKeyList.size();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<titleKeyList.size();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();
}
}
三、運行
至此,所有代碼工作已經(jīng)做完,把程序運行起來,在瀏覽器調(diào)用接口,會自動下載到電腦中
瀏覽器打開:
http://192.168.1.70:8080/apios/exportdata/excel/?time_start=2018-12-19&end_start=2018-12-19
效果


得到xlsx文件,查看數(shù)據(jù)

以上這篇SpringBoot 導(dǎo)出數(shù)據(jù)生成excel文件返回方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- SpringBoot中EasyExcel實現(xiàn)Excel文件的導(dǎo)入導(dǎo)出
- SpringBoot?整合?EasyExcel?實現(xiàn)自由導(dǎo)入導(dǎo)出功能
- SpringBoot整合EasyExcel實現(xiàn)批量導(dǎo)入導(dǎo)出
- springboot實現(xiàn)excel表格導(dǎo)出幾種常見方法
- SpringBoot整合EasyExcel實現(xiàn)文件導(dǎo)入導(dǎo)出
- 使用VUE+SpringBoot+EasyExcel?整合導(dǎo)入導(dǎo)出數(shù)據(jù)的教程詳解
- SpringBoot+EasyPoi實現(xiàn)excel導(dǎo)出功能
- SpringBoot導(dǎo)出Excel的四種實現(xiàn)方式
- springboot實現(xiàn)對接poi 導(dǎo)出excel折線圖
相關(guān)文章
詳解SpringBoot注冊Windows服務(wù)和啟動報錯的原因
這篇文章主要介紹了詳解SpringBoot注冊Windows服務(wù)和啟動報錯的原因,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03

