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

SpringBoot實(shí)現(xiàn)導(dǎo)出復(fù)雜對(duì)象到Excel文件

 更新時(shí)間:2025年03月05日 15:51:22   作者:liangblog  
這篇文章主要為大家詳細(xì)介紹了如何使用Hutool和EasyExcel兩種方式來實(shí)現(xiàn)在Spring Boot項(xiàng)目中導(dǎo)出復(fù)雜對(duì)象到Excel文件,需要的小伙伴可以參考下

在Spring Boot項(xiàng)目中導(dǎo)出復(fù)雜對(duì)象到Excel文件,可以利用Hutool或EasyExcel等庫(kù)來簡(jiǎn)化操作。這里我們將詳細(xì)介紹如何使用Hutool和EasyExcel兩種方式來實(shí)現(xiàn)這一功能。

使用Hutool導(dǎo)出復(fù)雜對(duì)象到Excel

首先確保你的pom.xml中添加了Hutool的依賴:

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.10</version> <!-- 請(qǐng)根據(jù)實(shí)際情況選擇最新版本 -->
</dependency>

接下來是一個(gè)簡(jiǎn)單的示例,展示如何導(dǎo)出一個(gè)包含復(fù)雜對(duì)象的列表到Excel文件。

示例代碼

假設(shè)我們有一個(gè)User類,它包含一個(gè)嵌套的Address對(duì)象。

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
public class UserController {

    @GetMapping("/exportUsers")
    public void exportUsers(HttpServletResponse response) throws IOException {
        // 模擬獲取用戶數(shù)據(jù)
        List<User> users = getUsers();

        // 創(chuàng)建ExcelWriter實(shí)例
        ExcelWriter writer = ExcelUtil.getWriter(true); // true表示自動(dòng)創(chuàng)建表頭

        // 將復(fù)雜對(duì)象轉(zhuǎn)換為Map列表,方便寫入Excel
        List<Map<String, Object>> dataList = users.stream().map(user -> {
            Map<String, Object> row = new HashMap<>();
            row.put("ID", user.getId());
            row.put("姓名", user.getName());
            row.put("郵箱", user.getEmail());
            row.put("年齡", user.getAge());
            row.put("城市", user.getAddress().getCity());
            row.put("街道", user.getAddress().getStreet());
            return row;
        }).collect(Collectors.toList());

        // 寫入數(shù)據(jù)
        writer.write(dataList, true);

        // 設(shè)置響應(yīng)內(nèi)容類型和頭部信息
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        String fileName = URLEncoder.encode("用戶列表", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        // 將輸出流寫入response
        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();
    }

    private List<User> getUsers() {
        List<User> users = new ArrayList<>();
        Address address = new Address("北京", "中關(guān)村大街");
        users.add(new User(1L, "張三", "zhangsan@example.com", 28, address));
        return users;
    }
}

class User {
    private Long id;
    private String name;
    private String email;
    private Integer age;
    private Address address;

    public User(Long id, String name, String email, Integer age, Address address) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.age = age;
        this.address = address;
    }

    // getter和setter方法
}

class Address {
    private String city;
    private String street;

    public Address(String city, String street) {
        this.city = city;
        this.street = street;
    }

    // getter和setter方法
}

使用EasyExcel導(dǎo)出復(fù)雜對(duì)象到Excel

EasyExcel是阿里巴巴開源的一個(gè)非常高效的Excel處理庫(kù),特別適合處理大數(shù)據(jù)量的Excel文件。首先,在pom.xml中添加EasyExcel的依賴:

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.2.10</version> <!-- 請(qǐng)根據(jù)實(shí)際情況選擇最新版本 -->
</dependency>

接下來是一個(gè)使用EasyExcel導(dǎo)出復(fù)雜對(duì)象的例子。

示例代碼

假設(shè)我們?nèi)匀皇褂蒙厦嫣岬降?code>User和Address類。

import com.alibaba.excel.EasyExcel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api")
public class EasyExcelController {

    @GetMapping("/exportUsers")
    public void exportUsers(HttpServletResponse response) throws IOException {
        // 模擬獲取用戶數(shù)據(jù)
        List<User> users = getUsers();

        // 設(shè)置響應(yīng)內(nèi)容類型和頭部信息
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        String fileName = URLEncoder.encode("用戶列表", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        // 使用EasyExcel寫出數(shù)據(jù)到輸出流
        EasyExcel.write(response.getOutputStream(), UserData.class)
                .sheet("用戶信息")
                .doWrite(users);
    }

    private List<User> getUsers() {
        List<User> users = new ArrayList<>();
        Address address = new Address("北京", "中關(guān)村大街");
        users.add(new User(1L, "張三", "zhangsan@example.com", 28, address));
        return users;
    }
}

// 數(shù)據(jù)實(shí)體類
class UserData {
    @com.alibaba.excel.annotation.ExcelProperty("ID")
    private Long id;

    @com.alibaba.excel.annotation.ExcelProperty("姓名")
    private String name;

    @com.alibaba.excel.annotation.ExcelProperty("郵箱")
    private String email;

    @com.alibaba.excel.annotation.ExcelProperty("年齡")
    private Integer age;

    @com.alibaba.excel.annotation.ExcelProperty("城市")
    private String city;

    @com.alibaba.excel.annotation.ExcelProperty("街道")
    private String street;

    // 構(gòu)造函數(shù)、getter和setter方法
    public UserData(User user) {
        this.id = user.getId();
        this.name = user.getName();
        this.email = user.getEmail();
        this.age = user.getAge();
        this.city = user.getAddress().getCity();
        this.street = user.getAddress().getStreet();
    }

    // getter和setter方法
}

在這個(gè)例子中,我們定義了一個(gè)UserData類來映射User對(duì)象的數(shù)據(jù),并使用EasyExcel將這些數(shù)據(jù)寫入Excel文件。

通過上述方法,你可以輕松地在Spring Boot項(xiàng)目中導(dǎo)出復(fù)雜對(duì)象到Excel文件。無論是使用Hutool還是EasyExcel,都可以有效地簡(jiǎn)化Excel處理的工作。

以上就是SpringBoot實(shí)現(xiàn)導(dǎo)出復(fù)雜對(duì)象到Excel文件的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot導(dǎo)出復(fù)雜對(duì)象到Excel的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論

柳林县| 鄂伦春自治旗| 盐池县| 泽普县| 桃园县| 冕宁县| 宜章县| 巴里| 都昌县| 若尔盖县| 通榆县| 武强县| 新宁县| 保定市| 辉县市| 长岭县| 大洼县| 富源县| 怀仁县| 会泽县| 张掖市| 洛浦县| 嘉禾县| 岑溪市| 夹江县| 宜宾县| 沙洋县| 高唐县| 兴业县| 兰州市| 仙居县| 寿阳县| 修文县| 无锡市| 基隆市| 南部县| 东明县| 永寿县| 扎鲁特旗| 从化市| 罗源县|