POI 2003/2007 下拉列表

               

http://fruitking.iteye.com/blog/811931

Excel2007和Excel2003的部分功能菜单有所调整
比如2003的“插入-名称”,在2007中更为到“公式-定义的名称”
比如2003的“插入-名称-指定-首行”,在2007中更为到“公式-定义的名称-根据所选内容创建-首行”

Excel功能点应用:
相对位置和绝对位置,特别在某个行列的数据是参考另外某个行列的数据而变动的,相对位置的表示方法:A8、B9等等,绝对位置的表示方法:$A$8、$B$9(就是使用美元符号$)

隐藏页的数据引用。
2003中,假设sheet1是隐藏页,并先定义好数据(公式-定义的名称→定义;在sheet1中定义数据源名称:省份;引用位置:=Sheet1!$A$1:$A$5),然后在“添加”数据,设置数据的有效性选项(数据-数据有效性-序列;来源填写“=省份”)
2007中,假设sheet1是隐藏页,并先定义好数据(插入→名称→定义;在sheet1中定义数据源名称:省份;引用位置:=Sheet1!$A$1:$A$5),然后在“添加”数据,设置数据的有效性选项(数据-数据有效性-序列;来源填写“=省份”)

POI 2003/2007 下拉列表

POI 2003/2007 下拉列表

