【网络转载】poi数据有效性下拉列表

定义函数

    public HSSFDataValidation SetDataValidation(String[] textList, int firstRow, int endRow, int firstCol, int endCol) {  
        // 设置下拉列表的内容  
        log.info("起始行:" + firstRow + "___起始列:" + firstCol + "___终止行:" + endRow + "___终止列:" + endCol);  
        // 加载下拉列表内容  
        DVConstraint constraint = DVConstraint.createExplicitListConstraint(textList);  
        // DVConstraint constraint = new DVConstraint();  
        constraint.setExplicitListValues(textList);  
      
        // 设置数据有效性加载在哪个单元格上。  
        // 四个参数分别是:起始行、终止行、起始列、终止列  
        CellRangeAddressList regions = new CellRangeAddressList((short) firstRow, (short) endRow, (short) firstCol, (short) endCol);  
      
        // 数据有效性对象  
        HSSFDataValidation data_validation = new HSSFDataValidation(regions, constraint);  
      
        return data_validation;  
    }  

调用类

    import java.io.FileOutputStream;  
    import java.io.IOException;  
      
    import org.apache.poi.hssf.usermodel.HSSFSheet;  
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
      
    public class Test {  
        public static void main(String[] args) throws IOException {  
            HSSFWorkbook wb = new HSSFWorkbook();// excel文件对象  
            HSSFSheet sheetlist = wb.createSheet("sheetlist");// 工作表对象  
            FileOutputStream out = new FileOutputStream("success.xls");  
            String[] textList = { "空闲1", "已用2" };  
            sheetlist.addValidationData((new OracleToExcel()).SetDataValidation(textList, 1,2, 3, 4));  
            wb.write(out);  
            out.close();  
        }  
    }  

【文章转自

iekhnyqi的专栏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值