如何在Java中使用Apache POI将数据验证添加到Excel工作表的整个列中? [英] How to add Data validation to entire column of an excel sheet using apache poi in java?

查看:363
本文介绍了如何在Java中使用Apache POI将数据验证添加到Excel工作表的整个列中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我需要在整个列而不是特定单元格中添加数据验证.我浏览了Apache poi的文档,并在下面找到了示例

I have a requirement Where I need to add data validation to an entire column rather than a specific cell. I went through the documentation of Apache poi and found the example below

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

但是上面的示例为特定单元格创建了一个下拉数据验证.在这种情况下,行0为列0.一列中的其余单元格都没有验证.但是在实际的excel文件中,我们可以做到,因此应该可行.我尝试并搜索了很多,但无法找到解决方案.请帮忙.

But the above example creates a drop down datavalidation for a specific cell. In this case row 0, column 0. The rest of the cells in a column doesn't have validation. But in actual excel file we can do it so it should be possible. I tried and searched a lot but could not come to a solution. Please help..

推荐答案

Chetan构造函数CellRangeAddressList的所有四个参数如下

Chetan all the four parameters of the constructor CellRangeAddressList as below

CellRangeAddressList(index_of_starting_row, index_of_ending_row, index_of_starting_column,index_of_ending_column);

例如如果有10行和5列,并且如果要在所有行的第二列中添加下拉列表,则意味着整个第二列中都包含该代码,那么请使用下面的代码作为单元格地址

so eg. if there are 10 rows and 5 columns, and if you want to add drop down list in 2nd column throughout the all rows means in entire 2nd column then use below code for cell address

CellRangeAddressList addressList = new CellRangeAddressList(0,9,1,1);

根据您的代码,如果您通过替换代码添加上述代码,则会在整个第二列中添加下拉式列表,其值分别为10、20、30.

based on your code this will add drop down with the values 10,20,30 in entire 2nd column if you add the above line of code by replacing your code.

希望这将清除概念,您将获得期望的结果.

hope this will clear the concept and you would get the desire result.

这篇关于如何在Java中使用Apache POI将数据验证添加到Excel工作表的整个列中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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