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