zoukankan      html  css  js  c++  java
  • POI设置excel添加列下拉框

    POI在生成excel模板时需要为列添加下拉框,我写了两个方法。

    @ 方法一:适用任何情况,不受下拉框值数量限制。但是需要通过引用其它列值。

    思路大概如下:

    1.创建一个隐藏的sheet页,用于存放下拉框的值。

    HSSFSheet listSheet = workBook.createSheet("ShtDictionary");//隐藏的sheet,用于存放下拉框的限定值

    2.将下拉框的值放置在隐藏sheet【ShtDictionary】某列

    //rows表示下拉框限定值的个数
    for
    (int i = 0 ;i<rows;i++){
    String cellValue = ...; listSheet.createRow(i).createCell(
    0).setCellValue(cellValue); }

    3.添加下拉框引用

    setHSSFValidation(HSSFSheet sheet,String sheetName, int lastRow,String col, int firstRow, int endRow, int firstCol, int endCol) 
    

      @ 自定义方法详解

     /** 
         * 设置某些列的值只能sheet中某列输入预制的数据,显示下拉框. 
         * @param HSSFSheet sheet 模板sheet页(需要设置下拉框的sheet) 
         * @param String sheetName 隐藏的sheet页,用于存放下拉框的值 (下拉框值对应一列)
         * @param int lastRow 存放下拉框值的最后一行
         * @param String col 存放下拉框值的列名 "A"
         * @param int firstRow  添加下拉框对应开始行 
         * @param int endRow    添加下拉框对应结束行 
         * @param int firstCol  添加下拉框对应开始列 
         * @param int endCol    添加下拉框对应结束列 
         * @return HSSFSheet 设置好的sheet. 
         */  
        public static HSSFSheet setHSSFValidation(HSSFSheet sheet,String sheetName, int lastRow,String col, int firstRow, int endRow, int firstCol, int endCol){  
             //设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列  
             CellRangeAddressList regions = new CellRangeAddressList(firstRow, endRow, firstCol, endCol);
             String cell = """+sheetName +"!$"+col+"$1:$"+col+"$"+lastRow+""";
             System.out.println("下拉框列:"+cell);
             // 这句话是关键 引用ShtDictionary 的单元格
             DVConstraint constraint = DVConstraint.createFormulaListConstraint("INDIRECT("+cell+ ")");
             HSSFDataValidation dataValidate = new HSSFDataValidation(regions, constraint);
             sheet.addValidationData(dataValidate);
             return sheet; 
        } 

    @ 方法二 :此方法仅仅使用下拉框值不超过232个的情况,优点是简单。

    1.将下拉框的值存放在自定义的一个数组对象中 String[] textList

    String[] textlist = ...;//下拉框显示的内容

     2.添加下拉框引用

    setHSSFValidation(HSSFSheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol);
    

    @ setHSSFValidation

        /** 
         * 设置某些列的值只能输入预制的数据,显示下拉框. 
         * @param HSSFSheet sheet  模板sheet页(需要设置下拉框的sheet)  
         * @param String[] textlist 下拉框显示的内容 
         * @param int firstRow  添加下拉框对应开始行 
         * @param int endRow    添加下拉框对应结束行 
         * @param int firstCol  添加下拉框对应开始列 
         * @param int endCol    添加下拉框对应结束列 
         * @return HSSFSheet 设置好的sheet. 
         */  
        public static HSSFSheet setHSSFValidation(HSSFSheet sheet, String[] textlist, int firstRow, int endRow, int firstCol, int endCol){  
            // 设置数据有效性加载在哪个单元格上,四个参数分别是:起始行、终止行、起始列、终止列  
            CellRangeAddressList regions = new CellRangeAddressList(firstRow,endRow, firstCol, endCol);  
            // 加载下拉列表内容  
            DVConstraint constraint = DVConstraint.createExplicitListConstraint(textlist);  
            // 数据有效性对象  
            HSSFDataValidation data_validation_list = new HSSFDataValidation(regions, constraint);  
            sheet.addValidationData(data_validation_list);  
            return sheet;  
        }  

     @ 数据有效性对象【HSSFDataValidation】的相关方法:

    // 设置提示内容,标题,内容
    void
    org.apache.poi.hssf.usermodel.HSSFDataValidation.createPromptBox(String title, String text);

  • 相关阅读:
    METHODS OF AND APPARATUS FOR USING TEXTURES IN GRAPHICS PROCESSING SYSTEMS
    Display controller
    Graphics processing architecture employing a unified shader
    Graphics-Processing Architecture Based on Approximate Rendering
    Architectures for concurrent graphics processing operations
    Procedural graphics architectures and techniques
    DYNAMIC CONTEXT SWITCHING BETWEEN ARCHITECTURALLY DISTINCT GRAPHICS PROCESSORS
    Thermal zone monitoring in an electronic device
    System and method for dynamically adjusting to CPU performance changes
    Framework for Graphics Animation and Compositing Operations
  • 原文地址:https://www.cnblogs.com/zhutouying/p/3447309.html
Copyright © 2011-2022 走看看