EasyExcel Wrapper for Simplified Import and Export in Spring Boot Backend
This article demonstrates how to encapsulate Alibaba's EasyExcel library within a Spring Boot backend, providing step‑by‑step environment setup, entity annotations, service interfaces, implementation details, usage examples, and solutions to common issues such as date conversion and template‑based export limitations.
Environment Preparation
Development environment: Spring Boot + MyBatis‑Plus + database.
Example table definition:
CREATE TABLE `test` (
`num` decimal(10,0) DEFAULT NULL COMMENT '数字',
`sex` varchar(100) DEFAULT NULL COMMENT '性别',
`name` varchar(100) DEFAULT NULL COMMENT '姓名',
`born_date` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;Usage Steps
Inject @Autowired ExcelService excelService; into the controller.
Annotate entity fields with EasyExcel annotations while preserving them, e.g.: @Data @TableName("test") public class TestEntity { @Schema(description = "数字") @ExcelProperty("数字") private BigDecimal num; @Schema(description = "性别") @ExcelProperty("性别") private String sex; @Schema(description = "姓名") @ExcelProperty("姓名") private String name; @Schema(description = "创建时间") @ExcelProperty(value = "创建时间") private Date bornDate; }
Use the service methods for import and export: @PostMapping("/importExcel") public void importExcel(@RequestParam MultipartFile file) { excelService.importExcel(file, TestEntity.class, 2, testService::saveBatch); } @PostMapping("/exportExcel") public void exportExcel(HttpServletResponse response) throws IOException { excelService.exportExcel(testService.list(), TestEntity.class, response); }
Full Controller Example
@RestController
@RequestMapping("test")
@RequiredArgsConstructor
public class TestController {
private final ExcelService excelService;
private final TestService testService;
@PostMapping("/importExcel")
public void importExcel(@RequestParam MultipartFile file) {
excelService.importExcel(file, TestEntity.class, 2, testService::saveBatch);
}
@PostMapping("/exportExcel")
public void exportExcel(HttpServletResponse response) throws IOException {
excelService.exportExcel(testService.list(), TestEntity.class, response);
}
}Core Service Interface
public interface ExcelService {
void exportExcel(List
list, Class
tClass, HttpServletResponse response) throws IOException;
void exportExcel(List
list, Function
map, Class
rClass, HttpServletResponse response) throws IOException;
void exportExcel(List
list, Class
tClass, String template, HttpServletResponse response) throws IOException;
void importExcel(MultipartFile file, Class
tClass, Integer headRowNumber, Function
map, Consumer
> consumer);
void importExcel(MultipartFile file, Class
tClass, Integer headRowNumber, Consumer
> consumer);
}Implementation Highlights
The DefaultExcelServiceImpl sets the HTTP response headers, uses EasyExcel to write data, registers converters (e.g., LocalDateTimeConverter ), and provides overloads for template‑based export and data transformation.
@Service
public class DefaultExcelServiceImpl implements ExcelService {
@Override
public
void exportExcel(List
list, Class
tClass, HttpServletResponse response) throws IOException {
setResponse(response);
EasyExcel.write(response.getOutputStream())
.head(tClass)
.excelType(ExcelTypeEnum.XLSX)
.registerConverter(new LocalDateTimeConverter())
.sheet("工作簿1")
.doWrite(list);
}
// other overloads omitted for brevity
private void setResponse(HttpServletResponse response) throws UnsupportedEncodingException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("data", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xls");
}
}Common Issues and Solutions
Date format when exporting via template – implement a custom DateConverter and annotate the date field with converter = DateConverter.class .
POI version compatibility – ensure POI and ooxml versions match.
LocalDateTime conversion – use LocalDateTimeConverter to parse and format strings.
Current limitation: EasyExcel 3.3.2 only supports template export for .xls files; .xlsx templates are not yet supported.
Reference: EasyExcel Official Documentation
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.