Excel批量导入数据的数据验证

电脑杂谈  发布时间:2020-12-11 07:00:57  来源:网络整理

excel 批量导入 校验_excel 批量导入 校验

最近,我收到Excel的请求,要求批量导入数据。在这里,我要分享的是数据验证。至于如何读取Excel数据,可以将其下载到百度。

通常,当Excel批量导入数据时,我们将为客户提供一个固定的模板来输入我们期望的数据。但是,我们无法预测客户的运作。他们可以添加或删除列,或删除Excel中特定字段的输入限制。

Excel模板,可能只有一个或几个。每个模板的数据列要么相同,要么存在个性化列。

excel 批量导入 校验_excel 批量导入 校验

由于特定列中的数据不同,同一模板可能需要填充不同的数据列。

......

面对一系列不确定因素,编辑在这里提出了一个小解决方案。

excel 批量导入 校验_excel 批量导入 校验

我们可以定义这样的类:

 1      public class ExcelTemplate
 2     {
 3         /// 
 4         /// 列名
 5         /// 
 6         public string ColumnName { get; set; }
 7 
 8         /// 
 9         /// 是否必填
10         /// 
11         public bool IsRequire { get; set; }
12 
13         /// 
14         /// 列类型
15         /// 
16         public string ColumnType { get; set; }
17     }

有这样一个模板:

excel 批量导入 校验_excel 批量导入 校验

excel 批量导入 校验

创建标题类

1 var myExcelTemplate = new List();
2 myExcelTemplate.Add(new ExcelTemplate() { ColumnName = "序号", IsRequire = true, ColumnType = "string" });
3 myExcelTemplate.Add(new ExcelTemplate() { ColumnName = "姓名", IsRequire = true, ColumnType = "string" });
4 myExcelTemplate.Add(new ExcelTemplate() { ColumnName = "入职日期", IsRequire = true, ColumnType = "DateTime" });

excel 批量导入 校验_excel 批量导入 校验

如果Excel数据已成功读取

1 System.Data.DataTable excelData = GetDataFromExcel(excelPath);

这时,确认Excel可以是这样的~~

 1             //判断是否存在数据行
 2             if (excelData == null || excelData.Rows.Count <= 0)
 3             {
 4                 //不存在数据行
 5                 return;
 6             }
 7 
 8             //遍历所有行
 9             foreach (System.Data.DataRow row in excelData.Rows)
10             {
11                 //遍历列
12                 foreach (var item in myExcelTemplate)
13                 {
14                     //判断列合法性
15                     if (!excelData.Columns.Contains(item.ColumnName))
16                     {
17                         //列非法
18                         return;
19                     }
20 
21                     //判断列是否必填
22                     if (item.IsRequire && (row[item.ColumnName] == null || string.IsNullOrWhiteSpace(row[item.ColumnName].ToString())))
23                     {
24                         //空列
25                         return;
26                     }
27 
28                     //根据列类型尝试类型转换
29                     switch (item.ColumnType)
30                     {
31                         case "DateTime":
32                             {
33                                 if (!row[item.ColumnName].IsDateTime())
34                                 {
35                                     //类型转换失败
36                                     return;
37                                 }
38                                 break;
39                             }
40                     }
41 
42                     //...
43 
44                 }
45             }


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/shumachanpin/article-339648-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      热点图片
      拼命载入中...