poi 3.2 通过数据有效性,设置Excel单元格为列表实现

相关包:

import org.apache.poi.hssf.usermodel.DVConstraint;

import org.apache.poi.hssf.usermodel.HSSFDataValidation;

import org.apache.poi.hssf.util.CellRangeAddressList;

 

数据有效性代码:

    /**

     * 方法名称:SetDataValidation

     * 内容摘要:设置数据有效性

     * @param firstRow

     * @param firstCol

     * @param endRow

     * @param endCol 

     */

private static HSSFDataValidation SetDataValidation(short firstRow,

short firstCol, short endRow, short endCol) {

//设置下拉列表的内容

String[] textList = {

“列表1”,

“列表2”,

“列表3”,

“列表4”,

“列表5”};

//加载下拉列表内容

DVConstraint constraint = DVConstraint.createExplicitListConstraint(textList);

//设置数据有效性加载在哪个单元格上。

//四个参数分别是:起始行、终止行、起始列、终止列

CellRangeAddressList regions =

                            new CellRangeAddressList(firstRow,endRow, firstCol, endCol);

//数据有效性对象

HSSFDataValidation data_validation = new HSSFDataValidation(regions, constraint);

 

return data_validation;

}

 

添加到Sheet:

     //调用接口方法加载数据有效性            

         HSSFDataValidation data_validation =

                  SetDataValidation((short)3, (short)(cellIndex+1), (short)500,(short)(cellIndex+1));        

         aSheet.addValidationData(data_validation);                            


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值