poi 设置单元格 格式为 数据有效性


转自:http://www.iteye.com/problems/65041

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

import org.apache.poi.hssf.usermodel.HSSFDataValidation;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddressList;
 
public class PoiTest1{  
      
    public static void main(String[] args) {  
        FileOutputStream out = null;  
        try {  
            // excel对象  
            HSSFWorkbook wb = new HSSFWorkbook();  
            // sheet对象  

            HSSFSheet sheet = wb.createSheet("sheet1");  

            // 输出excel对象  
            out = new FileOutputStream("C://aaa.xls");  

            // 取得规则    第二行第二列

          //例如D6: 5,5,3,3 表示从第六行的第四列,到第六行的第四列   :可以参考二维数组就知道了。

            HSSFDataValidation validate = PoiTest1.setValidate((short) 1,  
                    (short) 1, (short) 1, (short) 1);  
            // 设定规则  
            sheet.addValidationData(validate);  
            // 输出excel  
            wb.write(out);  
            out.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            if (out != null) {  
                try {  
                    out.close();  
                } catch (IOException e) {  
                    // TODO Auto-generated catch block  
                    e.printStackTrace();  
                }  
            }  
        }  
    }  
      
    public static HSSFDataValidation setValidate(short beginRow,  
            short beginCol, short endRow, short endCol) {  
//        // 创建一个规则:1-100的数字  
//        DVConstraint constraint = DVConstraint.createNumericConstraint(  
//                DVConstraint.ValidationType.INTEGER,  
//                DVConstraint.OperatorType.BETWEEN, "1", "100");
        DVConstraint constraint = DVConstraint.createExplicitListConstraint(new String[]{"aa","bb","cc"});
        // 设定在哪个单元格生效  
        CellRangeAddressList regions = new CellRangeAddressList(beginRow,  
                beginCol, endRow, endCol);  
        // 创建规则对象  
        HSSFDataValidation ret = new HSSFDataValidation(regions, constraint);  
        return ret;  
    }  
      
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值