欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot利用easypoi實現(xiàn)簡單導(dǎo)出功能

 更新時間:2024年12月03日 10:25:22   作者:小林想被監(jiān)督學(xué)習(xí)  
本文介紹了如何使用Spring Boot和EasyPoi庫實現(xiàn)Excel文件的導(dǎo)出功能,EasyPoi是一個簡化Excel和Word操作的工具,通過簡單的配置和代碼,可以輕松地將Java對象導(dǎo)出為Excel文件,并且支持圖片導(dǎo)出等功能,感興趣的朋友一起看看吧

前言

        今天玩了一下springboot利用easypoi實現(xiàn)excel的導(dǎo)出,以前沒玩過導(dǎo)入導(dǎo)出,只不過聽說過看別人用過,怎么說呢,想玩就玩一下吧,畢竟結(jié)合自己業(yè)務(wù)場景需要才會考慮是否使用。先簡單介紹一下easypoi。

一、easypoi是什么?

1.不太熟悉poi的
2.不想寫太多重復(fù)太多的
3.只是簡單的導(dǎo)入導(dǎo)出的
4.喜歡使用模板的

        若poi都不知道的童鞋請自行百度。。。

        Easypoi的目標(biāo)不是替代poi,而是讓一個不懂導(dǎo)入導(dǎo)出的快速使用poi完成Excel和word的各種操作,而不是看很多api才可以完成這樣工作。

二、使用步驟

1.傳送門

因為我也是第一次使用,這里還是先將easypoi的文檔放這兒吧:

結(jié)尾

2.后端springboot

首先引入easypoi所需依賴:

       <!--easypoi導(dǎo)入導(dǎo)出-->
		<dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-base</artifactId>
			<version>4.1.3</version>
		</dependency>
		<dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-web</artifactId>
			<version>4.1.3</version>
		</dependency>
		<dependency>
			<groupId>cn.afterturn</groupId>
			<artifactId>easypoi-annotation</artifactId>
			<version>4.1.3</version>
		</dependency>

或者使用這個:

<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-spring-boot-starter</artifactId>
    <version>4.0.0</version>
</dependency>

2.1編寫實體類(我這里是dto,也一樣)

package top.wangxingjun.separate.dto;
import cn.afterturn.easypoi.excel.annotation.Excel;
import top.wangxingjun.separate.dto.base.OutputConverter;
import top.wangxingjun.separate.entity.AdminRole;
import top.wangxingjun.separate.entity.User;
import lombok.Data;
import lombok.ToString;
import java.util.List;
/**
 * @author wxj
 * @Date 2020/8/10
 */
@Data
@ToString
public class UserDTO implements OutputConverter<UserDTO, User> {
    @Excel(name = "編號")
    private int id;
    @Excel(name = "賬號")
    private String username;
    @Excel(name = "真實姓名")
    private String name;
    @Excel(name = "手機(jī)號")
    private String phone;
    @Excel(name = "郵箱")
    private String email;
    @Excel(name = "狀態(tài)",replace = {"啟用_true","禁用_false"})
    private boolean enabled;
}
 

        簡單看一下這個 @Excel 注解主要的值:

關(guān)于圖片路徑

        著重說明一下這個圖片路徑,當(dāng) type 取值為 2 的時候表示導(dǎo)出為圖片,同時配合使用的是 imageType 參數(shù),該參數(shù)決定是從 file 讀取,還是去數(shù)據(jù)庫讀取,默認(rèn)為從 file 中讀取,記得很早之前,有小伙伴圖片是直接 base64 存數(shù)據(jù)庫的,不過現(xiàn)在是沒有人干這種事了

2.2控制層

直接看代碼:

    /**
     * 用戶信息導(dǎo)出
     */
    @GetMapping("api/exportUser")
    public void exportUser(HttpServletResponse response) throws Exception {
        List<UserDTO> list = userService.list();
        ExcelUtil.exportExcel(list, null, "sheet1", UserDTO.class, "用戶信息", response);
    }
 

        沒錯,就這么幾行代碼,當(dāng)然了,還要有個工具類,是我封裝好的,網(wǎng)上也有很多的咯。下面看工具類:

 package top.wangxingjun.separate.util;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
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.enmus.ExcelType;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
/**
 * @ProjectName: separate
 * @Package: top.wangxingjun.separate.util
 * @ClassName: ExcelUtil
 * @Author: wxj
 * @Description: Excel導(dǎo)入導(dǎo)出工具類
 * @Date: 2020/8/25 10:07
 * @Version: 1.0
 */
