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

效果

相关推荐
Asthenia04125 分钟前
ES-Java:一网打尽SearchRequest/SearchSourceBuilder/BoolQueryBuilder/QueryBuilders
后端
Aska_Lv27 分钟前
业务架构设计---硬件设备监控指标数据上报业务Java企业级架构
后端·架构
m0_7482552638 分钟前
Spring Boot 3.x 引入springdoc-openapi (内置Swagger UI、webmvc-api)
spring boot·后端·ui
小华同学ai42 分钟前
吊打中文合成!这款开源语音神器效果炸裂,逼真到离谱!
前端·后端·github
语落心生1 小时前
算法计算与训练如何支持低开销流式计算? deepseek背后的smallpond需要些新改造
后端
uhakadotcom1 小时前
Python高并发实战:阿里云函数计算 + 异步编程高效处理万人请求
后端·面试·github
uhakadotcom1 小时前
Apache Flink:实时数据处理的强大工具
后端·面试·github
INSO1 小时前
Docker Compose
后端
uhakadotcom1 小时前
了解Nginx替代品:选择合适的Web服务器
后端·面试·github
RisingWave中文开源社区1 小时前
经验分享|用开源产品构建一个高性能实时推荐引擎
数据库·后端·开源