EasyExcel级联下拉

代码

java 复制代码
package com.xc.excel.select;

import com.alibaba.excel.EasyExcel;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddressList;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class EasyExcelCascadingDropdown {
    public static void main(String[] args) throws IOException {
        String fileName = "test.xlsx";

        // 定义国家和城市的级联关系
        Map<String, List<String>> countryCityMap = new HashMap<>();
        countryCityMap.put("中国", Arrays.asList("北京", "上海", "广州"));
        countryCityMap.put("美国", Arrays.asList("纽约", "洛杉矶", "芝加哥"));

        // 生成Excel文件
        Workbook workbook = new XSSFWorkbook();
        Sheet mainSheet = workbook.createSheet("MainSheet");

        // 创建标题行
        Row headerRow = mainSheet.createRow(0);
        headerRow.createCell(0).setCellValue("国家");
        headerRow.createCell(1).setCellValue("城市");

        // 创建国家下拉列表数据验证
        DataValidationHelper helper = mainSheet.getDataValidationHelper();
        DataValidationConstraint countryConstraint = helper.createExplicitListConstraint(countryCityMap.keySet().toArray(new String[0]));
        CellRangeAddressList countryRange = new CellRangeAddressList(1, 100, 0, 0);  // 第一列(国家)
        DataValidation countryValidation = helper.createValidation(countryConstraint, countryRange);
        mainSheet.addValidationData(countryValidation);

        // 设置城市的级联下拉
        for (int i = 1; i < 101; i++) { // B列的下拉列表依赖A列的值
            String formula = "INDIRECT(A" + (i + 1) + ")";
            DataValidationConstraint cityConstraint = helper.createFormulaListConstraint(formula);
            CellRangeAddressList cityRange = new CellRangeAddressList(i, i, 1, 1);  // 第二列(城市)
            DataValidation cityValidation = helper.createValidation(cityConstraint, cityRange);
            mainSheet.addValidationData(cityValidation);
        }

        // 创建DataSheet存放国家和城市映射数据
        Sheet dataSheet = workbook.createSheet("DataSheet");
        int rowIndex = 0;
        for (Map.Entry<String, List<String>> entry : countryCityMap.entrySet()) {
            String country = entry.getKey();
            List<String> cities = entry.getValue();

            // 写入国家和对应城市到DataSheet
            Row countryRow = dataSheet.createRow(rowIndex++);
            countryRow.createCell(0).setCellValue(country);
            for (int i = 0; i < cities.size(); i++) {
                Row cityRow = dataSheet.createRow(rowIndex++);
                cityRow.createCell(0).setCellValue(cities.get(i));
            }

            // 为每个国家创建命名区域
            Name name = workbook.createName();
            name.setNameName(country);
            name.setRefersToFormula("DataSheet!$A$" + (rowIndex - cities.size()) + ":$A$" + (rowIndex - 1));
        }

        // 保存Excel文件
        try (FileOutputStream fileOut = new FileOutputStream(fileName)) {
            workbook.write(fileOut);
        }
        workbook.close();

        System.out.println("Excel文件生成成功: " + fileName);
    }
}

效果

相关推荐
橙*^O^*安6 分钟前
Go 语言基础:变量与常量
运维·开发语言·后端·golang·kubernetes
工程师小星星15 分钟前
Golang语言的文件组织方式
开发语言·后端·golang
哈喽姥爷1 小时前
Spring Boot---自动配置原理和自定义Starter
java·spring boot·后端·自定义starter·自动配置原理
舒一笑3 小时前
为什么where=Version就是乐观锁了?
后端·mysql·程序员
GoGeekBaird3 小时前
关于垂类AI应用落地行业的方法论思考
后端·github·agent
小宁爱Python3 小时前
Django 基础入门:命令、结构与核心配置全解析
后端·python·django
你的人类朋友3 小时前
认识一下Bcrypt哈希算法
后端·安全·程序员
tangweiguo030519874 小时前
基于 Django 与 Bootstrap 构建的现代化设备管理平台
后端·django·bootstrap
IT果果日记4 小时前
详解DataX开发达梦数据库插件
大数据·数据库·后端
dazhong20124 小时前
Spring Boot 项目新增 Module 完整指南
java·spring boot·后端