简体   繁体   English

如何在java中使用apache poi向excel表的整列添加数据验证?

[英]How to add Data validation to entire column of an excel sheet using apache poi in java?

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我浏览了 Apache poi 的文档,找到了下面的例子

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

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.在这种情况下,第 0 行,第 0 列。列中的其余单元格没有验证。 But in actual excel file we can do it so it should be possible.但在实际的 excel 文件中我们可以做到,所以它应该是可能的。 I tried and searched a lot but could not come to a solution.我尝试并搜索了很多,但无法找到解决方案。 Please help..请帮忙..

Another way to get the validation on the whole column you can also use -1 for both row parameters like:另一种对整列进行验证的方法,您还可以对两个行参数使用 -1,例如:

CellRangeAddressList columnRange = new CellRangeAddressList(-1, -1, columnIndex, columnIndex);

Tested this in POI 3.16在 POI 3.16 中对此进行了测试

Chetan all the four parameters of the constructor CellRangeAddressList as below把构造函数CellRangeAddressList的四个参数全部扯掉如下

CellRangeAddressList(index_of_starting_row, index_of_ending_row, index_of_starting_column,index_of_ending_column);

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如果有 10 行和 5 列,并且如果您想在整个第二列中的所有行中添加第二列中的下拉列表,则使用以下代码作为单元格地址

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

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.根据您的代码,如果您通过替换代码添加上面的代码行,这将在整个第二列中添加值 10,20,30 的下拉列表。

hope this will clear the concept and you would get the desire result.希望这会清除概念,你会得到想要的结果。

I'm trying to add below data validation but 1-3 is getting converted to 3-Jan in excel drop down.我正在尝试添加以下数据验证,但 1-3 在 excel 下拉列表中转换为 3-Jan。 Is there a way to fix this?有没有办法来解决这个问题?

DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(new String[]{"1-3", "2", "3"}); DVConstraint dvConstraint = DVConstraint.createExplicitListConstraint(new String[]{"1-3", "2", "3"});

Note - Before adding data validation, I'm using code in the first answer here - How to set a cell format to Text to set data type of 2nd column to text.注意 - 在添加数据验证之前,我在此处的第一个答案中使用了代码 - 如何将单元格格式设置为文本以将第二列的数据类型设置为文本。 Else, on entering 1-3 in excel cell, it was getting converted to 3-Jan automatically.否则,在 excel 单元格中输入 1-3 时,它会自动转换为 3-Jan。

This solved issue of entering 1-3 in excel cell directly, but in drop down, I'm still seeing 3-Jan.这解决了直接在excel单元格中输入1-3的问题,但在下拉菜单中,我仍然看到3-Jan。

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

相关问题 如何使用 Java Apache POI 在 excel 中添加标题列? - How to add header column in excel using Java Apache POI? Apache POI Excel工作表保护和数据验证 - Apache POI Excel Sheet Protection and Data Validation 使用 apache poi 将数据按列导出到 excel 工作表? - Export the data to excel sheet column wise using apache poi? 使用Java Apache POI将数据库数据存储到Excel工作表中 - Store database data into the excel sheet using java Apache POI 如何使用Java Apache POI从excel中删除整行? - How to delete an entire row from excel using Java Apache POI? 如何使用Apache POI获取java中各行excel表的最后一列值 - how to get last column value of respective row of excel sheet in java using Apache POI 如何使用Apache POI for Java将Excel工作表复制到另一个工作簿中? - How to copy a Excel Sheet into another Workbook using Apache POI for Java? 如何在使用apache POI库中按列名获取Excel工作表单元格数据 - How to get the Excel sheet cell data by column name in using apache POI library 如何使用Apache POI用来自数组列表的数据填充Excel工作表 - How to populate an Excel sheet with the data from an arraylist using Apache POI 如何在java中使用Apache POI从Excel工作表中提取数据(查找框架) - How to extract data from Excel sheet using Apache POI in java(lookup framing)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM