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

SpringBoot+Hutool+thymeleaf完成導(dǎo)出Excel的實(shí)現(xiàn)方法

 更新時(shí)間:2022年03月10日 15:40:48   作者:怪咖軟妹@  
這篇文章主要介紹了SpringBoot+Hutool+thymeleaf完成導(dǎo)出Excel,本篇示例當(dāng)中不僅僅有后端,而且還提供了前端html,html當(dāng)中利用js將后端 輸出流直接下載為文件,需要的朋友可以參考下

導(dǎo)出Excel的框架有很多種,POI相對(duì)來(lái)說(shuō)比較老了,很多Excel框架底層都是POI、有EasyPoi、EasyExcel、包括Hutool當(dāng)中封裝的也是POI。唯一不同的是Hutool工具包不局限與做Excel、他里面封裝了大量的util,一般現(xiàn)在開(kāi)發(fā)都會(huì)用到糊涂。

本篇示例當(dāng)中不僅僅有后端,而且還提供了前端html,html當(dāng)中利用js將后端 輸出流直接下載為文件。

實(shí)現(xiàn)的效果如下:一點(diǎn)擊導(dǎo)出文件直接下載到本地。應(yīng)該現(xiàn)在導(dǎo)出普遍都是這樣。

1、引入依賴

這里用到了lombok,就是簡(jiǎn)化實(shí)體類當(dāng)中的get、set方法的。

<dependency>
	<groupId>org.projectlombok</groupId>
	<artifactId>lombok</artifactId>
	<optional>true</optional>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 導(dǎo)出重要依賴|截止2022-03-09 hutool和poi-ooxml依賴是最新的 -->
<!--POI組件 設(shè)置字體樣式-->
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>5.2.1</version>
</dependency>
<dependency>
	<groupId>cn.hutool</groupId>
	<artifactId>hutool-all</artifactId>
	<version>5.7.22</version>
</dependency>

2、創(chuàng)建實(shí)體類

用于導(dǎo)出測(cè)試使用。

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class User {
    private String name;
    private Integer age;
    private String sex;

}

3、創(chuàng)建導(dǎo)出接口

導(dǎo)出主要是圍繞ExcelWriter類來(lái)進(jìn)行開(kāi)發(fā)的。

hutool在線api:https://apidoc.gitee.com/dromara/hutool/

我大概看了一眼,根據(jù)api其實(shí)任何復(fù)雜的Excel導(dǎo)出都可以做出來(lái),API可以設(shè)置指定單元格樣式字體、還有指定行的樣式字體、合并單元格等等…

在這里插入圖片描述

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.gzl.cn.hutoolexcel.domain.User;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;

@Controller
@RequestMapping("/test")
public class TestController {

