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

效果

相关推荐
妙码生花11 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(四十三):前后端数据验证
后端·go·ai编程
YuePeng12 小时前
别再让 AI 直接写 SQL 了:一个注解搞定十亿行数据的语义层
后端·github
半夜里咳嗽的狼12 小时前
Go 1.25 的 WaitGroup.Go 省了两行代码,也补不上这三个并发边界
后端·go
Lihua奏12 小时前
身份验证:登录之后,服务器怎么一直认得你?
后端
XuCoder12 小时前
Redis 缓存和 MySQL 数据不一致怎么办?双写一致性一次讲透
后端
程序员天天困12 小时前
Arthas ognl 表达式从入门到实战:掌握在线调试最强的表达式引擎
java·jvm·后端
Escape12 小时前
为什么你的 AI 越聊越傻?从 Token 到 Agent,彻底搞懂 AI Agent的秘密㊙️
前端·人工智能·后端
用户409666013175113 小时前
Lombok 你用对了吗?@Data 之外的 6 个隐藏神器
java·后端·代码规范
董员外13 小时前
RAG 系统进化论(二):Naive RAG,检索增强生成的最小闭环
前端·人工智能·后端
掘金一周13 小时前
看看大家每月的成本有多少 | 沸点周刊 7.30
前端·人工智能·后端