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 檔案上傳與下載Spring Boot
- Spring boot + Vue axios 檔案下載Spring BootVueiOS
- Spring Boot + thymeleaf 實現檔案上傳下載Spring Boot
- Spring Boot: 加密應用配置檔案敏感資訊Spring Boot加密
- Gradle入門:建立 Spring Boot Web 應用專案GradleSpring BootWeb
- Spring Boot加密應用配置檔案敏感資訊(jasypt)Spring Boot加密
- Spring 對檔案上傳下載的支援(Spring boot實現)Spring Boot
- ajax請求下載excel檔案Excel
- js實現txt/excel檔案下載JSExcel
- Spring Boot @PropertySource 載入指定配置檔案、@ImportResource 匯入Spring 配置檔案Spring BootImport
- spring boot啟動載入外部配置檔案Spring Boot
- Spring Boot應用程式有哪些功能?Spring Boot
- Spring Boot 應用程式中的 QueryDSLSpring Boot
- Spring Boot 配置檔案Spring Boot
- 檔案程式設計、檔案下載程式設計
- Spring Boot應用程式事件教程 - reflectoringSpring Boot事件
- Spring Boot + Kotlin + Coroutines應用演示程式Spring BootKotlin
- Spring Boot 應用程式啟動流程分析Spring Boot
- Spring Boot(十七):使用 Spring Boot 上傳檔案Spring Boot
- 向web伺服器下載檔案Web伺服器
- spring webflux檔案上傳下載SpringWebUX
- 五、Spring Web應用程式構建SpringWeb
- spring cloud feign 檔案上傳和檔案下載SpringCloud
- 【Spring實戰】構建Spring Web應用程式SpringWeb
- spring boot 建立web專案(IDEA)Spring BootWebIdea
- 使用Spring Boot開發Web專案Spring BootWeb
- Spring Boot MVC 單張圖片和多張圖片上傳 和通用檔案下載Spring BootMVC
- Spring Boot (十九):使用 Spring Boot Actuator 監控應用Spring Boot
- vue實現Excel檔案的上傳與下載VueExcel
- Spring Boot 分片上傳檔案Spring Boot
- Spring Boot的檔案上傳Spring Boot
- spring boot配置檔案相關Spring Boot
- spring boot 執行sql檔案Spring BootSQL
- Spring Boot 配置檔案總結Spring Boot
- python讀寫excel檔案簡單應用PythonExcel
- 有關web頁面內容檔案強制下載程式碼Web
- 精進 Spring Boot 03:Spring Boot 的配置檔案和配置管理,以及用三種方式讀取配置檔案Spring Boot