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

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

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

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

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

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

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

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

示例代碼

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

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ù)雜對象轉(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ù)雜對象到Excel

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

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

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

示例代碼

假設(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對象的數(shù)據(jù),并使用EasyExcel將這些數(shù)據(jù)寫入Excel文件。

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

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

相關(guān)文章

最新評論

开化县| 麟游县| 阳信县| 北碚区| 仙居县| 弥勒县| 房山区| 武鸣县| 三都| 汉中市| 定州市| 宜都市| 大兴区| 涡阳县| 凌海市| 麻阳| 威信县| 门头沟区| 玉门市| 汶上县| 尖扎县| 临夏县| 肇州县| 诸暨市| 洛南县| 西乌珠穆沁旗| 玉山县| 黔东| 罗田县| 建宁县| 二连浩特市| 新邵县| 永定县| 宜阳县| 利津县| 宁夏| 淮北市| 平乐县| 武邑县| 开封县| 休宁县|