辛集市住房和城鄉(xiāng)建設(shè)廳網(wǎng)站優(yōu)化20條措施
文章目錄
- 1、準備工作
- 2、編寫代碼
- 2.1 創(chuàng)建實體類
- 2.2 創(chuàng)建Excel生成服務(wù)
- 2.3 創(chuàng)建控制器
- 3、測試
- 4、結(jié)論
在許多企業(yè)應(yīng)用程序中,導(dǎo)出數(shù)據(jù)到Excel表格是一項常見的需求。Spring Boot
提供了許多庫來簡化這個過程,其中包括Apache POI
和Spring Boot
的相關(guān)模塊。在本文中,我們將使用這些工具來生成一個復(fù)雜的Excel表格。
1、準備工作
首先,確保你的項目中已經(jīng)引入了Spring Boot
及相關(guān)依賴。在pom.xml
中添加以下依賴:
<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、編寫代碼
2.1 創(chuàng)建實體類
首先,我們創(chuàng)建一個代表數(shù)據(jù)的實體類,例如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ù)
接下來,我們創(chuàng)建一個服務(wù)類來生成Excel表格
。這個服務(wù)類將使用Apache POI
庫來操作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)建一個控制器來處理HTTP請求
,并調(diào)用Excel
生成服務(wù)來生成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ù)庫或其他數(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、測試
現(xiàn)在,啟動Spring Boot
應(yīng)用程序,并訪問/export
端點,將會下載一個名為employees.xlsx
的Excel
文件,其中包含了我們模擬的員工數(shù)據(jù)。
4、結(jié)論
通過本文,我們學習了如何使用Spring Boot
和Apache POI
來生成復(fù)雜的Excel表格
。我們創(chuàng)建了一個服務(wù)類來處理Excel
生成邏輯,并創(chuàng)建了一個控制器來處理HTTP請求
,并提供生成的Excel文件
的下載鏈接。這個例子可以作為在實際項目中導(dǎo)出數(shù)據(jù)到Excel
的起點,你可以根據(jù)自己的需求進行擴展和定制。