Spring Boot Web應用程式下載Excel檔案 - simplesolution
在本文中,我們將逐步建立Spring Boot Web應用程式並實現下載Excel檔案功能。我使用Spring Tool Suite 4建立用於演示的應用程式和程式碼編輯器。點選標題見原文圖示。
如果您正在使用Maven,則在xml下面新增:
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.0.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> |
實現ExcelFileExporter類:
public class ExcelFileExporter { public static ByteArrayInputStream contactListToExcelFile(List<Customer> customers) { try(Workbook workbook = new XSSFWorkbook()){ Sheet sheet = workbook.createSheet("Customers"); Row row = sheet.createRow(0); CellStyle headerCellStyle = workbook.createCellStyle(); headerCellStyle.setFillForegroundColor(IndexedColors.AQUA.getIndex()); headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); // Creating header Cell cell = row.createCell(0); cell.setCellValue("First Name"); cell.setCellStyle(headerCellStyle); cell = row.createCell(1); cell.setCellValue("Last Name"); cell.setCellStyle(headerCellStyle); cell = row.createCell(2); cell.setCellValue("Mobile"); cell.setCellStyle(headerCellStyle); cell = row.createCell(3); cell.setCellValue("Email"); cell.setCellStyle(headerCellStyle); // Creating data rows for each customer for(int i = 0; i < customers.size(); i++) { Row dataRow = sheet.createRow(i + 1); dataRow.createCell(0).setCellValue(customers.get(i).getFirstName()); dataRow.createCell(1).setCellValue(customers.get(i).getLastName()); dataRow.createCell(2).setCellValue(customers.get(i).getMobileNumber()); dataRow.createCell(3).setCellValue(customers.get(i).getEmail()); } // Making size of column auto resize to fit with data sheet.autoSizeColumn(0); sheet.autoSizeColumn(1); sheet.autoSizeColumn(2); sheet.autoSizeColumn(3); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); workbook.write(outputStream); return new ByteArrayInputStream(outputStream.toByteArray()); } catch (IOException ex) { ex.printStackTrace(); return null; } } } |
對外控制類:
@Controller public class DownloadExcelController { @RequestMapping("/") public String index() { return "index"; } @GetMapping("/download/customers.xlsx") public void downloadCsv(HttpServletResponse response) throws IOException { response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=customers.xlsx"); ByteArrayInputStream stream = ExcelFileExporter.contactListToExcelFile(createTestData()); IOUtils.copy(stream, response.getOutputStream()); } private List<Customer> createTestData(){ List<Customer> customers = new ArrayList<Customer>(); customers.add(new Customer("Vernon", "Barlow", "0123456789", "test1@simplesolution.dev")); customers.add(new Customer("Maud", "Brock", "0123456788", "test2@simplesolution.dev")); customers.add(new Customer("Chyna", "Cowan", "0123456787", "test3@simplesolution.dev")); customers.add(new Customer("Krisha", "Tierney", "0123456786", "test4@simplesolution.dev")); customers.add(new Customer("Sherry", "Rosas", "0123456785", "test5@simplesolution.dev")); return customers; } } |
訪問:http://localhost:8080/download/customers.xlsx
相關文章
- 在Spring Boot程式中上傳和下載檔案Spring Boot
- Spring boot + Vue axios 檔案下載Spring BootVueiOS
- Spring Boot 檔案上傳與下載Spring Boot
- Spring 對檔案上傳下載的支援(Spring boot實現)Spring Boot
- Spring Boot加密應用配置檔案敏感資訊(jasypt)Spring Boot加密
- ajax請求下載excel檔案Excel
- Spring Boot @PropertySource 載入指定配置檔案、@ImportResource 匯入Spring 配置檔案Spring BootImport
- js實現txt/excel檔案下載JSExcel
- Spring Boot 應用程式中的 QueryDSLSpring Boot
- Spring Boot + Kotlin + Coroutines應用演示程式Spring BootKotlin
- Spring Boot應用程式事件教程 - reflectoringSpring Boot事件
- Spring Boot應用程式有哪些功能?Spring Boot
- Spring Boot 配置檔案Spring Boot
- 檔案程式設計、檔案下載程式設計
- Spring Boot 應用程式啟動流程分析Spring Boot
- spring boot 建立web專案(IDEA)Spring BootWebIdea
- spring cloud feign 檔案上傳和檔案下載SpringCloud
- 五、Spring Web應用程式構建SpringWeb
- 向web伺服器下載檔案Web伺服器
- python讀寫excel檔案簡單應用PythonExcel
- 在Spring Boot應用程式中使用Kubernetes ConfigMapSpring Boot
- 使用Spring Boot開發Web專案Spring BootWeb
- Spring Boot 配置檔案總結Spring Boot
- Spring Boot 分片上傳檔案Spring Boot
- spring boot 執行sql檔案Spring BootSQL
- Spring Boot的檔案上傳Spring Boot
- spring boot配置檔案相關Spring Boot
- 精進 Spring Boot 03:Spring Boot 的配置檔案和配置管理,以及用三種方式讀取配置檔案Spring Boot
- vue實現Excel檔案的上傳與下載VueExcel
- Spring Boot應用程式中的常用註釋列表Spring Boot
- Spring Boot入門-快速搭建web專案Spring BootWeb
- Maven建立Web應用程式專案MavenWeb
- Spring Boot中用@RequestMapping 提供 Zip 檔案Spring BootAPP
- Spring Boot的配置檔案管理技巧Spring Boot
- 從檔案下載視角來理解Web APIWebAPI
- Java 匯入資料到Excel並提供檔案下載介面JavaExcel
- Spring boot應用如何支援httpsSpring BootHTTP
- spring-boot-route(十八)spring-boot-adtuator監控應用Springboot