Back to Repositories

Testing POI Date Format Handling in EasyExcel

A comprehensive test suite for validating date formatting functionality in Apache POI integration within EasyExcel. This test class focuses on examining date cell handling, format detection, and value extraction from Excel workbooks.

Test Coverage Overview

The test suite covers essential date formatting operations in POI integration:
  • Excel workbook loading and sheet access
  • Date cell value extraction and validation
  • Numeric value handling for date cells
  • Date format detection using POI utilities

Implementation Analysis

The testing approach utilizes JUnit 5 framework with direct POI API calls. The implementation demonstrates systematic workbook navigation and cell access patterns, focusing on date-specific cell operations through XSSFWorkbook and related POI classes.

The test validates both raw numeric values and formatted date representations from Excel cells.

Technical Details

Testing infrastructure includes:
  • JUnit Jupiter for test execution
  • Apache POI XSSF implementation for Excel handling
  • SLF4J logging framework for test output
  • POI DateUtil for date format validation
  • Local test resources with sample Excel workbook

Best Practices Demonstrated

The test exemplifies several testing best practices:
  • Proper resource handling with explicit file paths
  • Comprehensive logging for debugging and validation
  • Multiple assertion points for date handling
  • Clear separation of workbook access and data validation
  • Proper exception handling for IO operations

alibaba/easyexcel

easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiDateFormatTest.java

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

import java.io.IOException;

import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 测试poi
 *
 * @author Jiaju Zhuang
 **/

public class PoiDateFormatTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(PoiDateFormatTest.class);

    @Test
    public void read() throws IOException {
        String file
            = "/Users/zhuangjiaju/IdeaProjects/easyexcel/easyexcel-test/src/test/resources/dataformat/dataformat.xlsx";
        XSSFWorkbook xssfWorkbook = new XSSFWorkbook( file);
        XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
        LOGGER.info("一共行数:{}", xssfSheet.getLastRowNum());
        XSSFRow row = xssfSheet.getRow(7);
        XSSFCell cell = row.getCell(0);
        LOGGER.info("dd{}", cell.getDateCellValue());
        LOGGER.info("dd{}", cell.getNumericCellValue());

        LOGGER.info("dd{}", DateUtil.isCellDateFormatted(cell));


    }

}