    /**
     * 導(dǎo)出測(cè)試
     *
     * @param response
     * @param startDate
     * @param endDate
     */
    @PostMapping("/export")
    public void export2(HttpServletResponse response, String startDate, String endDate) {
        List<User> list = data();

        // 1.創(chuàng)建ExcelWriter
        // 通過(guò)工具類創(chuàng)建writer,默認(rèn)創(chuàng)建xls格式
        ExcelWriter excelWriter = ExcelUtil.getWriter();
        //創(chuàng)建xlsx格式的
        //ExcelWriter writer = ExcelUtil.getWriter(true);

        // 2.設(shè)置一級(jí)標(biāo)題
        // 合并單元格后的標(biāo)題行,使用默認(rèn)標(biāo)題樣式,從0開(kāi)始
        excelWriter.merge(2, "學(xué)生記錄");
        // 設(shè)置表頭高度
        excelWriter.setRowHeight(0, 25);

        // 3.設(shè)置二級(jí)標(biāo)題
        excelWriter.addHeaderAlias("name", "姓名");
        excelWriter.addHeaderAlias("age", "年齡");
        excelWriter.addHeaderAlias("sex", "性別");

        // 4.設(shè)置表頭字體
        // 獲取表頭樣式,獲取樣式后可自定義樣式
        CellStyle headCellStyle = excelWriter.getHeadCellStyle();
        // 獲取單元格樣式
//        CellStyle cellStyle = excelWriter.getCellStyle();
        // 設(shè)置內(nèi)容字體
        Font font = excelWriter.createFont();
        // 設(shè)置字體
        font.setFontName("宋體");
        // 設(shè)置字體大小
        font.setFontHeightInPoints((short) 14);
        // 字體加粗
        font.setBold(true);
        // 字體顏色
        font.setColor(Font.SS_NONE);
        headCellStyle.setFont(font);

        // 5.設(shè)置單元格寬度
        int[] arr = {30, 30, 25};
        for (int i = 0; i < arr.length; i++) {
            excelWriter.setColumnWidth(i, arr[i]);
        }

        // 只導(dǎo)出有別名的字段
        excelWriter.setOnlyAlias(true);
        // 一次性寫出內(nèi)容,使用默認(rèn)樣式,強(qiáng)制輸出標(biāo)題
        excelWriter.write(list, true);
        // 從第幾行寫入
//        excelWriter.setCurrentRow(1);
//        excelWriter.writeRow(data());
        // 設(shè)置某個(gè)單元格的樣式
//        CellStyle orCreateCellStyle = excelWriter.getOrCreateCellStyle(0, 1);
        // 設(shè)置某行的樣式
//        excelWriter.setRowStyle();

        try {
            String fileName = URLEncoder.encode("統(tǒng)計(jì)" + startDate + "日到" + endDate + "日", StandardCharsets.UTF_8.name());
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 將Excel Workbook刷出到輸出流
            excelWriter.flush(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("文件寫入失敗!");
        }
        //關(guān)閉流
        excelWriter.close();
    }

    /**
     * 構(gòu)造 導(dǎo)出的數(shù)據(jù)
     *
     * @return
     */
    public List<User> data() {
        List<User> users = new ArrayList<>();
        users.add(new User("張三", 2, "男"));
        users.add(new User("李四", 2, "女"));
        return users;
    }
}

4、創(chuàng)建html

在templates下創(chuàng)建index.html,這樣啟動(dòng)項(xiàng)目后直接訪問(wèn)8080端口,他會(huì)直接調(diào)到index.html。

在這里插入圖片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-3.5.1.min.js"></script>
</head>
<body>
<input type="button" id="clickme" value="導(dǎo)出"/>
</body>
<script type="text/javascript">
    function postExcelFile(params, url) { //params是post請(qǐng)求需要的參數(shù),url是請(qǐng)求url地址
        var form = document.createElement("form");
        form.style.display = 'none';
        form.action = url;
        form.method = "post";
        document.body.appendChild(form);

        for (var key in params) {
            var input = document.createElement("input");
            input.type = "hidden";
            input.name = key;
            input.value = params[key];
            form.appendChild(input);
        }

        form.submit();
        form.remove();
    }

    //點(diǎn)擊導(dǎo)出按鈕導(dǎo)出excel表格
    clickme.onclick = function () {
        var params = {};
        params.startDate = '2022-01-01';
        params.endDate = '2022-03-03';
        postExcelFile(params, "/test/export");
    }

</script>
</html>

5、測(cè)試導(dǎo)出

在這里插入圖片描述

到此這篇關(guān)于SpringBoot+Hutool+thymeleaf完成導(dǎo)出Excel的文章就介紹到這了,更多相關(guān)SpringBoot導(dǎo)出Excel內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 在CentOS系統(tǒng)中檢測(cè)Java安裝及運(yùn)行jar應(yīng)用的方法

    在CentOS系統(tǒng)中檢測(cè)Java安裝及運(yùn)行jar應(yīng)用的方法

    這篇文章主要介紹了在CentOS系統(tǒng)中檢測(cè)Java安裝及運(yùn)行jar應(yīng)用的方法,同樣適用于Fedora等其他RedHat系的Linux系統(tǒng),需要的朋友可以參考下
    2015-06-06
  • 基于Properties實(shí)現(xiàn)配置數(shù)據(jù)庫(kù)驅(qū)動(dòng)

