辛集市住房和城鄉(xiāng)建設(shè)廳網(wǎng)站優(yōu)化20條措施
文章目錄
- 1、準(zhǔn)備工作
- 2、編寫(xiě)代碼
- 2.1 創(chuàng)建實(shí)體類(lèi)
- 2.2 創(chuàng)建Excel生成服務(wù)
- 2.3 創(chuàng)建控制器
- 3、測(cè)試
- 4、結(jié)論
在許多企業(yè)應(yīng)用程序中,導(dǎo)出數(shù)據(jù)到Excel表格是一項(xiàng)常見(jiàn)的需求。Spring Boot
提供了許多庫(kù)來(lái)簡(jiǎn)化這個(gè)過(guò)程,其中包括Apache POI
和Spring Boot
的相關(guān)模塊。在本文中,我們將使用這些工具來(lái)生成一個(gè)復(fù)雜的Excel表格。
1、準(zhǔn)備工作
首先,確保你的項(xiàng)目中已經(jīng)引入了Spring Boot
及相關(guān)依賴(lài)。在pom.xml
中添加以下依賴(lài):
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version>
</dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version>
</dependency>
2、編寫(xiě)代碼
2.1 創(chuàng)建實(shí)體類(lèi)
首先,我們創(chuàng)建一個(gè)代表數(shù)據(jù)的實(shí)體類(lèi),例如Employee
:
public class Employee {private Long id;private String name;private String department;private double salary;// 省略構(gòu)造函數(shù)和getter/setter方法
}
2.2 創(chuàng)建Excel生成服務(wù)
接下來(lái),我們創(chuàng)建一個(gè)服務(wù)類(lèi)來(lái)生成Excel表格
。這個(gè)服務(wù)類(lèi)將使用Apache POI
庫(kù)來(lái)操作Excel
文件。
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;@Service
public class ExcelService {public byte[] generateExcel(List<Employee> employees) throws IOException {try (Workbook workbook = new XSSFWorkbook()) {Sheet sheet = workbook.createSheet("Employee Data");// 創(chuàng)建表頭Row headerRow = sheet.createRow(0);String[] columns = {"ID", "Name", "Department", "Salary"};for (int i = 0; i < columns.length; i++) {Cell cell = headerRow.createCell(i);cell.setCellValue(columns[i]);}// 填充數(shù)據(jù)int rowNum = 1;for (Employee employee : employees) {Row row = sheet.createRow(rowNum++);row.createCell(0).setCellValue(employee.getId());row.createCell(1).setCellValue(employee.getName());row.createCell(2).setCellValue(employee.getDepartment());row.createCell(3).setCellValue(employee.getSalary());}// 將工作簿轉(zhuǎn)換為字節(jié)數(shù)組ByteArrayOutputStream outputStream = new ByteArrayOutputStream();workbook.write(outputStream);return outputStream.toByteArray();}}
}
2.3 創(chuàng)建控制器
最后,我們創(chuàng)建一個(gè)控制器來(lái)處理HTTP請(qǐng)求
,并調(diào)用Excel
生成服務(wù)來(lái)生成Excel文件
。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;@RestController
public class ExcelController {@Autowiredprivate ExcelService excelService;@GetMapping("/export")public ResponseEntity<byte[]> exportExcel() throws IOException {List<Employee> employees = getEmployees(); // 假設(shè)這里是從數(shù)據(jù)庫(kù)或其他數(shù)據(jù)源獲取數(shù)據(jù)的方法byte[] excelBytes = excelService.generateExcel(employees);HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));headers.setContentDispositionFormData("attachment", "employees.xlsx");return new ResponseEntity<>(excelBytes, headers, HttpStatus.OK);}// 輔助方法,用于生成模擬數(shù)據(jù)private List<Employee> getEmployees() {List<Employee> employees = new ArrayList<>();employees.add(new Employee(1L, "John Doe", "IT", 5000));employees.add(new Employee(2L, "Jane Smith", "HR", 6000));// 添加更多員工...return employees;}
}
3、測(cè)試
現(xiàn)在,啟動(dòng)Spring Boot
應(yīng)用程序,并訪(fǎng)問(wèn)/export
端點(diǎn),將會(huì)下載一個(gè)名為employees.xlsx
的Excel
文件,其中包含了我們模擬的員工數(shù)據(jù)。
4、結(jié)論
通過(guò)本文,我們學(xué)習(xí)了如何使用Spring Boot
和Apache POI
來(lái)生成復(fù)雜的Excel表格
。我們創(chuàng)建了一個(gè)服務(wù)類(lèi)來(lái)處理Excel
生成邏輯,并創(chuàng)建了一個(gè)控制器來(lái)處理HTTP請(qǐng)求
,并提供生成的Excel文件
的下載鏈接。這個(gè)例子可以作為在實(shí)際項(xiàng)目中導(dǎo)出數(shù)據(jù)到Excel
的起點(diǎn),你可以根據(jù)自己的需求進(jìn)行擴(kuò)展和定制。