我想使用EPPlus验证Excel中的列,时间范围如下午1:00到11:00。
时间必须是“hh:mm AM / PM”格式。
我解决了这个问题。这是代码:
var validationEndTime = workSheet.DataValidations.AddTimeValidation("H:H");
validationEndTime.ShowInputMessage = true;
validationEndTime.Prompt = "Add end time in hh:mm AM/PM format as example 9:00 AM or 12:30 PM";
validationEndTime.ErrorStyle = ExcelDataValidationWarningStyle.stop;
validationEndTime.ShowErrorMessage = true;
validationEndTime.Error = "Insert valid time";
validationStartDate.Operator = ExcelDataValidationOperator.between;
var timeEnd = validationStartDate.Formula.Value;
timeEnd.Hour = 00;
timeEnd.Minute = 00;
var timeEnd2 = validationStartDate.Formula2.Value;
timeEnd2.Hour = 23;
timeEnd2.Minute = 59;
validationEndTime.Formula.Value = timeEnd;
validationEndTime.Formula2.Value = timeEnd2;
这将在excel中进行验证,如下所示:
答案 0 :(得分:2)
我尝试了很多使DataValidations.AddDateTimeValidation工作,但它对我没有用。因此,我没有在C#中寻找解决方案,而是试图找出excel如何处理时间。
Excel以十进制数存储时间。所以我没有使用DataValidations.AddDateTimeValidation,而是使用了 DataValidations.AddDecimalValidation, 我将excel中的开始时间和结束时间转换为10位精度的十进制数。
所以12:00:00刚好变成0.00而11:59:59 pm变成0.999305555555556。
我的C#代码看起来像这样
var validdateTime = sheet1.DataValidations.AddDecimalValidation("H:H");
validdateTime.ShowInputMessage = true;
validdateTime.Prompt = "Add end time in hh:mm AM/PM format as example 9:00 AM or 12:30 PM";
validdateTime.AllowBlank = false;
validdateTime.ShowErrorMessage = true;
validdateTime.Error = "The time entered is not valid";
validdateTime.ErrorStyle = ExcelDataValidationWarningStyle.stop;
validdateTime.Operator = ExcelDataValidationOperator.between;
validdateTime.Formula.Value = 0.0;
validdateTime.Formula2.Value = 0.999305555555556;