最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

前言

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

一、easypoi是什么?

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

        若poi都不知道的童鞋請(qǐng)自行百度。。。

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

二、使用步驟

1.傳送門

因?yàn)槲乙彩堑谝淮问褂茫@里還是先將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>

或者使用這個(gè):

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

2.1編寫實(shí)體類(我這里是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 = "編號(hào)")
    private int id;
    @Excel(name = "賬號(hào)")
    private String username;
    @Excel(name = "真實(shí)姓名")
    private String name;
    @Excel(name = "手機(jī)號(hào)")
    private String phone;
    @Excel(name = "郵箱")
    private String email;
    @Excel(name = "狀態(tài)",replace = {"啟用_true","禁用_false"})
    private boolean enabled;
}
 

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

關(guān)于圖片路徑

        著重說明一下這個(gè)圖片路徑,當(dāng) type 取值為 2 的時(shí)候表示導(dǎo)出為圖片,同時(shí)配合使用的是 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);
    }
 

        沒錯(cuò),就這么幾行代碼,當(dāng)然了,還要有個(gè)工具類,是我封裝好的,網(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對(duì)象
     */
    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 映射的實(shí)體類
     * @param fileName  導(dǎo)出的文件名(如果為null,則默認(rèn)文件名為當(dāng)前時(shí)間戳)
     * @param response  HttpServletResponse對(duì)象
     */
    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      映射的實(shí)體類
     * @param fileName       導(dǎo)出的文件名
     * @param isCreateHeader 是否創(chuàng)建表頭
     * @param response       HttpServletResponse對(duì)象
     */
    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    對(duì)應(yīng)的實(shí)體類
     * @param fileName     導(dǎo)出的文件名
     * @param response     HttpServletResponse對(duì)象
     * @param exportParams 導(dǎo)出參數(shù)實(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對(duì)象
     */
    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對(duì)象
     * @param response HttpServletResponse對(duì)象
     */
    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  映射的實(shí)體類
     * @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,并封裝成實(shí)體類
     *
     * @param file       上傳的文件
     * @param titleRows  表標(biāo)題的行數(shù)
     * @param headerRows 表頭行數(shù)
     * @param pojoClass  映射的實(shí)體類
     * @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實(shí)現(xiàn)簡單導(dǎo)出的文章就介紹到這了,更多相關(guān)springboot easypoi導(dǎo)出內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • RabbitMQ消息隊(duì)列之持久化機(jī)制詳解

    RabbitMQ消息隊(duì)列之持久化機(jī)制詳解

    這篇文章主要介紹了RabbitMQ消息隊(duì)列之持久化機(jī)制詳解,持久化,即將原本存在于內(nèi)存中的數(shù)據(jù)寫入到磁盤上永久保存數(shù)據(jù),防止服務(wù)宕機(jī)時(shí)內(nèi)存數(shù)據(jù)的丟失,Rabbitmq 的持久化分為隊(duì)列持久化、消息持久化和交換器持久化,需要的朋友可以參考下
    2023-08-08
  • Spring MVC如何使用@RequestParam注解獲取參數(shù)

    Spring MVC如何使用@RequestParam注解獲取參數(shù)

    這篇文章主要介紹了Spring MVC實(shí)現(xiàn)使用@RequestParam注解獲取參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Spring?@Conditional注解示例詳細(xì)講解

    Spring?@Conditional注解示例詳細(xì)講解

    @Conditional是Spring4新提供的注解,它的作用是按照一定的條件進(jìn)行判斷,滿足條件給容器注冊(cè)bean,這篇文章主要介紹了Spring?@Conditional注解示例詳細(xì)講解,需要的朋友可以參考下
    2022-11-11
  • JAVA如何獲取30天或某段范圍日期

    JAVA如何獲取30天或某段范圍日期

    JAVA獲取30天或某段范圍日期的方法,在項(xiàng)目使用中比較頻繁,通過示例代碼介紹了Java獲取當(dāng)前時(shí)間的上一年、下一年、上個(gè)月、下個(gè)月、前一天,當(dāng)天,本周,上周,本季度,上季度等(時(shí)間格式化),感興趣的朋友一起看看吧
    2023-10-10
  • Java中定時(shí)任務(wù)的6種實(shí)現(xiàn)方式

    Java中定時(shí)任務(wù)的6種實(shí)現(xiàn)方式

    這篇文章主要給大家分享的是Java中定時(shí)任務(wù)的6種實(shí)現(xiàn)方式,幾乎在所有的項(xiàng)目中,定時(shí)任務(wù)的使用都是不可或缺的,如果使用不當(dāng)甚至?xí)斐少Y損,下面文章我們就來看看Java中定時(shí)任務(wù)的具體使用方式吧
    2021-10-10
  • 解決mybatis使用char類型字段查詢oracle數(shù)據(jù)庫時(shí)結(jié)果返回null問題

    解決mybatis使用char類型字段查詢oracle數(shù)據(jù)庫時(shí)結(jié)果返回null問題

    這篇文章主要介紹了mybatis使用char類型字段查詢oracle數(shù)據(jù)庫時(shí)結(jié)果返回null問題的解決方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-06-06
  • 全面了解Java中對(duì)于異常的捕捉方法

    全面了解Java中對(duì)于異常的捕捉方法

    這篇文章主要全面介紹了Java中對(duì)于異常的捕捉方法,是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-11-11
  • 基于java實(shí)現(xiàn)租車管理系統(tǒng)

    基于java實(shí)現(xiàn)租車管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了基于java實(shí)現(xiàn)租車管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • java 如何判斷是否是26個(gè)英文字母

    java 如何判斷是否是26個(gè)英文字母

    這篇文章主要介紹了java 如何判斷是否是26個(gè)英文字母的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • Java簡單工廠和工廠方法模式詳細(xì)解析

    Java簡單工廠和工廠方法模式詳細(xì)解析

    這篇文章主要介紹了Java簡單工廠和工廠方法模式詳細(xì)解析,簡單工廠模式屬于類的創(chuàng)新型模式,又叫靜態(tài)工廠方法模式是通過專門定義一個(gè)類來負(fù)責(zé)創(chuàng)建其他類的實(shí)例,被創(chuàng)建的實(shí)例通常都具有共同的父類,需要的朋友可以參考下
    2023-12-12

最新評(píng)論

阳春市| 鲁甸县| 昌平区| 台东市| 大埔县| 鄂伦春自治旗| 革吉县| 上林县| 莒南县| 娱乐| 万盛区| 博客| 建德市| 蒙自县| 额济纳旗| 隆德县| 冕宁县| 荔波县| 图们市| 郸城县| 丹阳市| 武穴市| 海宁市| 呼和浩特市| 南平市| 邵阳市| 达州市| 靖安县| 公主岭市| 五指山市| 临洮县| 镇安县| 盐亭县| 兰溪市| 报价| 鹤山市| 韶关市| 麻城市| 镇宁| 阿克陶县| 景东|