简体   繁体   English

如何在使用POI生成的Excel工作表中创建依赖下拉列表?

[英]How to create dependent drop downs in excel sheet generated using POI?

we have a function in our java based web application where user can download an excel sheet template from the web application. 我们在基于Java的Web应用程序中提供了一个功能,用户可以在其中从Web应用程序下载Excel工作表模板。 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. 然后,系统读取此excel文件并将此数据保存在数据库中。

Below is snapshot of template file with some sample data in it. 以下是模板文件的快照,其中包含一些示例数据。

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. 我想要的是当用户下载模板文件时(模板文件通常只有标题,因此用户知道哪些数据在哪一列中),Excel工作表应具有分区,产品,二级产品,地区和国家/地区的下拉列表。 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. 它更像是依赖下拉列表。

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. 基本上,我将需要使用Apache POI创建excel工作表,在该工作表中,用户将从下拉菜单中选择值,而不用键入它们。

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 eg some users might enter country as US, some as USA and some as United states. 我们这样做的原因是,例如,某些用户可能以美国身份进入某个国家,某些用户可能以美国进入,而某些用户则以美国作为进入国家。

The same thing goes for products etc. user may enter product as GFFX or GFFX Structuring or gffx etc. 对于产品等也是如此。用户可以将产品输入为GFFX或GFFX Structuring或gffx等。

Is it possible to do this in excel sheet using POI? 是否可以使用POI在Excel工作表中执行此操作? If not what are the other possible solutions or wasy to make sure users know what they have to enter in each columns? 如果不是,还有什么其他可能的解决方案,或者是在确保用户知道他们必须在每个列中输入的内容方面是否浪费?

EDIT 1 : 编辑1:

I could created the drop downs but is it possible to created the dependent drop downs? 我可以创建下拉菜单,但是可以创建从属下拉菜单吗?

在此处输入图片说明

I was about to suggest AurA's solution but it looks like you'll have to build the validation list at run-time indeed. 我本来建议AurA的解决方案,但实际上您似乎必须在运行时构建验证列表。

You should have a look at the POI Quick Guide , it seems they have exactly what you need: 您应该看一下POI快速指南 ,看来它们正是您所需要的:

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

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 format) xssf.usermodel(.xlsx格式)

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

You can get drop-down list (after clicking right mouse button) in case you've added (using POI) suggestions to the rows upper the first row that is visible to the user and should be filled (thus the rows beneath the header contain suggestions and are hgidden). 如果您已添加建议(使用POI)到用户可见并应填充的第一行上方的行中(因此标题下方的行包含建议和被忽略)。

You won't get (AFAIK) category dependancy with POI or even pure excel (without VBA) drop-down list (that holds suggestions on the basis of values entered earlier). 您将不会获得带有POI的(AFAIK)类别依存关系​​,甚至不会获得纯Excel(无VBA)下拉列表(其中包含根据先前输入的值提供的建议)。

What you can do, is to use POI to fill helper sheet with appropriate raw data and use VBA to dynamically generate drop-downs that would allow to pick a value from a list. 您可以做的是使用POI用适当的原始数据填充帮助表,并使用VBA动态生成下拉列表,以允许从列表中选择一个值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM