網(wǎng)站首頁 編程語言 正文
提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
文章目錄
- 前言
- 一、導入依賴
- 二、使用步驟
- 1.編寫工具類
- 2.傳入數(shù)據(jù)
- 總結
前言
最近有個需求需要導出Excel,要求使用POI,這里記錄一下
一、導入依賴
首先在項目中導入POI需要的依賴
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
二、使用步驟
1.編寫工具類
因為導出Excel在項目中的多處地方都有使用,所以僅僅在一個地方去編寫顯然不符合代碼復用的要求,私以為抽出一個工具類是最佳方案,工具類如下:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;
@Component
public class ExcelUtil {
/**
* 導出表格
*
* @param data
* @param filePath
*/
public void createExcel(List<List<String>> data, String filePath) {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet();
for (int i = 0; i < data.size(); i++) {
List<String> rowDate = data.get(i);
Row row = sheet.createRow(i);
for (int j = 0; j < rowDate.size(); j++) {
Cell cell = row.createCell(j);
cell.setCellValue(rowDate.get(j));
}
}
File fileExcel = new File(filePath);
fileExcel.deleteOnExit();
File folder = new File(filePath.substring(0, filePath.indexOf(File.separator)));
if (!folder.exists()) {
folder.mkdirs();
}
OutputStream os = null;
try {
os = new FileOutputStream(filePath);
workbook.write(os);
} catch (Exception e) {
e.printStackTrace();
}
}
}
工具類中編寫了一個導出Excel的方法,核心思想是:將需要導出的數(shù)據(jù)傳入,data是一個雙層List集合,里邊的每一個子集合都是需要導出的每一行的數(shù)據(jù),第一行是表頭,將集合中的每一行拿出創(chuàng)建excel的行,再輸出到工作簿中。而filePath就是我們需要輸出Excel的路徑,其實也可以將方法改造成流的形式,讓用戶自己決定去保存到某個位置。但是,,,,產(chǎn)品的需求是生成的excel要上傳到OSS云存儲,然后前端會拿到一個地址,然后再執(zhí)行下載,咱也不知道,就按照需求來吧。
2.傳入數(shù)據(jù)
這里需要我們自己在業(yè)務層去將需要導出的數(shù)據(jù)進行整合。具體看下邊代碼
//根據(jù)查詢條件查詢場景記錄
List<SceneRecord> sceneRecordList = baseMapper.selectList(queryWrapper);
List<List<String>> data = new ArrayList<>();
List<String> headerData = new ArrayList<>(sceneRecordList.size());
headerData.add("時間");
headerData.add("會員ID");
headerData.add("場景名稱");
headerData.add("減排量變化");
headerData.add("附帶信息");
data.add(headerData);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//查詢場景名稱
List<SceneRule> sceneRuleList = sceneRuleMapper.selectList(new QueryWrapper<>());
HashMap<String, String> map = new HashMap<>(sceneRuleList.size());
//遍歷場景規(guī)則
for (SceneRule rule : sceneRuleList) {
map.put(rule.getId(), rule.getName());
}
for (SceneRecord sceneRecord : sceneRecordList) {
List<String> bodyData = new ArrayList<>(sceneRecordList.size());
bodyData.add(simpleDateFormat.format(sceneRecord.getCreateTime()));
bodyData.add(sceneRecord.getMemberId());
//將對應的場景名稱添加
bodyData.add(map.get(sceneRecord.getSceneRuleId()));
bodyData.add(String.valueOf(sceneRecord.getEmission()));
bodyData.add(sceneRecord.getAdditional());
data.add(bodyData);
}
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = new Date();
String filename = "場景減排量記錄-" + simpleDateFormat1.format(date) + date.getTime() + ".xlsx";
excelUtil.createExcel(data, BasicFilePath.PATH + File.separator + filename);
String path = "";
try {
File file = new File(BasicFilePath.PATH + File.separator + filename);
path = ossUtil.upload(filename, new FileInputStream(file));
file.delete();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return ossUtil.getUrl(path);
其中部分業(yè)務代碼,沒有貼上去,應該可以看出個大概,主要操作就是先將header表頭數(shù)據(jù)存入,然后將需要導出的數(shù)據(jù)一行行的添加進去,場景名稱和場景規(guī)則這些是前端需要的數(shù)據(jù)處理,可以不用看,以上就是這次需求的解決辦法
總結
另外一種EasyExcel之前已經(jīng)記錄過詳情看這一篇博客
使用EasyExcel進行導入導出數(shù)據(jù)
原文鏈接:https://blog.csdn.net/l_zl2021/article/details/130319332
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2023-03-22 Python裝飾器使用方法全面梳理_python
- 2022-07-09 kubernetes之資源限制
- 2022-05-22 C#開發(fā)Winform實現(xiàn)窗體間相互傳值_C#教程
- 2023-04-01 SqlServer字符截取的具體函數(shù)使用_MsSql
- 2022-11-11 Android利用Canvas類繪制圖形_Android
- 2023-05-23 Python實現(xiàn)指定數(shù)組下標值正序與倒序排序算法功能舉例_python
- 2022-08-15 基于FTP協(xié)議的文件上傳與下載
- 2022-10-15 Tomcat生命周期詳解_Tomcat
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支