Back to Repositories

Testing Excel Template Fill Operations with Null Handling in EasyExcel

This test suite validates Excel template filling functionality in EasyExcel, focusing on handling null values and template variable mismatches. It demonstrates vertical data filling and template-based Excel generation.

Test Coverage Overview

The test suite covers template-based Excel file generation with specific focus on null pointer handling and missing variable scenarios.

  • Tests vertical data filling with FillConfig
  • Validates handling of non-existent template variables
  • Verifies template-based Excel generation with multiple data sources

Implementation Analysis

The implementation uses EasyExcel’s writer API with template-based approach.

Key patterns include:
  • ExcelWriter configuration with template file
  • FillConfig for controlling data fill direction
  • FillWrapper usage for named data sets
  • HashMap implementation for variable mapping

Technical Details

Testing tools and configuration:
  • JUnit Jupiter test framework
  • EasyExcel API for Excel operations
  • Custom TestFileUtil for file path management
  • FillData model class for test data generation

Best Practices Demonstrated

The test demonstrates robust Excel processing practices.

  • Proper resource handling with ExcelWriter
  • Structured test data generation
  • Clear separation of template and output files
  • Graceful handling of missing template variables

alibaba/easyexcel

easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue1663/FillTest.java

            
package com.alibaba.easyexcel.test.temp.issue1663;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.easyexcel.test.demo.fill.FillData;
import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;

import org.junit.jupiter.api.Test;

public class FillTest {
    @Test
    public void TestFillNullPoint() {
        String templateFileName =
            TestFileUtil.getPath() + "temp/issue1663" + File.separator + "template.xlsx";

        String fileName = TestFileUtil.getPath() + "temp/issue1663" + File.separator + "issue1663.xlsx";
        ExcelWriter excelWriter = EasyExcel.write(fileName).withTemplate(templateFileName).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();
        FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.VERTICAL).build();
        excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet);

        Map<String, Object> map = new HashMap<String, Object>();
        // Variable {date} does not exist in the template.xlsx, which should be ignored instead of reporting an error.
        map.put("date", "2019年10月9日13:28:28");
        excelWriter.fill(map, writeSheet);
        excelWriter.finish();
    }

    private List<com.alibaba.easyexcel.test.demo.fill.FillData> data() {
        List<com.alibaba.easyexcel.test.demo.fill.FillData> list
            = new ArrayList<com.alibaba.easyexcel.test.demo.fill.FillData>();
        for (int i = 0; i < 10; i++) {
            com.alibaba.easyexcel.test.demo.fill.FillData fillData = new FillData();
            list.add(fillData);
            fillData.setName("张三");
            fillData.setNumber(5.2);
        }
        return list;
    }
}