日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

使用POI導出Excel

作者:鑄鍵為犁 更新時間: 2023-07-25 編程語言

提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔

文章目錄

  • 前言
  • 一、導入依賴
  • 二、使用步驟
    • 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

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新