Apache POI Excel:同一列中的多个数据验证
我使用的是Poi版本3.16。我正在用Groovy编写代码。 我尝试在同一列中添加两个不同的数据验证。第一个被接受,但第二个失败。有人有办法做对吗?非常感谢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"); /
// 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();
}
}
嗨,阿克塞尔。哇!你真是难以置信的快!!!谢谢。你是对的。错误发生在另一个地方。我用你的代码找到了。