如何在使用 POI 生成的 Excel 表中创建相关下拉列表? [英] How to create dependent drop downs in excel sheet generated using POI?

查看:46
本文介绍了如何在使用 POI 生成的 Excel 表中创建相关下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在基于 Java 的 Web 应用程序中有一个函数,用户可以在其中从 Web 应用程序下载 Excel 工作表模板.在此模板中填写他们的数据,然后上传相同的 Excel 表.

we have a function in our java based web application where user can download an excel sheet template from the web application. Fill their data in this template and then upload the same excel sheet.

然后系统读取这个excel文件并将这些数据保存在数据库中.

The system then reads this excel file and save this data in the database.

以下是模板文件的快照,其中包含一些示例数据.

Below is snapshot of template file with some sample data in it.

我想要的是当用户下载模板文件时(模板文件通常只有标题,所以用户知道哪些数据在哪一列),excel表应该有部门、产品、次要产品、地区和国家的下拉菜单.这样用户就不会在这些列中输入任何无效值.

What I want is when users download template file (template file usually just has the headers, so users know which data goes in which column), excel sheet should have drop downs for Division, Product, secondary product , Region and country. So that users do not enter any invalid values in those columns.

同样,产品因部门而异,次级产品因产品而异.它更像是依赖下拉菜单.

As well, products varies according to divisions and secondary product varies according to products. Its more like dependent drop downs.

基本上我需要使用 Apache POI 创建 Excel 表格,用户将在其中从下拉列表中选择值,而不是输入它们.

Basically I will need to create the excel sheet using Apache POI in which users will chose values from the drop dowsn instead of typing it themselevs.

即使我们有服务器端验证,我们也会检查用户输入的值是否有效.

Even though we do have server side validation where we check if the values entered by users are valid or not.

我们这样做的原因是,例如有些用户可能输入国家为美国,有些为美国,有些为美国.

The reason we wnat to do this is that e.g. some users might enter country as US, some as USA and some as United states.

产品等也一样.用户可以输入产品为 GFFX 或 GFFX Structuring 或 gffx 等.

The same thing goes for products etc. user may enter product as GFFX or GFFX Structuring or gffx etc.

是否可以使用 POI 在 Excel 表中执行此操作?如果不是,其他可能的解决方案是什么,或者有什么办法来确保用户知道他们必须在每列中输入什么?

Is it possible to do this in excel sheet using POI? If not what are the other possible solutions or wasy to make sure users know what they have to enter in each columns?

编辑 1:

我可以创建下拉菜单,但是否可以创建相关下拉菜单?

I could created the drop downs but is it possible to created the dependent drop downs?

推荐答案

我正要推荐 AurA 的解决方案,但看起来您确实必须在运行时构建验证列表.

I was about to suggest AurA's solution but it looks like you'll have to build the validation list at run-time indeed.

您应该查看 POI 快速指南,它似乎他们正是您所需要的:

You should have a look at the POI Quick Guide, it seems they have exactly what you need:

hssf.usermodel(二进制 .xls 格式)

hssf.usermodel (binary .xls format)

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Data Validation");
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(new String[]{"10", "20", "30"});
DataValidation dataValidation = new HSSFDataValidation(addressList, dvConstraint);
dataValidation.setSuppressDropDownArrow(false);
sheet.addValidationData(dataValidation);

xssf.usermodel(.xlsx 格式)

xssf.usermodel (.xlsx format)

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Data Validation");
XSSFDataValidationHelper dvHelper = new XSSFDataValidationHelper(sheet);
XSSFDataValidationConstraint dvConstraint = (XSSFDataValidationConstraint)
dvHelper.createExplicitListConstraint(new String[]{"11", "21", "31"});
CellRangeAddressList addressList = new CellRangeAddressList(0, 0, 0, 0);
XSSFDataValidation validation = (XSSFDataValidation)dvHelper.createValidation(
dvConstraint, addressList);
validation.setShowErrorBox(true);
sheet.addValidationData(validation);

这篇关于如何在使用 POI 生成的 Excel 表中创建相关下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