poi.apache.org网站提供以下定义
public static DVConstraint createDateConstraint(int comparisonOperator, java.lang.String expr1, java.lang.String expr2, java.lang.String dateFormat)
我想要一个示例,其中expr1和expr2是格式化的日期值。
答案 0 :(得分:1)
我发现的键是使用日期(年,月,日)功能。我还需要我的代码可用于除标题之外的整个列,因此单元格范围是1到excel一张工作表的最后一行。我不确定为什么但仅靠日期本身似乎不起作用,因为该公式会自动转换为在前面带有“ =”符号,这会混淆我认为的excel。
错误样式和消息始终是使人们了解自己在做什么错误的好主意。
DataValidationHelper dataValidationHelper = sheet.getDataValidationHelper();
DataValidationConstraint constraint = dataValidationHelper.createDateConstraint(OperatorType.BETWEEN,
"Date(1990, 1, 1)", "Date(9999, 12, 31)", "mm/dd/yyyy");
CellRangeAddressList addressList = new CellRangeAddressList(1, 1048575, 0, 0);
DataValidation validation = dataValidationHelper.createValidation(constraint, addressList);
validation.setErrorStyle(DataValidation.ErrorStyle.STOP);
validation.createErrorBox("Invalid date", "Please Insert Valid Date");
validation.setSuppressDropDownArrow(dropDownArrow);
sheet.addValidationData(validation);
答案 1 :(得分:0)
Apache POI附带的cunningly named TestDataValidation junit test中有一个示例,显示使用radius
:
$scope.init = function (){
console.log($scope.languageFilePath);
return $http.get($scope.languageFilePath) //languageFilePath contain url
.then(function(res){
console.log('scueese');
$scope.translate = res.data;
console.log($scope.translate.SERVICE);
});
};
$scope.init().then(function(){
//this code will be executed after the http get...
});
包括在约束上设置日期,显示所需的格式
答案 2 :(得分:0)
此代码对我有用,它可以将单元格样式设置为日期并在其上添加日期验证:
Cell cel= rowx.createCell(i);
cel.setCellStyle(style);
XSSFCreationHelper createHelper =
workbook.getCreationHelper();
short dateFormat =
createHelper.createDataFormat().getFormat("MM/dd/yyyy");
style.setDataFormat(dateFormat);
DataValidationHelper dvHelper =
sheet.getDataValidationHelper();
DataValidationConstraint dvConstraint =
dvHelper.createDateConstraint(
org.apache.poi.ss.usermodel.
DataValidationConstraint.OperatorType.BETWEEN,
"Date(1900, 1, 1)",
"Date(9999, 12, 31)",
"MM/dd/yyyy");
CellRangeAddressList addressList = new CellRangeAddressList(
2, pendingRowCount, columnNumber, columnNumber);
DataValidation validation =
dvHelper.createValidation(dvConstraint, addressList);
if (validation instanceof XSSFDataValidation) {
validation.setSuppressDropDownArrow(true);
validation.setShowErrorBox(true);
} else {
validation.setSuppressDropDownArrow(false);
}
sheet.addValidationData(validation);
答案 3 :(得分:0)
即使问题是针对JAVA的,也有些像我这样的C#开发人员在Google搜索后也来到了此页面,因此,在下面发布了答案的C#版本。
private void AddDateTypeValidation(IWorkbook workbook, ISheet sheet, int[] regions)
{
int _startRow = 1, _endRow = Int16.MaxValue, // Start from first row & continue upto end of Excel.
_startColumn = regions[0], _endColumn = regions[1]; // start column, end column
XSSFDataValidation dataValidation;
XSSFDataValidationConstraint dvConstraint;
XSSFDataValidationHelper validationHelper = new XSSFDataValidationHelper((XSSFSheet)sheet);
XSSFCellStyle dateCellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
dateCellStyle.SetDataFormat(HSSFDataFormat.GetBuiltinFormat("m/d/yy"));
CellRangeAddressList _regions = new CellRangeAddressList(_startRow, _endRow, _startColumn, _endColumn);
dvConstraint = (XSSFDataValidationConstraint)validationHelper.CreateDateConstraint(
OperatorType.BETWEEN, "Date(2000, 1, 1)", "Date(3000, 12, 31)", "m/d/yy");
dataValidation = (XSSFDataValidation)validationHelper.CreateValidation(dvConstraint, _regions);
dataValidation.ShowErrorBox = true;
dataValidation.SuppressDropDownArrow = true;
dataValidation.ErrorStyle = 0;
dataValidation.CreateErrorBox("Invalid Input Provided", "Please enter a valid date in format 'm/d/yy'.");
dataValidation.ShowErrorBox = true;
dataValidation.CreatePromptBox("Date Validation", "Enter a Valid Date in format 'm/d/yy'.");
dataValidation.ShowPromptBox = true;
sheet.AddValidationData(dataValidation);
sheet.SetDefaultColumnStyle(_startColumn, dateCellStyle);
sheet.AddValidationData(dataValidation);
}
答案 4 :(得分:-1)
我需要BETWEEN约束,我找不到任何可行的解决方案。最后我发现它相对简单:你应该使用第四个参数dateFormat中定义的格式提供expr1(from)和expr2(to):
dataValidationHelper.createDateConstraint(OperatorType.BETWEEN,
"1900-01-01", "2100-10-10", "yyyy-MM-dd");