    基于Properties實(shí)現(xiàn)配置數(shù)據(jù)庫(kù)驅(qū)動(dòng)

    這篇文章主要介紹了基于Properties實(shí)現(xiàn)配置數(shù)據(jù)庫(kù)驅(qū)動(dòng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05
  • java圖片驗(yàn)證碼生成教程詳解

    java圖片驗(yàn)證碼生成教程詳解

    這篇文章主要為大家詳細(xì)介紹了java圖片驗(yàn)證碼生成教程,從簡(jiǎn)單到復(fù)雜,從本地到前后臺(tái),感興趣的小伙伴們可以參考一下
    2016-07-07
  • IDEA正則表達(dá)式全局搜索圖文教程

    IDEA正則表達(dá)式全局搜索圖文教程

    當(dāng)您要搜索和替換特定的文本模式時(shí),請(qǐng)使用正則表達(dá)式,下面這篇文章主要給大家介紹了關(guān)于IDEA正則表達(dá)式全局搜索的相關(guān)資料,文中通過(guò)代碼以及圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-08-08
  • SpringMVC編程使用Controller接口實(shí)現(xiàn)控制器實(shí)例代碼

    SpringMVC編程使用Controller接口實(shí)現(xiàn)控制器實(shí)例代碼

    這篇文章主要介紹了SpringMVC編程使用Controller接口實(shí)現(xiàn)控制器實(shí)例代碼,具有一定參考價(jià)值,需要的朋友可以參考下。
    2017-11-11
  • SpringBoot后端驗(yàn)證碼的實(shí)現(xiàn)示例

    SpringBoot后端驗(yàn)證碼的實(shí)現(xiàn)示例

    為了防止網(wǎng)站的用戶被通過(guò)密碼典爆破,引入驗(yàn)證碼的功能是十分有必要的,本文主要介紹了SpringBoot后端驗(yàn)證碼的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • Java 守護(hù)線程_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java 守護(hù)線程_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java語(yǔ)言機(jī)制是構(gòu)建在JVM的基礎(chǔ)之上的,意思是Java平臺(tái)把操作系統(tǒng)的底層給屏蔽起來(lái),所以它可以在它自己的虛擬的平臺(tái)里面構(gòu)造出對(duì)自己有利的機(jī)制,而語(yǔ)言或者說(shuō)平臺(tái)的設(shè)計(jì)者多多少少是收到Unix思想的影響,而守護(hù)線程機(jī)制又是對(duì)JVM這樣的平臺(tái)湊合,于是守護(hù)線程應(yīng)運(yùn)而生
    2017-05-05
  • java實(shí)現(xiàn)將ftp和http的文件直接傳送到hdfs

    java實(shí)現(xiàn)將ftp和http的文件直接傳送到hdfs

    前面幾篇文章,我們已經(jīng)做了很好的鋪墊了,幾個(gè)要用到的工具我們都做了出來(lái),本文就是將他們集合起來(lái),說(shuō)下具體的用法,小伙伴們可以參考下。
    2015-03-03
  • MyBatis-Plus實(shí)現(xiàn)邏輯刪除的示例代碼

    MyBatis-Plus實(shí)現(xiàn)邏輯刪除的示例代碼

    本文主要介紹了MyBatis-Plus實(shí)現(xiàn)邏輯刪除的示例代碼,就是通過(guò)邏輯判斷的手段表示該條數(shù)據(jù)已刪除,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • 詳解關(guān)于spring bean名稱命名的那些事

    詳解關(guān)于spring bean名稱命名的那些事

    每個(gè)bean都有一個(gè)或者多個(gè)標(biāo)識(shí)符,這些標(biāo)識(shí)符在容器中必須是唯一的,這篇文章主要給大家介紹了關(guān)于spring bean名稱命名的那些事,需要的朋友可以參考下
    2021-07-07

最新評(píng)論