Skip to content

Commit

Permalink
支持对输入提示,不支持enum和bool类型的提示 dotnetcore#167
Browse files Browse the repository at this point in the history
  • Loading branch information
hueifeng committed Oct 29, 2020
1 parent 8d460b8 commit 14fd342
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,11 @@ public class ImporterHeaderAttribute : Attribute
/// <remarks>对于Excel数据验证,仅用于生成导入模板特性中,作为限制用户对Excel模板数据的约束性</remarks>
/// </summary>
public bool IsInterValidation { get; set; }

/// <summary>
/// 选定单元格时,显示输入的信息
/// <remarks>仅在IsInterValidation启用的情况下</remarks>
/// </summary>
public string ShowInputMessage { get; set; }
}
}
34 changes: 32 additions & 2 deletions src/Magicodes.ExporterAndImporter.Excel/Utility/ImportHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ protected virtual void StructureExcel(ExcelPackage excelPackage)
{
var range = ExcelCellBase.GetAddress(ExcelImporterSettings.HeaderRowIndex + 1, i + 1,
ExcelPackage.MaxRows, i + 1);
SetInterValidation(worksheet, ImporterHeaderInfos[i].PropertyInfo, range);
SetInterValidation(worksheet, ImporterHeaderInfos[i].PropertyInfo, range, ImporterHeaderInfos[i].Header.ShowInputMessage);
}
}

Expand All @@ -818,9 +818,10 @@ protected virtual void StructureExcel(ExcelPackage excelPackage)
/// <param name="worksheet"></param>
/// <param name="propertyInfo"></param>
/// <param name="address"></param>
/// <param name="showInputMessage"></param>
/// <remarks>在ResourceType为空的情况下,默认会选择属性本身的类型去做相应的处理
/// </remarks>
private void SetInterValidation(ExcelWorksheet worksheet, PropertyInfo propertyInfo, string address)
private void SetInterValidation(ExcelWorksheet worksheet, PropertyInfo propertyInfo, string address, string showInputMessage)
{
//MaxLength属性和MinLength属性可以去控制对指定列的大小
//StringLength属允许去指定小长度和最大长度,和max和min有有些类似,不过仅对string类型生效
Expand Down Expand Up @@ -878,6 +879,12 @@ private void SetInterValidation(ExcelWorksheet worksheet, PropertyInfo propertyI
textLengthValidation.Formula.Value = Convert.ToInt32(formulaVal);
textLengthValidation.Formula2.Value = Convert.ToInt32(formula2Val);
textLengthValidation.ShowErrorMessage = true;

if (!showInputMessage.IsNullOrWhiteSpace())
{
textLengthValidation.ShowInputMessage = true;
textLengthValidation.Prompt = showInputMessage;
}
}
else if (range != null)
{
Expand All @@ -890,6 +897,12 @@ private void SetInterValidation(ExcelWorksheet worksheet, PropertyInfo propertyI
intValidation.Formula2.Value = Convert.ToInt32(formula2Val);
intValidation.Error = errorMsg;
intValidation.ShowErrorMessage = true;

if (!showInputMessage.IsNullOrWhiteSpace())
{
intValidation.ShowInputMessage = true;
intValidation.Prompt = showInputMessage;
}
}
else if (type == typeof(DateTime))
{
Expand All @@ -899,8 +912,25 @@ private void SetInterValidation(ExcelWorksheet worksheet, PropertyInfo propertyI
dateTimeValidation.Operator = dataValidationOperator;
dateTimeValidation.Formula.Value = Convert.ToDateTime(formulaVal);
dateTimeValidation.Formula2.Value = Convert.ToDateTime(formula2Val);

if (!showInputMessage.IsNullOrWhiteSpace())
{
dateTimeValidation.ShowInputMessage = true;
dateTimeValidation.Prompt = showInputMessage;
}
}
}
else
{
if (!showInputMessage.IsNullOrWhiteSpace())
{
//如果仅启用数据验证属性,没有对数据大小的校验,则判断是否存在输入提示信息,enum和bool不支持
var anyValidation = worksheet.DataValidations.AddAnyValidation(address);
anyValidation.ShowInputMessage = true;
anyValidation.Prompt = showInputMessage;
}

}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Shouldly;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -117,7 +118,6 @@ public async Task GenerateTemplate_Test()
var result = await Importer.GenerateTemplate<ImportProductDto>(filePath);
result.ShouldNotBeNull();
File.Exists(filePath).ShouldBeTrue();

//TODO:读取Excel检查表头和格式
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class GenerateStudentImportSheetDataValidationDto
[Range(minimum: 18, maximum: 20, ErrorMessage = "年龄范围需要在18-20岁哦", ErrorMessageResourceType = typeof(string))]
public int IgnoreType { get; set; }

[ImporterHeader(Name = "出生日期", IsInterValidation = true)]
[ImporterHeader(Name = "出生日期", IsInterValidation = true, ShowInputMessage = "输入日期")]
[Range(typeof(DateTime), minimum: "2020-10-20", maximum: "2020-10-24", ErrorMessage = "日期范围超出了哦")]
public DateTime Birthday { get; set; }

Expand All @@ -64,7 +64,6 @@ public class GenerateStudentImportSheetDataValidationDto
/// <summary>
/// 性别
/// </summary>
[ImporterHeader(Name = "性别")]
[Required(ErrorMessage = "性别不能为空")]
[ValueMapping("男", 0)]
[ValueMapping("女", 1)]
Expand Down Expand Up @@ -110,8 +109,7 @@ public class GenerateStudentImportSheetDataValidationDto
/// <summary>
/// QQ
/// </summary>
[ImporterHeader(Name = "QQ号")]
[MaxLength(30, ErrorMessage = "QQ号字数超出最大限制,请修改!")]
[ImporterHeader(Name = "QQ号", IsInterValidation = true, ShowInputMessage = "性别列")]
public string QQ { get; set; }

/// <summary>
Expand Down

0 comments on commit 14fd342

Please sign in to comment.