App下載

無(wú)需插件!使用Spring Boot輕松導(dǎo)出數(shù)據(jù)到Excel

神仙女孩破破 2023-12-10 16:28:11 瀏覽數(shù) (2423)
反饋

導(dǎo)出數(shù)據(jù)為Excel是Web應(yīng)用中常見(jiàn)的需求之一,但往往需要依賴(lài)插件或外部工具。然而,Spring Boot作為一個(gè)快速開(kāi)發(fā)框架,提供了豐富的功能和庫(kù),使得將數(shù)據(jù)導(dǎo)出為Excel變得簡(jiǎn)單而高效,無(wú)需任何額外的插件或工具。本文將介紹如何利用Spring Boot的強(qiáng)大功能,以簡(jiǎn)單而高效的方式將數(shù)據(jù)導(dǎo)出為Excel文件。

20231207-161814

1. 添加依賴(lài) 

首先,我們需要在Spring Boot項(xiàng)目中引入所需的依賴(lài)。在項(xiàng)目的?pom.xml?文件中添加以下依賴(lài):

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.1.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.1.0</version>
</dependency>

這些依賴(lài)項(xiàng)將使我們能夠使用Apache POI庫(kù)來(lái)處理Excel文件。

2. 創(chuàng)建數(shù)據(jù)源 

在開(kāi)始導(dǎo)出數(shù)據(jù)之前,我們需要準(zhǔn)備一個(gè)數(shù)據(jù)源。您可以使用Spring的數(shù)據(jù)訪(fǎng)問(wèn)技術(shù)(如JPA、Hibernate或MyBatis)從數(shù)據(jù)庫(kù)中獲取數(shù)據(jù),或者直接構(gòu)造一個(gè)數(shù)據(jù)集合。例如,假設(shè)我們有一個(gè)?UserService?類(lèi),它可以從數(shù)據(jù)庫(kù)中獲取用戶(hù)數(shù)據(jù):

@Service
public class UserService {
    public List<User> getAllUsers() {
        // 從數(shù)據(jù)庫(kù)獲取用戶(hù)數(shù)據(jù)的邏輯
    }
}

3. 創(chuàng)建Excel文檔 

接下來(lái),使用Apache POI庫(kù)創(chuàng)建一個(gè)Excel文檔,并將數(shù)據(jù)填充到其中。

// 創(chuàng)建工作簿
Workbook workbook = new XSSFWorkbook(); 
// 創(chuàng)建工作表
Sheet sheet = workbook.createSheet("User Data"); 

// 創(chuàng)建表頭
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("ID");
headerRow.createCell(1).setCellValue("姓名");
headerRow.createCell(2).setCellValue("年齡");

// 填充數(shù)據(jù)
List<User> users = userService.getAllUsers();
int rowNum = 1;
for (User user : users) {
    Row row = sheet.createRow(rowNum++);
    row.createCell(0).setCellValue(user.getId());
    row.createCell(1).setCellValue(user.getName());
    row.createCell(2).setCellValue(user.getAge());
}

在上述示例中,我們創(chuàng)建了一個(gè)名為"User Data"的工作表,并添加了表頭和數(shù)據(jù)行。我們假設(shè)`User`類(lèi)具有?id?、?name?和?age?屬性,并且?userService?是一個(gè)用于獲取用戶(hù)數(shù)據(jù)的服務(wù)類(lèi)。

4. 導(dǎo)出Excel文件 

使用Spring Boot的?@RestController?注解和?@GetMapping?注解,創(chuàng)建一個(gè)處理導(dǎo)出請(qǐng)求的接口。在該接口中,將Excel文檔寫(xiě)入響應(yīng)流,以實(shí)現(xiàn)導(dǎo)出。

@RestController
public class ExcelController {
    @Autowired
    private UserService userService;

    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) throws IOException {
        List<User> users = userService.getAllUsers();

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("User Data");

        // 創(chuàng)建表頭和填充數(shù)據(jù)...
        // 設(shè)置響應(yīng)頭
        response.setHeader("Content-Disposition", "attachment; filename=users.xlsx");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        // 寫(xiě)入響應(yīng)流
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        workbook.close();
        outputStream.close();
    }
}

在上述示例中,我們使用了?@RestController?注解和?@GetMapping?注解來(lái)定義了一個(gè)處理GET請(qǐng)求的接口。該接口的路徑為?/export?,用戶(hù)訪(fǎng)問(wèn)該路徑將觸發(fā)數(shù)據(jù)導(dǎo)出為Excel的過(guò)程。在?exportExcel?方法中,我們獲取用戶(hù)數(shù)據(jù)并創(chuàng)建了一個(gè)Excel工作簿,并填充數(shù)據(jù)。然后,我們?cè)O(shè)置了響應(yīng)頭,指定要下載的文件名為"users.xlsx",并將響應(yīng)的內(nèi)容類(lèi)型設(shè)置為Excel文件的MIME類(lèi)型。最后,我們將工作簿寫(xiě)入響應(yīng)流,并關(guān)閉流。

總結(jié)

本文介紹了如何使用Spring Boot框架將數(shù)據(jù)導(dǎo)出為Excel文件的簡(jiǎn)單而高效的方法。通過(guò)使用Apache POI庫(kù),我們能夠輕松地創(chuàng)建Excel文檔并填充數(shù)據(jù)。結(jié)合Spring Boot的特性,我們可以將Excel文件導(dǎo)出為響應(yīng)流,使用戶(hù)能夠方便地下載該文件。希望本文能夠幫助您實(shí)現(xiàn)數(shù)據(jù)導(dǎo)出為Excel的功能,并提升您的應(yīng)用程序的實(shí)用性和用戶(hù)體驗(yàn)。

1698630578111788

如果你對(duì)編程知識(shí)和相關(guān)職業(yè)感興趣,歡迎訪(fǎng)問(wèn)編程獅官網(wǎng)(http://www.o2fo.com/)。在編程獅,我們提供廣泛的技術(shù)教程、文章和資源,幫助你在技術(shù)領(lǐng)域不斷成長(zhǎng)。無(wú)論你是剛剛起步還是已經(jīng)擁有多年經(jīng)驗(yàn),我們都有適合你的內(nèi)容,助你取得成功。


0 人點(diǎn)贊