【网络转载】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的专栏

你可能感兴趣的:(工具类技术文章,JAVA相关文章,网络,validation,import,string,excel,class)