SpringBoot集成Apache POI實(shí)現(xiàn)Excel的導(dǎo)入導(dǎo)出
前言
在 Spring Boot 中使用 Apache POI 實(shí)現(xiàn) Excel 的導(dǎo)入和導(dǎo)出功能是一種常見的做法。Apache POI 是一個(gè)流行的 Java 庫(kù),用于處理 Microsoft Office 格式文件,包括 Excel 文件。在 Spring Boot 中結(jié)合 Apache POI 可以輕松地實(shí)現(xiàn) Excel 文件的讀寫操作。下面我將詳細(xì)介紹如何在 Spring Boot 中使用 Apache POI 實(shí)現(xiàn) Excel 的導(dǎo)入和導(dǎo)出。
一、Apache POI 是什么?
Apache POI(Poor Obfuscation Implementation)是一個(gè)流行的 Java 庫(kù),用于處理 Microsoft Office 格式文件,包括 Word 文檔、Excel 表格和 PowerPoint 演示文稿。它提供了一組類和方法,使開發(fā)人員能夠讀取、創(chuàng)建和修改這些 Office 格式文件。
Apache POI 提供了對(duì) Office 格式文件的抽象表示,使得開發(fā)人員可以在程序中操作這些文件的內(nèi)容、格式和樣式。通過 Apache POI,開發(fā)人員可以實(shí)現(xiàn)諸如從 Excel 中導(dǎo)入數(shù)據(jù)、向 Word 文檔中插入表格、從 PowerPoint 中提取文本等操作。
Apache POI 由 Apache 軟件基金會(huì)維護(hù)和發(fā)布,是一個(gè)開源項(xiàng)目。它為 Java 開發(fā)人員提供了處理 Office 格式文件的強(qiáng)大工具,使得在 Java 應(yīng)用程序中集成 Office 文件操作變得更加便捷和靈活。
二、使用 Apache POI 實(shí)現(xiàn) Excel 的導(dǎo)入和導(dǎo)出
① 導(dǎo)入 Excel
1. 添加依賴
首先,在 Maven 或 Gradle 項(xiàng)目中的配置文件中添加 Apache POI 的依賴項(xiàng)。
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. 編寫導(dǎo)入邏輯
編寫一個(gè)方法,該方法接收上傳的 Excel 文件,并解析其中的數(shù)據(jù)。這里以導(dǎo)入用戶信息為例:
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); // 假設(shè)用戶信息在第一個(gè) 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 中處理上傳請(qǐng)求
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());
// 處理導(dǎo)入的用戶數(shù)據(jù),如保存到數(shù)據(jù)庫(kù)等
return ResponseEntity.ok("導(dǎo)入成功");
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("導(dǎo)入失敗");
}
}
}
② 導(dǎo)出 Excel
1. 添加依賴
已經(jīng)在前面添加了 Apache POI 的依賴,這里不需要重復(fù)添加。
2. 編寫導(dǎo)出邏輯
編寫一個(gè)方法,該方法將數(shù)據(jù)寫入到 Excel 文件中并提供下載鏈接。這里同樣以導(dǎo)出用戶信息為例:
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());
// 添加更多字段...
}
// 設(shè)置響應(yīng)頭
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-disposition", "attachment; filename=users.xlsx");
// 輸出到響應(yīng)流
workbook.write(response.getOutputStream());
workbook.close();
}
}
3. 在 Controller 中處理導(dǎo)出請(qǐng)求
@RestController
@RequestMapping("/export")
public class ExcelExportController {
@Autowired
private ExcelExportService excelExportService;
@GetMapping("/users")
public void exportUsers(HttpServletResponse response) {
try {
List<User> userList = userService.getAllUsers(); // 假設(shè)獲取所有用戶信息的方法
excelExportService.exportUsers(userList, response);
} catch (Exception e) {
e.printStackTrace();
// 處理異常
}
}
}
總結(jié)
本文簡(jiǎn)單講述了Spring Boot 中使用 Apache POI 實(shí)現(xiàn) Excel 的導(dǎo)入和導(dǎo)出的方法步驟,通過 Apache POI,我們可以方便地處理 Excel 文件,完成數(shù)據(jù)的導(dǎo)入和導(dǎo)出操作。更多相關(guān)SpringBoot POI Excel導(dǎo)入導(dǎo)出內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java猜數(shù)字游戲從思路到實(shí)現(xiàn)開發(fā)全過程
猜數(shù)字游戲雖然簡(jiǎn)單,卻涵蓋了程序設(shè)計(jì)的基本要素,數(shù)據(jù)處理、流程控制、用戶交互和異常處理,這篇文章主要介紹了Java猜數(shù)字游戲從思路到實(shí)現(xiàn)開發(fā)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-10-10
SpringBoot 配置文件中配置的中文,程序讀取出來是亂碼的解決
這篇文章主要介紹了SpringBoot 配置文件中配置的中文,程序讀取出來是亂碼的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
Javaweb 500 服務(wù)器內(nèi)部錯(cuò)誤的解決
這篇文章主要介紹了Javaweb 500 服務(wù)器內(nèi)部錯(cuò)誤的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
透明化Sharding-JDBC數(shù)據(jù)庫(kù)字段加解密方案
這篇文章主要為大家介紹了透明化Sharding-JDBC數(shù)據(jù)庫(kù)字段加解密方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-02-02
idea hibernate jpa 生成實(shí)體類的實(shí)現(xiàn)
這篇文章主要介紹了idea hibernate jpa 生成實(shí)體類的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
Java 無符號(hào)右移與右移運(yùn)算符的使用介紹
這篇文章主要介紹了Java 無符號(hào)右移與右移運(yùn)算符的使用介紹,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Java日期轉(zhuǎn)換注解配置date?format時(shí)間失效
這篇文章主要為大家介紹了Java日期轉(zhuǎn)換注解配置date?format時(shí)間失效,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Spring中Service注入多個(gè)實(shí)現(xiàn)類的方法詳解
這篇文章主要介紹了Spring中Service注入多個(gè)實(shí)現(xiàn)類的方法詳解,Spring是一個(gè)開源的Java框架,用于構(gòu)建企業(yè)級(jí)應(yīng)用程序,它提供了許多功能,如依賴注入、面向切面編程、數(shù)據(jù)訪問、Web開發(fā)等,需要的朋友可以參考下2023-07-07