Java代码 POI 2003/2007 下拉列表 POI 2003/2007 下拉列表POI 2003/2007 下拉列表
  1. package com.fruitking.caipiao;   
  2.   
  3. import java.io.FileOutputStream;   
  4. import java.io.IOException;   
  5.   
  6. import org.apache.poi.hssf.usermodel.DVConstraint;   
  7. import org.apache.poi.hssf.usermodel.HSSFCell;   
  8. import org.apache.poi.hssf.usermodel.HSSFDataValidation;   
  9. import org.apache.poi.hssf.usermodel.HSSFRow;   
  10. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  11. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  12. import org.apache.poi.ss.usermodel.DataValidation;   
  13. import org.apache.poi.ss.usermodel.Name;   
  14. import org.apache.poi.ss.util.CellRangeAddressList;   
  15.   
  16. public class TestExcelSelect {   
  17.   
  18.        
  19.      public static void main(String [] args) throws IOException {     
  20.          HSSFWorkbook workbook = new HSSFWorkbook();//excel文件对象    
  21.          HSSFSheet userinfosheet1 = workbook.createSheet("用户信息表-1");//工作表对象  
  22.          HSSFSheet userinfosheet2 = workbook.createSheet("用户信息表-2");//工作表对象  
  23.          //创建一个隐藏页和隐藏数据集   
  24.          TestExcelSelect.creatHideSheet(workbook, "hideselectinfosheet");   
  25.          //设置名称数据集   
  26.          TestExcelSelect.creatExcelNameList(workbook);   
  27.          //创建一行数据   
  28.          TestExcelSelect.creatAppRow(userinfosheet1, "许果",1);   
  29.          TestExcelSelect.creatAppRow(userinfosheet1, "刘德华",2);   
  30.          TestExcelSelect.creatAppRow(userinfosheet1, "刘若英",3);   
  31.          TestExcelSelect.creatAppRow(userinfosheet2, "张学友",1);   
  32.          TestExcelSelect.creatAppRow(userinfosheet2, "林志玲",2);   
  33.          TestExcelSelect.creatAppRow(userinfosheet2, "林熙蕾",3);   
  34.             
  35.          //生成输入文件   
  36.          FileOutputStream out=new FileOutputStream("success.xls");     
  37.          workbook.write(out);     
  38.          out.close();   
  39.      }   
  40.         
  41.      /**  
  42.       * 名称管理  
  43.       * @param workbook  
  44.       */  
  45.      public static void creatExcelNameList(HSSFWorkbook workbook){   
  46.         //名称管理   
  47.          Name name;   
  48.          name = workbook.createName();   
  49.          name.setNameName("provinceInfo");   
  50.          name.setRefersToFormula("hideselectinfosheet!$A$1:$E$1");   
  51.          name = workbook.createName();   
  52.          name.setNameName("浙江");   
  53.          name.setRefersToFormula("hideselectinfosheet!$B$2:$K$2");   
  54.          name = workbook.createName();   
  55.          name.setNameName("山东");   
  56.          name.setRefersToFormula("hideselectinfosheet!$B$3:$I$3");   
  57.          name = workbook.createName();   
  58.          name.setNameName("江西");   
  59.          name.setRefersToFormula("hideselectinfosheet!$B$4:$E$4");   
  60.          name = workbook.createName();   
  61.          name.setNameName("江苏");   
  62.          name.setRefersToFormula("hideselectinfosheet!$B$5:$I$5");   
  63.          name = workbook.createName();   
  64.          name.setNameName("四川");   
  65.          name.setRefersToFormula("hideselectinfosheet!$B$6:$K$6");   
  66.      }   
  67.         
  68.         
  69.      /**  
  70.       * 创建隐藏页和数据域  
  71.       * @param workbook  
  72.       * @param hideSheetName  
  73.       */  
  74.      public static void creatHideSheet(HSSFWorkbook workbook,String hideSheetName){   
  75.          HSSFSheet hideselectinfosheet = workbook.createSheet(hideSheetName);//隐藏一些信息  
  76.          //设置下拉列表的内容     
  77.          String[] provinceList = {"浙江","山东","江西","江苏","四川"};   
  78.          String[] zjProvinceList = {"浙江","杭州","宁波","温州","台州","绍兴","金华","湖州","丽水","衢州","舟山"};   
  79.          String[] sdProvinceList = {"山东","济南","青岛","烟台","东营","菏泽","淄博","济宁","威海"};   
  80.          String[] jxProvinceList = {"江西","南昌","新余","鹰潭","抚州"};   
  81.          String[] jsProvinceList = {"江苏","南京","苏州","无锡","常州","南通","泰州","连云港","徐州"};   
  82.          String[] scProvinceList = {"四川","成都","绵阳","自贡","泸州","宜宾","攀枝花","广安","达州","广元","遂宁"};   
  83.          //在隐藏页设置选择信息   
  84.          HSSFRow provinceRow = hideselectinfosheet.createRow(0);   
  85.          TestExcelSelect.creatRow(provinceRow, provinceList);   
  86.          HSSFRow zjProvinceRow = hideselectinfosheet.createRow(1);   
  87.          TestExcelSelect.creatRow(zjProvinceRow, zjProvinceList);   
  88.          HSSFRow sdProvinceRow = hideselectinfosheet.createRow(2);   
  89.          TestExcelSelect.creatRow(sdProvinceRow, sdProvinceList);   
  90.          HSSFRow jxProvinceRow = hideselectinfosheet.createRow(3);   
  91.          TestExcelSelect.creatRow(jxProvinceRow, jxProvinceList);   
  92.          HSSFRow jsProvinceRow = hideselectinfosheet.createRow(4);   
  93.          TestExcelSelect.creatRow(jsProvinceRow, jsProvinceList);   
  94.          HSSFRow scProvinceRow = hideselectinfosheet.createRow(5);   
  95.          TestExcelSelect.creatRow(scProvinceRow, scProvinceList);   
  96.          //设置隐藏页标志   
  97.          workbook.setSheetHidden(workbook.getSheetIndex(hideSheetName), true);   
  98.      }   
  99.         
  100.      /**  
  101.       * 创建一列应用数据  
  102.       * @param userinfosheet1  
  103.       * @param userName  
  104.       */  
  105.      public static void creatAppRow(HSSFSheet userinfosheet1,String userName,int naturalRowIndex){   
  106.         //构造一个信息输入表单,用户姓名,出生省份,出生城市   
  107.          //要求省份是可以下拉选择的,出生城市根据所选择的省份级联下拉选择  
  108.          //在第一行第一个单元格,插入下拉框   
  109.          HSSFRow row = userinfosheet1.createRow(naturalRowIndex-1);   
  110.          HSSFCell userNameLableCell = row.createCell(0);   
  111.          userNameLableCell.setCellValue("用户姓名:");   
  112.          HSSFCell userNameCell = row.createCell(1);   
  113.          userNameCell.setCellValue(userName);   
  114.          HSSFCell provinceLableCell = row.createCell(2);   
  115.          provinceLableCell.setCellValue("出生省份:");   
  116.          HSSFCell provinceCell = row.createCell(3);   
  117.          provinceCell.setCellValue("请选择");   
  118.          HSSFCell cityLableCell = row.createCell(4);   
  119.          cityLableCell.setCellValue("出生城市:");   
  120.          HSSFCell cityCell = row.createCell(5);   
  121.          cityCell.setCellValue("请选择");   
  122.             
  123.          //得到验证对象     
  124.          DataValidation data_validation_list = TestExcelSelect.getDataValidationByFormula("provinceInfo",naturalRowIndex,4);   
  125.          //工作表添加验证数据     
  126.          userinfosheet1.addValidationData(data_validation_list);   
  127.          DataValidation data_validation_list2 = TestExcelSelect.getDataValidationByFormula("INDIRECT($D"+naturalRowIndex+")",naturalRowIndex,6);   
  128.          //工作表添加验证数据     
  129.          userinfosheet1.addValidationData(data_validation_list2);   
  130.      }   
  131.         
  132.      /**  
  133.       * 创建一列数据  
  134.       * @param currentRow  
  135.       * @param textList  
  136.       */  
  137.      public static void creatRow(HSSFRow currentRow,String[] textList){   
  138.          if(textList!=null&&textList.length>0){   
  139.              int i = 0;   
  140.              for(String cellValue : textList){   
  141.                  HSSFCell userNameLableCell = currentRow.createCell(i++);   
  142.                  userNameLableCell.setCellValue(cellValue);   
  143.              }   
  144.          }   
  145.      }   
  146.         
  147.      /**  
  148.       * 对Excel自然行列设置一个数据验证(并出现下拉列表选择格式)  
  149.       * @param selectTextList  
  150.       * @param naturalRowIndex  
  151.       * @param naturalColumnIndex  
  152.       * @return  
  153.       */  
  154.      public static DataValidation getDataValidationList(String[] selectTextList,int naturalRowIndex,int naturalColumnIndex){   
  155.          //加载下拉列表内容     
  156.          DVConstraint constraint = DVConstraint.createExplicitListConstraint(selectTextList);   
  157.          //设置数据有效性加载在哪个单元格上。     
  158.          //四个参数分别是:起始行、终止行、起始列、终止列     
  159.          int firstRow = naturalRowIndex-1;   
  160.          int lastRow = naturalRowIndex-1;   
  161.          int firstCol = naturalColumnIndex-1;   
  162.          int lastCol = naturalColumnIndex-1;   
  163.          CellRangeAddressList regions=new CellRangeAddressList(firstRow,lastRow,firstCol,lastCol);     
  164.          //数据有效性对象   
  165.          DataValidation data_validation_list = new HSSFDataValidation(regions,constraint);     
  166.          return data_validation_list;     
  167.      }   
  168.         
  169.      /**  
  170.       * 使用已定义的数据源方式设置一个数据验证  
  171.       * @param formulaString  
  172.       * @param naturalRowIndex  
  173.       * @param naturalColumnIndex  
  174.       * @return  
  175.       */  
  176.      public static DataValidation getDataValidationByFormula(String formulaString,int naturalRowIndex,int naturalColumnIndex){   
  177.          //加载下拉列表内容     
  178.          DVConstraint constraint = DVConstraint.createFormulaListConstraint(formulaString);    
  179.          //设置数据有效性加载在哪个单元格上。     
  180.          //四个参数分别是:起始行、终止行、起始列、终止列     
  181.          int firstRow = naturalRowIndex-1;   
  182.          int lastRow = naturalRowIndex-1;   
  183.          int firstCol = naturalColumnIndex-1;   
  184.          int lastCol = naturalColumnIndex-1;   
  185.          CellRangeAddressList regions=new CellRangeAddressList(firstRow,lastRow,firstCol,lastCol);     
  186.          //数据有效性对象    
  187.          DataValidation data_validation_list = new HSSFDataValidation(regions,constraint);   
  188.          return data_validation_list;     
  189.      }   
  190. }  
