Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Apache POI Excel:同一列中的多个数据验证_Excel_Apache_Validation_Apache Poi - Fatal编程技术网

Apache POI Excel:同一列中的多个数据验证

Apache POI Excel:同一列中的多个数据验证,excel,apache,validation,apache-poi,Excel,Apache,Validation,Apache Poi,我使用的是Poi版本3.16。我正在用Groovy编写代码。 我尝试在同一列中添加两个不同的数据验证。第一个被接受,但第二个失败。有人有办法做对吗?非常感谢 // Something like that FileOutputStream fileOut = new FileOutputStream("pois-test.xls"); def workbook = new XSSFWorkbook(); def sheet = workbook.createSheet("Sheet1"); /

我使用的是Poi版本3.16。我正在用Groovy编写代码。 我尝试在同一列中添加两个不同的数据验证。第一个被接受,但第二个失败。有人有办法做对吗?非常感谢

// Something like that
FileOutputStream fileOut = new FileOutputStream("pois-test.xls");
def workbook = new XSSFWorkbook();
def sheet = workbook.createSheet("Sheet1");

 // Create 2 Dropdownlists
addMyValidation(1, 10, 3, 3, new String[]{"One", "Two", "Three"});
addMyValidation(15, 25, 3, 3, new String[]{"A", "B", "C"});   // second one failed without errormessage

Boolean addMyValidation(int firstRow, int lastRow, int firstCol, int lastCol, String[] listOfValue)
{
        def helper     = new XSSFDataValidationHelper(sheet);
        def constraint = helper.createExplicitListConstraint(listOfValue);
        CellRangeAddressList range = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
        def validation = helper.createValidation(constraint, range);

        validation.setErrorStyle(ErrorStyle.STOP);
        validation.setSuppressDropDownArrow(true);
        validation.setEmptyCellAllowed(false);
        validation.setShowPromptBox(true);
        validation.setShowErrorBox(true);

        sheet.addValidationData(validation);

        return (true);
}        

我使用
Java
apachepoi
工作。但也应该使用
Groovy
代码

您可能不完全确定第15行的实际位置,因为行id和列id都是基于0的

以下完整的代码工作:

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddressList;

public class CreateExcelDataValidation {

 static Sheet sheet = null;

 static boolean addMyValidation(int firstRow, int lastRow, int firstCol, int lastCol, String[] listOfValue) {
  DataValidationHelper helper = sheet.getDataValidationHelper();
  DataValidationConstraint constraint = helper.createExplicitListConstraint(listOfValue);
  CellRangeAddressList range = new CellRangeAddressList(firstRow, lastRow, firstCol, lastCol);
  DataValidation validation = helper.createValidation(constraint, range);

  validation.setErrorStyle(DataValidation.ErrorStyle.STOP);
  validation.setSuppressDropDownArrow(true);
  validation.setEmptyCellAllowed(false);
  validation.setShowPromptBox(true);
  validation.setShowErrorBox(true);

  sheet.addValidationData(validation);

  return true;
 }        

 public static void main(String[] args) throws Exception {

  Workbook workbook = new XSSFWorkbook();
  sheet = workbook.createSheet("Data Validation");

  sheet.createRow(0).createCell(3).setCellValue("Col 3");
  sheet.createRow(1).createCell(0).setCellValue("Row 1");
  sheet.createRow(10).createCell(0).setCellValue("Row 10");
  sheet.createRow(15).createCell(0).setCellValue("Row 15");
  sheet.createRow(25).createCell(0).setCellValue("Row 25");

  addMyValidation(1, 10, 3, 3, new String[]{"One", "Two", "Three"});
  addMyValidation(15, 25, 3, 3, new String[]{"A", "B", "C"});

  FileOutputStream out = new FileOutputStream("CreateExcelDataValidation.xlsx");
  workbook.write(out);
  workbook.close();
  out.close();

 }

}

嗨,阿克塞尔。哇!你真是难以置信的快!!!谢谢。你是对的。错误发生在另一个地方。我用你的代码找到了。