如何使用Apache POI生成xlsm下拉列表时设置限制

时间:2019-02-13 19:51:47

标签: java apache-poi

我正在尝试使用Apache POI为一个单元添加一个下拉列表。下拉列表包含300个字符串。

我下拉但没有打开。.它仅显示下拉符号。

代码:

try{
    String excelFilePath="ApplyTiresBatchUpload.xlsm";
    File f=new File(excelFilePath);
    System.out.println("FILE NAME:"+f.getName());
    System.out.println("FILE PATH:"+f.getCanonicalPath());
    FileInputStream fsIP = new FileInputStream(f);
    XSSFWorkbook wb = new XSSFWorkbook(fsIP);
    XSSFSheet worksheet = wb.getSheetAt(0);
    String[] exlist = new String[]{"1LI","1LO","1RI","1RO","2LI","2LO","2RI","2RO","3LI","3LO","3RI","3RO","4LI","4LO","4RI","4RO","5LI","5LO","5RI","5RO","3LI","3LO","3RI","3RO","4LI","4LO","4RI","4RO","5LI","5LO","5RI","5RO","1LI","1LO","1RI","1RO","2LI","2LO","2RI","2RO","3LI","3LO","3RI","3RO","4LI","4LO","4RI","4RO","5LI","5LO","5RI","5RO","3LI","3LO","3RI","3RO","4LI","4LO","4RI","4RO","5LI","5LO","5RI","5RO","1LI","1LO","1RI","1RO","2LI","2LO","2RI","2RO","3LI","3LO","3RI","3RO","4LI","4LO","4RI","4RO","5LI","5LO","5RI","5RO","3LI","3LO","3RI","3RO","4LI","4LO","4RI","4RO","5LI","5LO","5RI","5RO","1LI","1LO","1RI","1RO","2LI","2LO","2RI","2RO","3LI","3LO","3RI","3RO","4LI","4LO","4RI","4RO","5LI","5LO","5RI","5RO","3LI","3LO","3RI","3RO","4LI","4LO","4RI","4RO","5LI","5LO","5RI","5RO","1LI","1LO","1RI","1RO","2LI","2LO","2RI","2RO","3LI","3LO","3RI","3RO","4LI","4LO","4RI","4RO","5LI","5LO","5RI","5RO","3LI","3LO","3RI","3RO","4LI","4LO","4RI","4RO","5LI","5LO","5RI","5RO"};        
    System.out.println("exlist:-"+exlist.length);
    DataValidationHelper dataValidationHelper = worksheet.getDataValidationHelper();
    CellRangeAddressList  addressList = new  CellRangeAddressList(1, 20, 0, 0);
    DataValidationConstraint dataValidationConstraint =dataValidationHelper.createFormulaListConstraint("hidden!$A$1:$A$" + exlist.length);
    DataValidation dv = dataValidationHelper.createValidation(dataValidationConstraint, addressList);
    dv.setSuppressDropDownArrow(true);
   wb.setSheetHidden(1, true);      
   worksheet.addValidationData(dv);       
   response.setContentType("text/xlsm");
    response.addHeader("Content-Disposition", "attachment; filename=ApplyTiresBatchUpload.xlsm");
    ServletOutputStream outt = response.getOutputStream();
        wb.write(outt);
        outt.flush();
        outt.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch(Exception e){
    e.printStackTrace();
}

0 个答案:

没有答案