package com.fruitking.caipiao;import java.io.FileOutputStream;import java.io.IOException;import org.apache.poi.hssf.usermodel.DVConstraint;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFDataValidation;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.DataValidation;import org.apache.poi.ss.usermodel.Name;import org.apache.poi.ss.util.CellRangeAddressList;public class TestExcelSelect {   public static void main(String [] args) throws IOException {           HSSFWorkbook workbook = new HSSFWorkbook();//excel文件对象           HSSFSheet userinfosheet1 = workbook.createSheet("用户信息表-1");//工作表对象         HSSFSheet userinfosheet2 = workbook.createSheet("用户信息表-2");//工作表对象         //创建一个隐藏页和隐藏数据集         TestExcelSelect.creatHideSheet(workbook, "hideselectinfosheet");         //设置名称数据集         TestExcelSelect.creatExcelNameList(workbook);         //创建一行数据         TestExcelSelect.creatAppRow(userinfosheet1, "许果",1);         TestExcelSelect.creatAppRow(userinfosheet1, "刘德华",2);         TestExcelSelect.creatAppRow(userinfosheet1, "刘若英",3);         TestExcelSelect.creatAppRow(userinfosheet2, "张学友",1);         TestExcelSelect.creatAppRow(userinfosheet2, "林志玲",2);         TestExcelSelect.creatAppRow(userinfosheet2, "林熙蕾",3);                  //生成输入文件         FileOutputStream out=new FileOutputStream("success.xls");           workbook.write(out);           out.close();     }    /**   * 名称管理   * @param workbook   */  public static void creatExcelNameList(HSSFWorkbook workbook){  //名称管理         Name name;         name = workbook.createName();         name.setNameName("provinceInfo");         name.setRefersToFormula("hideselectinfosheet!$A$1:$E$1");         name = workbook.createName();         name.setNameName("浙江");         name.setRefersToFormula("hideselectinfosheet!$B$2:$K$2");         name = workbook.createName();         name.setNameName("山东");         name.setRefersToFormula("hideselectinfosheet!$B$3:$I$3");         name = workbook.createName();         name.setNameName("江西");         name.setRefersToFormula("hideselectinfosheet!$B$4:$E$4");         name = workbook.createName();         name.setNameName("江苏");         name.setRefersToFormula("hideselectinfosheet!$B$5:$I$5");         name = workbook.createName();         name.setNameName("四川");         name.setRefersToFormula("hideselectinfosheet!$B$6:$K$6");  }      /**   * 创建隐藏页和数据域   * @param workbook   * @param hideSheetName   */  public static void creatHideSheet(HSSFWorkbook workbook,String hideSheetName){   HSSFSheet hideselectinfosheet = workbook.createSheet(hideSheetName);//隐藏一些信息         //设置下拉列表的内容           String[] provinceList = {"浙江","山东","江西","江苏","四川"};         String[] zjProvinceList = {"浙江","杭州","宁波","温州","台州","绍兴","金华","湖州","丽水","衢州","舟山"};         String[] sdProvinceList = {"山东","济南","青岛","烟台","东营","菏泽","淄博","济宁","威海"};         String[] jxProvinceList = {"江西","南昌","新余","鹰潭","抚州"};         String[] jsProvinceList = {"江苏","南京","苏州","无锡","常州","南通","泰州","连云港","徐州"};         String[] scProvinceList = {"四川","成都","绵阳","自贡","泸州","宜宾","攀枝花","广安","达州","广元","遂宁"};         //在隐藏页设置选择信息         HSSFRow provinceRow = hideselectinfosheet.createRow(0);         TestExcelSelect.creatRow(provinceRow, provinceList);         HSSFRow zjProvinceRow = hideselectinfosheet.createRow(1);         TestExcelSelect.creatRow(zjProvinceRow, zjProvinceList);         HSSFRow sdProvinceRow = hideselectinfosheet.createRow(2);         TestExcelSelect.creatRow(sdProvinceRow, sdProvinceList);         HSSFRow jxProvinceRow = hideselectinfosheet.createRow(3);         TestExcelSelect.creatRow(jxProvinceRow, jxProvinceList);         HSSFRow jsProvinceRow = hideselectinfosheet.createRow(4);         TestExcelSelect.creatRow(jsProvinceRow, jsProvinceList);         HSSFRow scProvinceRow = hideselectinfosheet.createRow(5);         TestExcelSelect.creatRow(scProvinceRow, scProvinceList);         //设置隐藏页标志         workbook.setSheetHidden(workbook.getSheetIndex(hideSheetName), true);  }    /**   * 创建一列应用数据   * @param userinfosheet1   * @param userName   */  public static void creatAppRow(HSSFSheet userinfosheet1,String userName,int naturalRowIndex){  //构造一个信息输入表单,用户姓名,出生省份,出生城市         //要求省份是可以下拉选择的,出生城市根据所选择的省份级联下拉选择         //在第一行第一个单元格,插入下拉框         HSSFRow row = userinfosheet1.createRow(naturalRowIndex-1);         HSSFCell userNameLableCell = row.createCell(0);         userNameLableCell.setCellValue("用户姓名:");         HSSFCell userNameCell = row.createCell(1);         userNameCell.setCellValue(userName);         HSSFCell provinceLableCell = row.createCell(2);         provinceLableCell.setCellValue("出生省份:");         HSSFCell provinceCell = row.createCell(3);         provinceCell.setCellValue("请选择");         HSSFCell cityLableCell = row.createCell(4);         cityLableCell.setCellValue("出生城市:");         HSSFCell cityCell = row.createCell(5);         cityCell.setCellValue("请选择");                  //得到验证对象           DataValidation data_validation_list = TestExcelSelect.getDataValidationByFormula("provinceInfo",naturalRowIndex,4);         //工作表添加验证数据           userinfosheet1.addValidationData(data_validation_list);         DataValidation data_validation_list2 = TestExcelSelect.getDataValidationByFormula("INDIRECT($D"+naturalRowIndex+")",naturalRowIndex,6);         //工作表添加验证数据           userinfosheet1.addValidationData(data_validation_list2);  }    /**   * 创建一列数据   * @param currentRow   * @param textList   */  public static void creatRow(HSSFRow currentRow,String[] textList){   if(textList!=null&&textList.length>0){    int i = 0;    for(String cellValue : textList){     HSSFCell userNameLableCell = currentRow.createCell(i++);           userNameLableCell.setCellValue(cellValue);    }   }  }       /**   * 对Excel自然行列设置一个数据验证(并出现下拉列表选择格式)   * @param selectTextList   * @param naturalRowIndex   * @param naturalColumnIndex   * @return   */     public static DataValidation getDataValidationList(String[] selectTextList,int naturalRowIndex,int naturalColumnIndex){         //加载下拉列表内容        DVConstraint constraint = DVConstraint.createExplicitListConstraint(selectTextList);         //设置数据有效性加载在哪个单元格上。           //四个参数分别是:起始行、终止行、起始列、终止列           int firstRow = naturalRowIndex-1;         int lastRow = naturalRowIndex-1;         int firstCol = naturalColumnIndex-1;         int lastCol = naturalColumnIndex-1;         CellRangeAddressList regions=new CellRangeAddressList(firstRow,lastRow,firstCol,lastCol);           //数据有效性对象         DataValidation data_validation_list = new HSSFDataValidation(regions,constraint);           return data_validation_list;       }          /**      * 使用已定义的数据源方式设置一个数据验证      * @param formulaString      * @param naturalRowIndex      * @param naturalColumnIndex      * @return      */     public static DataValidation getDataValidationByFormula(String formulaString,int naturalRowIndex,int naturalColumnIndex){         //加载下拉列表内容        DVConstraint constraint = DVConstraint.createFormulaListConstraint(formulaString);          //设置数据有效性加载在哪个单元格上。           //四个参数分别是:起始行、终止行、起始列、终止列           int firstRow = naturalRowIndex-1;         int lastRow = naturalRowIndex-1;         int firstCol = naturalColumnIndex-1;         int lastCol = naturalColumnIndex-1;         CellRangeAddressList regions=new CellRangeAddressList(firstRow,lastRow,firstCol,lastCol);           //数据有效性对象          DataValidation data_validation_list = new HSSFDataValidation(regions,constraint);         return data_validation_list;       }}

===============

http://ttaale.iteye.com/blog/836306

Java代码 POI 2003/2007 下拉列表 POI 2003/2007 下拉列表POI 2003/2007 下拉列表
  1. import java.io.FileOutputStream;   
  2. import java.util.Date;   
  3.   
  4. import org.apache.poi.hssf.usermodel.DVConstraint;   
  5. import org.apache.poi.hssf.usermodel.HSSFCell;   
  6. import org.apache.poi.hssf.usermodel.HSSFDataValidation;   
  7. import org.apache.poi.hssf.usermodel.HSSFRow;   
  8. import org.apache.poi.hssf.usermodel.HSSFSheet;   
  9. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  10. import org.apache.poi.hssf.util.CellRangeAddressList;   
  11.   
  12. public class POI   
  13. {   
  14.  public static void main(String [] args)   
  15.  {   
  16.   String [] list={"东软","华信","SAP","海辉"};   
  17.   new POI().createListBox(list);   
  18.   return;   
  19.  }   
  20.  public void createListBox (String [] list)   
  21.  {   
  22.   //文件初始化   
  23.   HSSFWorkbook wb = new HSSFWorkbook();   
  24.      HSSFSheet sheet = wb.createSheet("new sheet");   
  25.        
  26.      //在第一行第一个单元格,插入下拉框   
  27.      HSSFRow row = sheet.createRow(0);   
  28.      HSSFCell cell = row.createCell(0);   
  29.        
  30.      //普通写入操作   
  31.      cell.setCellValue("请选择");//这是实验  
  32.   
  33.      //生成下拉列表   
  34.        
  35.      //只对(0,0)单元格有效   
  36.      CellRangeAddressList regions = new CellRangeAddressList(0,0,0,0);   
  37.        
  38.      //生成下拉框内容   
  39.      DVConstraint constraint = DVConstraint.createExplicitListConstraint(list);   
  40.        
  41.      //绑定下拉框和作用区域   
  42.      HSSFDataValidation data_validation = new HSSFDataValidation(regions,constraint);    
  43.        
  44.      //对sheet页生效   
  45.      sheet.addValidationData(data_validation);   
  46.   
  47.      //写入文件   
  48.      FileOutputStream fileOut;   
  49.   try {   
  50.    fileOut = new FileOutputStream("workbook.xls");   
  51.    wb.write(fileOut);   
  52.    fileOut.close();   
  53.   } catch (Exception e) {   
  54.    // TODO Auto-generated catch block   
  55.    e.printStackTrace();   
  56.   }   
  57.      
  58.   //结束   
  59.   System.out.println("Over");   
  60.  }   
  61. }  ==============

http://www.iteye.com/problems/65191

谁使用过POI3.8 设置EXCEL2007的数据有效性

------------------------------------------------------------------------------------------------------------------
问题补充:

hudingchen 写道
有问题吗? 我写个例子,poi3.8,excel2007好用
Java代码
  1. public static void main(String[] args) {   
  2.     FileOutputStream out = null;   
  3.     try {   
  4.         // excel对象   
  5.         HSSFWorkbook wb = new HSSFWorkbook();   
  6.         // sheet对象   
  7.         HSSFSheet sheet = wb.createSheet("sheet1");   
  8.         // 输出excel对象   
  9.         out = new FileOutputStream("C://aaa.xls");   
  10.         // 取得规则   
  11.         HSSFDataValidation validate = PoiTest.setValidate((short1,   
  12.                 (short1, (short1, (short1);   
  13.         // 设定规则   
  14.         sheet.addValidationData(validate);   
  15.         // 输出excel   
  16.         wb.write(out);   
  17.         out.close();   
  18.     } catch (FileNotFoundException e) {   
  19.         e.printStackTrace();   
  20.     } catch (IOException e) {   
  21.         e.printStackTrace();   
  22.     } finally {   
  23.         if (out != null) {   
  24.             try {   
  25.                 out.close();   
  26.             } catch (IOException e) {   
  27.                 // TODO Auto-generated catch block  
  28.                 e.printStackTrace();   
  29.             }   
  30.         }   
  31.     }   
  32. }   
  33.   
  34. public static HSSFDataValidation setValidate(short beginRow,   
  35.         short beginCol, short endRow, short endCol) {   
  36.     // 创建一个规则:1-100的数字   
  37.     DVConstraint constraint = DVConstraint.createNumericConstraint(   
  38.             DVConstraint.ValidationType.INTEGER,   
  39.             DVConstraint.OperatorType.BETWEEN, "1""100");   
  40.     // 设定在哪个单元格生效   
  41.     CellRangeAddressList regions = new CellRangeAddressList(beginRow,   
  42.             beginCol, endRow, endCol);   
  43.     // 创建规则对象   
  44.     HSSFDataValidation ret = new HSSFDataValidation(regions, constraint);   
  45.     return ret;   
  46. }  
 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");   // 取得规则   HSSFDataValidation validate = PoiTest.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");  // 设定在哪个单元格生效  CellRangeAddressList regions = new CellRangeAddressList(beginRow,    beginCol, endRow, endCol);  // 创建规则对象  HSSFDataValidation ret = new HSSFDataValidation(regions, constraint);  return ret; }



你这个生成的EXCEL格式是2003的,我在生成EXCEL2007的时候,在实例化XSSFDataValidation 时候,不知道怎么写了,你以前弄好2007格式的吗,能给我一个例子吗,谢谢

我之前的代码,在2007下是好用的,不过没有使用XSSF,我试着用XSSF写了一下,但是生成的excel文件的数据有效性没有加上,不过代码我觉得没有问题的,先看下
Java代码 POI 2003/2007 下拉列表 POI 2003/2007 下拉列表POI 2003/2007 下拉列表
  1. public static void main(String[] args) {   
  2.     FileOutputStream out = null;   
  3.     try {   
  4.         // excel对象   
  5.         XSSFWorkbook wb = new XSSFWorkbook();   
  6.         // sheet对象   
  7.         XSSFSheet sheet = wb.createSheet("sheet1");   
  8.         // 输出excel对象   
  9.         out = new FileOutputStream("C://aaa1.xls");   
  10.         // 取得规则   
  11.         DataValidation validate = PoiTest.setValidate(sheet, (short1,   
  12.                 (short1, (short1, (short1);   
  13.         // 设定规则   
  14.         sheet.addValidationData(validate);   
  15.         wb.write(out);   
  16.         out.close();   
  17.     } catch (FileNotFoundException e) {   
  18.         e.printStackTrace();   
  19.     } catch (IOException e) {   
  20.         e.printStackTrace();   
  21.     } finally {   
  22.         if (out != null) {   
  23.             try {   
  24.                 out.close();   
  25.             } catch (IOException e) {   
  26.                 // TODO Auto-generated catch block  
  27.                 e.printStackTrace();   
  28.             }   
  29.         }   
  30.     }   
  31. }   
  32.   
  33. public static DataValidation setValidate(XSSFSheet sheet, short beginRow,   
  34.         short beginCol, short endRow, short endCol) {   
  35.     XSSFDataValidationHelper helper = new XSSFDataValidationHelper(sheet);   
  36.     DataValidationConstraint constraint = helper.createNumericConstraint(   
  37.             ValidationType.INTEGER, OperatorType.BETWEEN, "1""100");   
  38.     CellRangeAddressList regions = new CellRangeAddressList(beginRow,   
  39.             beginCol, endRow, endCol);   
  40.     return helper.createValidation(constraint, regions);   
  41. }  
 public static void main(String[] args) {  FileOutputStream out = null;  try {   // excel对象   XSSFWorkbook wb = new XSSFWorkbook();   // sheet对象   XSSFSheet sheet = wb.createSheet("sheet1");   // 输出excel对象   out = new FileOutputStream("C://aaa1.xls");   // 取得规则   DataValidation validate = PoiTest.setValidate(sheet, (short) 1,     (short) 1, (short) 1, (short) 1);   // 设定规则   sheet.addValidationData(validate);   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 DataValidation setValidate(XSSFSheet sheet, short beginRow,   short beginCol, short endRow, short endCol) {  XSSFDataValidationHelper helper = new XSSFDataValidationHelper(sheet);  DataValidationConstraint constraint = helper.createNumericConstraint(    ValidationType.INTEGER, OperatorType.BETWEEN, "1", "100");  CellRangeAddressList regions = new CellRangeAddressList(beginRow,    beginCol, endRow, endCol);  return helper.createValidation(constraint, regions); }


后来我查了下文档,http://poi.apache.org/spreadsheet/quick-guide.html#Validation

其中有这么一句Currently - as of version 3.5 - the XSSF stream does not support data validations and neither it nor the HSSF stream allow data validations to be recovered from sheets
也就是说3.5中XSSF是不支持validations的,我怀疑3.8也还不支持,估计以后会修正吧,希望对你有帮助。           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow