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)文章
Spring?Boot實(shí)現(xiàn)web.xml功能示例詳解
這篇文章主要介紹了Spring?Boot實(shí)現(xiàn)web.xml功能,通過本文介紹我們了解到,在Spring Boot應(yīng)用中,我們可以通過注解和編程兩種方式實(shí)現(xiàn)web.xml的功能,包括如何創(chuàng)建及注冊(cè)Servlet、Filter以及Listener等,需要的朋友可以參考下2023-09-09Spring Cloud 優(yōu)雅下線以及灰度發(fā)布實(shí)現(xiàn)
這篇文章主要介紹了Spring Cloud 優(yōu)雅下線以及灰度發(fā)布實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Java從ftp服務(wù)器上傳與下載文件的實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于Java從ftp服務(wù)器上傳與下載文件的實(shí)現(xiàn)方法,最近項(xiàng)目中需要實(shí)現(xiàn)將文件先存放到ftp上,需要的時(shí)候再?gòu)膄tp上下載,做的過程中碰到了問題,所以這里總結(jié)下,需要的朋友可以參考下2023-08-08java保證對(duì)象在內(nèi)存中唯一性的實(shí)現(xiàn)方法
這篇文章主要介紹了java如何保證對(duì)象在內(nèi)存中的唯一性,如果創(chuàng)建多個(gè)對(duì)象的話,可能會(huì)引發(fā)出各種各樣的問題,這時(shí),就需要我們保證這個(gè)對(duì)象在內(nèi)存中的唯一性,需要的朋友可以參考下2019-06-06SpringBoot中使用Quartz設(shè)置定時(shí)任務(wù)的實(shí)例詳解
Quartz是OpenSymphony開源組織在任務(wù)調(diào)度領(lǐng)域的一個(gè)開源項(xiàng)目,完全基于 Java 實(shí)現(xiàn),本文小編給大家介紹了SpringBoot中如何使用Quartz設(shè)置定時(shí)任務(wù),文中通過代碼示例給大家講解的非常詳細(xì),需要的朋友可以參考下2023-12-12Java對(duì)稱與非對(duì)稱加密算法原理詳細(xì)講解
對(duì)稱加密算法指加密和解密使用相同密鑰的加密算法。對(duì)稱加密算法用來對(duì)敏感數(shù)據(jù)等信息進(jìn)行加密,非對(duì)稱加密算法指加密和解密使用不同密鑰的加密算法,也稱為公私鑰加密2022-11-11Mybatis+Druid+MybatisPlus多數(shù)據(jù)源配置方法
在項(xiàng)目開發(fā)中,經(jīng)常需要連接多個(gè)數(shù)據(jù)庫(kù),使用Mybatis、Druid和MybatisPlus可以實(shí)現(xiàn)多數(shù)據(jù)源配置,通過定義配置類和修改配置文件,如properties或yaml,可以設(shè)置多個(gè)數(shù)據(jù)源,本文介紹了配置項(xiàng)包括Druid基本配置、數(shù)據(jù)源一、數(shù)據(jù)源二,感興趣的朋友一起看看吧2024-09-09java swing實(shí)現(xiàn)的掃雷游戲及改進(jìn)版完整示例
這篇文章主要介紹了java swing實(shí)現(xiàn)的掃雷游戲及改進(jìn)版,結(jié)合完整實(shí)例形式對(duì)比分析了java使用swing框架實(shí)現(xiàn)掃雷游戲功能與相關(guān)操作技巧,需要的朋友可以參考下2017-12-12