Backend Development 17 min read

Encapsulating EasyExcel in Spring Boot for Simplified Import and Export

This article demonstrates how to wrap EasyExcel within a Spring Boot + MyBatis‑Plus project, providing step‑by‑step guidance on environment preparation, component design, usage examples, full source code, and solutions to common issues such as template export, date conversion, and POI version compatibility.

Top Architect
Top Architect
Top Architect
Encapsulating EasyExcel in Spring Boot for Simplified Import and Export

In this tutorial a senior architect shares a complete solution for encapsulating EasyExcel in a Spring Boot + MyBatis‑Plus environment, aiming to simplify Excel import/export for developers and avoid repetitive code across projects.

Environment preparation

Development stack: SpringBoot, MyBatis‑Plus, MySQL. The article provides a sample table definition and the required Maven dependencies.

-- `dfec-tcht-platform-dev`.test 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 ExcelService in the controller.

Annotate entity fields with EasyExcel annotations ( @ExcelProperty , @Schema ).

Call importExcel and exportExcel methods provided by the service.

@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 code

package com.dfec.server.controller;

import com.dfec.framework.excel.service.ExcelService;
import com.dfec.server.entity.TestEntity;
import com.dfec.server.service.TestService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@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);
    }
}

Encapsulation process

The core idea is to provide a thin wrapper around EasyExcel that keeps dependencies minimal while exposing convenient overloads for import, export, template export, and type conversion.

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);
}

The implementation uses EasyExcel.write and EasyExcel.read , handling response headers, character encoding, and error fallback.

Common issues and solutions

Template export only supports .xls in EasyExcel 3.3.2; upgrading or custom handling is required for .xlsx .

Date conversion problems are solved by custom converters such as LocalDateTimeConverter and DateConverter .

POI and ooxml versions must match to avoid runtime errors.

References include the official EasyExcel documentation and links to related open‑source projects.

BackendJavaSpringBootEasyExcelExcelImportExport
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.