SpringBoot集成Apache POI實現(xiàn)Excel的導入導出
前言
在 Spring Boot 中使用 Apache POI 實現(xiàn) Excel 的導入和導出功能是一種常見的做法。Apache POI 是一個流行的 Java 庫,用于處理 Microsoft Office 格式文件,包括 Excel 文件。在 Spring Boot 中結(jié)合 Apache POI 可以輕松地實現(xiàn) Excel 文件的讀寫操作。下面我將詳細介紹如何在 Spring Boot 中使用 Apache POI 實現(xiàn) Excel 的導入和導出。
一、Apache POI 是什么?
Apache POI(Poor Obfuscation Implementation)是一個流行的 Java 庫,用于處理 Microsoft Office 格式文件,包括 Word 文檔、Excel 表格和 PowerPoint 演示文稿。它提供了一組類和方法,使開發(fā)人員能夠讀取、創(chuàng)建和修改這些 Office 格式文件。
Apache POI 提供了對 Office 格式文件的抽象表示,使得開發(fā)人員可以在程序中操作這些文件的內(nèi)容、格式和樣式。通過 Apache POI,開發(fā)人員可以實現(xiàn)諸如從 Excel 中導入數(shù)據(jù)、向 Word 文檔中插入表格、從 PowerPoint 中提取文本等操作。
Apache POI 由 Apache 軟件基金會維護和發(fā)布,是一個開源項目。它為 Java 開發(fā)人員提供了處理 Office 格式文件的強大工具,使得在 Java 應用程序中集成 Office 文件操作變得更加便捷和靈活。
二、使用 Apache POI 實現(xiàn) Excel 的導入和導出
① 導入 Excel
1. 添加依賴
首先,在 Maven 或 Gradle 項目中的配置文件中添加 Apache POI 的依賴項。
Maven 依賴:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>{latest_version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>{latest_version}</version>
</dependency>
Gradle 依賴:
implementation 'org.apache.poi:poi:{latest_version}'
implementation 'org.apache.poi:poi-ooxml:{latest_version}'
2. 編寫導入邏輯
編寫一個方法,該方法接收上傳的 Excel 文件,并解析其中的數(shù)據(jù)。這里以導入用戶信息為例:
import org.apache.poi.ss.usermodel.*;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Service
public class ExcelImportService {
public List<User> importUsers(InputStream inputStream) throws Exception {
List<User> userList = new ArrayList<>();
Workbook workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheetAt(0); // 假設用戶信息在第一個 Sheet 中
Iterator<Row> rowIterator = sheet.iterator();
while (rowIterator.hasNext()) {
Row row = rowIterator.next();
if (row.getRowNum() == 0) { // 跳過表頭
continue;
}
User user = new User();
user.setId(row.getCell(0).getStringCellValue());
user.setName(row.getCell(1).getStringCellValue());
// 解析更多字段...
userList.add(user);
}
workbook.close();
return userList;
}
}
3. 在 Controller 中處理上傳請求
import org.springframework.web.multipart.MultipartFile;
@RestController
@RequestMapping("/import")
public class ExcelImportController {
@Autowired
private ExcelImportService excelImportService;
@PostMapping("/users")
public ResponseEntity<String> importUsers(@RequestParam("file") MultipartFile file) {
try {
List<User> userList = excelImportService.importUsers(file.getInputStream());
// 處理導入的用戶數(shù)據(jù),如保存到數(shù)據(jù)庫等
return ResponseEntity.ok("導入成功");
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("導入失敗");
}
}
}
② 導出 Excel
1. 添加依賴
已經(jīng)在前面添加了 Apache POI 的依賴,這里不需要重復添加。
2. 編寫導出邏輯
編寫一個方法,該方法將數(shù)據(jù)寫入到 Excel 文件中并提供下載鏈接。這里同樣以導出用戶信息為例:
import org.apache.poi.ss.usermodel.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
@Service
public class ExcelExportService {
public void exportUsers(List<User> userList, HttpServletResponse response) throws Exception {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("用戶信息");
// 創(chuàng)建表頭
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("ID");
headerRow.createCell(1).setCellValue("姓名");
// 添加更多字段...
// 寫入數(shù)據(jù)
int rowNum = 1;
for (User user : userList) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(user.getId());
row.createCell(1).setCellValue(user.getName());
// 添加更多字段...
}
// 設置響應頭
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-disposition", "attachment; filename=users.xlsx");
// 輸出到響應流
workbook.write(response.getOutputStream());
workbook.close();
}
}
3. 在 Controller 中處理導出請求
@RestController
@RequestMapping("/export")
public class ExcelExportController {
@Autowired
private ExcelExportService excelExportService;
@GetMapping("/users")
public void exportUsers(HttpServletResponse response) {
try {
List<User> userList = userService.getAllUsers(); // 假設獲取所有用戶信息的方法
excelExportService.exportUsers(userList, response);
} catch (Exception e) {
e.printStackTrace();
// 處理異常
}
}
}
總結(jié)
本文簡單講述了Spring Boot 中使用 Apache POI 實現(xiàn) Excel 的導入和導出的方法步驟,通過 Apache POI,我們可以方便地處理 Excel 文件,完成數(shù)據(jù)的導入和導出操作。更多相關(guān)SpringBoot POI Excel導入導出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java猜數(shù)字游戲從思路到實現(xiàn)開發(fā)全過程
猜數(shù)字游戲雖然簡單,卻涵蓋了程序設計的基本要素,數(shù)據(jù)處理、流程控制、用戶交互和異常處理,這篇文章主要介紹了Java猜數(shù)字游戲從思路到實現(xiàn)開發(fā)的相關(guān)資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-10-10
SpringBoot 配置文件中配置的中文,程序讀取出來是亂碼的解決
這篇文章主要介紹了SpringBoot 配置文件中配置的中文,程序讀取出來是亂碼的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
透明化Sharding-JDBC數(shù)據(jù)庫字段加解密方案
這篇文章主要為大家介紹了透明化Sharding-JDBC數(shù)據(jù)庫字段加解密方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-02-02
idea hibernate jpa 生成實體類的實現(xiàn)
這篇文章主要介紹了idea hibernate jpa 生成實體類的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
Java日期轉(zhuǎn)換注解配置date?format時間失效
這篇文章主要為大家介紹了Java日期轉(zhuǎn)換注解配置date?format時間失效,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
Spring中Service注入多個實現(xiàn)類的方法詳解
這篇文章主要介紹了Spring中Service注入多個實現(xiàn)類的方法詳解,Spring是一個開源的Java框架,用于構(gòu)建企業(yè)級應用程序,它提供了許多功能,如依賴注入、面向切面編程、數(shù)據(jù)訪問、Web開發(fā)等,需要的朋友可以參考下2023-07-07