@Log4j2
public class ExcelUtil {
    /**
     * Map集合導(dǎo)出
     *
     * @param list     需要導(dǎo)出的數(shù)據(jù)
     * @param fileName 導(dǎo)出的文件名
     * @param response HttpServletResponse對象
     */
    public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws Exception{
        defaultExport(list, fileName, response);
    }
    /**
     * 復(fù)雜導(dǎo)出Excel,包括文件名以及表名(不創(chuàng)建表頭)
     *
     * @param list      需要導(dǎo)出的數(shù)據(jù)
     * @param title     表格首行標(biāo)題(不需要就傳null)
     * @param sheetName 工作表名稱
     * @param pojoClass 映射的實體類
     * @param fileName  導(dǎo)出的文件名(如果為null,則默認(rèn)文件名為當(dāng)前時間戳)
     * @param response  HttpServletResponse對象
     */
    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
                                   HttpServletResponse response) throws Exception{
        defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
    }
    /**
     * 復(fù)雜導(dǎo)出Excel,包括文件名以及表名(創(chuàng)建表頭)
     *
     * @param list           需要導(dǎo)出的數(shù)據(jù)
     * @param title          表格首行標(biāo)題(不需要就傳null)
     * @param sheetName      工作表名稱
     * @param pojoClass      映射的實體類
     * @param fileName       導(dǎo)出的文件名
     * @param isCreateHeader 是否創(chuàng)建表頭
     * @param response       HttpServletResponse對象
     */
    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName,
                                   boolean isCreateHeader, HttpServletResponse response) throws Exception{
        ExportParams exportParams = new ExportParams(title, sheetName);
        exportParams.setCreateHeadRows(isCreateHeader);
        defaultExport(list, pojoClass, fileName, response, exportParams);
    }
    /**
     * 默認(rèn)導(dǎo)出方法
     *
     * @param list         需要導(dǎo)出的數(shù)據(jù)
     * @param pojoClass    對應(yīng)的實體類
     * @param fileName     導(dǎo)出的文件名
     * @param response     HttpServletResponse對象
     * @param exportParams 導(dǎo)出參數(shù)實體
     */
    private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response,
                                      ExportParams exportParams) throws Exception{
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        downloadExcel(fileName, workbook, response);
    }
    /**
     * 默認(rèn)導(dǎo)出方法
     *
     * @param list     Map集合
     * @param fileName 導(dǎo)出的文件名
     * @param response HttpServletResponse對象
     */
    private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response)throws Exception {
        Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
        if (null != workbook) {
            downloadExcel(fileName, workbook, response);
        }
    }
    /**
     * Excel導(dǎo)出
     *
     * @param fileName Excel導(dǎo)出
     * @param workbook Excel對象
     * @param response HttpServletResponse對象
     */
    public static void downloadExcel(String fileName, Workbook workbook, HttpServletResponse response) throws Exception{
        try {
            if (StringUtils.isEmpty(fileName)) {
                throw new RuntimeException("導(dǎo)出文件名不能為空");
            }
            String encodeFileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("content-Type", "application/vnd.ms-excel; charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + encodeFileName);
            response.setHeader("FileName", encodeFileName);
            response.setHeader("Access-Control-Expose-Headers", "FileName");
            workbook.write(response.getOutputStream());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
    /**
     * 根據(jù)文件路徑來導(dǎo)入Excel
     *
     * @param filePath   文件路徑
     * @param titleRows  表標(biāo)題的行數(shù)
     * @param headerRows 表頭行數(shù)
     * @param pojoClass  映射的實體類
     * @return
     */
    public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception{
        //判斷文件是否存在
        if (StringUtils.isBlank(filePath)) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
        } catch (NoSuchElementException e) {
            log.error("模板不能為空", e);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return list;
    }
    /**
     * 根據(jù)接收的Excel文件來導(dǎo)入Excel,并封裝成實體類
     *
     * @param file       上傳的文件
     * @param titleRows  表標(biāo)題的行數(shù)
     * @param headerRows 表頭行數(shù)
     * @param pojoClass  映射的實體類
     * @return
     */
    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws Exception{
        if (file == null) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        } catch (NoSuchElementException e) {
            log.error("excel文件不能為空", e);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return list;
    }
    /**
     * 文件轉(zhuǎn)List
     *
     * @param file
     * @param pojoClass
     * @param <T>
     * @return
     */
    public static <T> List<T> fileToList(MultipartFile file, Class<T> pojoClass) throws Exception{
        if (file.isEmpty()) {
            throw new RuntimeException("文件為空");
        }
        List<T> list = ExcelUtil.importExcel(file, 1, 1, pojoClass);
        if (CollectionUtils.isEmpty(list)) {
            throw new RuntimeException("未解析到表格數(shù)據(jù)");
        }
        return list;
    }
}

excel導(dǎo)出所需要的都準(zhǔn)備好了,下面來看一下我的效果:

到此這篇關(guān)于springboot利用easypoi實現(xiàn)簡單導(dǎo)出的文章就介紹到這了,更多相關(guān)springboot easypoi導(dǎo)出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java遍歷Map鍵、值和獲取Map大小的方法示例

    Java遍歷Map鍵、值和獲取Map大小的方法示例

    本篇文章主要介紹了Java遍歷Map鍵、值和獲取Map大小的方法示例,詳細(xì)的介紹了Java遍歷Map的兩種實現(xiàn)方法和大小,具有一定的參考價值,有興趣的可以了解一下。
    2017-01-01
  • 精通Java泛型的使用與原理

    精通Java泛型的使用與原理

    針對利用繼承來實現(xiàn)通用程序設(shè)計所產(chǎn)生的問題,泛型提供了更好的解決方案,本文詳細(xì)的介紹了Java泛型的使用與原理,感興趣的可以了解一下
    2022-03-03
  • Java基于MySQL實現(xiàn)學(xué)生管理系統(tǒng)

    Java基于MySQL實現(xiàn)學(xué)生管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java基于MySQL實現(xiàn)學(xué)生管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • IntelliJ?IDEA?2022.2最新版本激活教程(親測可用版)永久激活工具分享

    IntelliJ?IDEA?2022.2最新版本激活教程(親測可用版)永久激活工具分享

    Jetbrains官方發(fā)布了?IntelliJ?IDEA2022.2?正式版,每次大的版本更新,都會有較大的調(diào)整和優(yōu)化,除本次更新全面擁抱?Java?17?外,還有對IDE?UI界面,安全性,便捷性等都做了調(diào)整和優(yōu)化完善,用戶體驗提升不少,相信后面會有不少小伙伴跟著更新
    2022-08-08
  • Windows同時配置兩個jdk環(huán)境變量的操作步驟

    Windows同時配置兩個jdk環(huán)境變量的操作步驟

    Java Development Kit (JDK) 是開發(fā)Java應(yīng)用程序的基礎(chǔ),包含了編譯器、調(diào)試器以及其他必要的工具,本指南將一步步指導(dǎo)您完成在Windows操作系統(tǒng)上同時配置兩個jdk環(huán)境變量的操作步驟,需要的朋友可以參考下
    2024-09-09
  • Eclipse中創(chuàng)建Web項目最新方法(2023年)

    Eclipse中創(chuàng)建Web項目最新方法(2023年)

    在Java開發(fā)人員中,最常用的開發(fā)工具應(yīng)該就是Eclipse,下面這篇文章主要給大家介紹了關(guān)于Eclipse中創(chuàng)建Web項目2023年最新的方法,需要的朋友可以參考下
    2023-09-09
  • Java中數(shù)組的定義和使用教程(一)

    Java中數(shù)組的定義和使用教程(一)

    這篇文章主要給大家介紹了關(guān)于Java中數(shù)組的定義和使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java中將bean放入Spring容器中的幾種方式詳解

    Java中將bean放入Spring容器中的幾種方式詳解

    這篇文章主要介紹了Java中將bean放入Spring容器中的幾種方式詳解,在Spring框架中,有多種方式可以將Bean(即對象)放入Spring容器中,今天我們就來詳細(xì)說一下這幾種方式,需要的朋友可以參考下
    2023-07-07
  • Jsoup解析HTML實例及文檔方法詳解

    Jsoup解析HTML實例及文檔方法詳解

    這篇文章主要介紹了Jsoup如何解析一個HTML文檔、從文件加載文檔、從URL加載Document等方法,對Jsoup常用方法做了詳細(xì)講解,最近提供了一個示例供大家參考 使用DOM方法來遍歷一個文檔 從元素抽取屬性,文本和HTML 獲取所有鏈接
    2013-11-11
  • Java中常用修飾符的使用方法匯總

    Java中常用修飾符的使用方法匯總

    下面小編就為大家?guī)硪黄狫ava中常用修飾符的使用方法匯總。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-01-01

最新評論