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.

Architecture Digest
Architecture Digest
Architecture Digest
EasyExcel Wrapper for Simplified Import and Export in Spring Boot Backend

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 {
    <T> void exportExcel(List<T> list, Class<T> tClass, HttpServletResponse response) throws IOException;
    <T, R> void exportExcel(List<T> list, Function<T, R> map, Class<R> rClass, HttpServletResponse response) throws IOException;
    <T> void exportExcel(List<T> list, Class<T> tClass, String template, HttpServletResponse response) throws IOException;
    <T, R> void importExcel(MultipartFile file, Class<T> tClass, Integer headRowNumber, Function<T, R> map, Consumer<List<R>> consumer);
    <T> void importExcel(MultipartFile file, Class<T> tClass, Integer headRowNumber, Consumer<List<T>> 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 <T> void exportExcel(List<T> list, Class<T> 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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaSpringBooteasyexcelExcelImportExport
Architecture Digest
Written by

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.

0 followers
Reader feedback

How this landed with the community

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.