批量导入功能对于大部分后台系统来说都是不可或缺的一部分,常见的场景-基础数据的录入(部门,用户),用批量导入方便快捷。最近项目需要用到批量导入,决定花点时间写套比较通用的Excel导入功能。经过考虑,最终的实现需要达到

 

下面介绍各种List的sort的用法与比较

   
 1.不同业务导入无需考虑npoi相关操作,只需要关注自己的业务逻辑,这里的业务逻辑最重要的两点(数据校验和数据保存)

1.下载专业版:pycharm-professional-2017.3.3.exe

首先,我们建一个People的实体,有name、age、sex的属性,我们要排序的字段是年龄age

 
 2.导入异常(模板不匹配,数据填写错误…),提醒信息准确精细,达到帮助用户修正数据的目地
 

2.安装Pycharm

新建一个实体类

     在线体验地址:,最终实现效果

图片 1

    public class People
    {
        public string name { get; set; }
        public int age { get; set; }
        public string sex { get; set; }
    }

   
 导入成功
图片 2

图片 3

新建list的数据

 
 导入失败图片 4

 

            List<People> peoples = new List<People>()
            {
                new People() {age = 11, name="alun", sex = "男"},
                new People() {age=25, name = "陈敬桃", sex = "男"},
                new People() {age=9, name = "惠安", sex = "男"},
                new People() {age = 45, name = "小票", sex = "女"},
                new People() {age=3, name = "晓鸥", sex = "女"},
                new People() {age=70, name = "望谟", sex = "男"}
            };

阅读目录

图片 5

 

  • 设计流程图
  • 代码实现
  • npoi生成下拉框两种方式比较
  • 总结

图片 6

1. 第1种排序方法,使用 IComparer

回到顶部

等待安装完成

    public class PeopleAgeComparer : IComparer<People>
    {
        public int Compare(People p1, People p2)
        {
            return p1.age.CompareTo(p2.age);
        }
    }

peoples.Sort(new PeopleAgeComparer());

   本文使用的npoi版本:1.2.5,可以nuget下载相应包。系统相关流程和重要类的类图如下。

3.激活使用

可以看到第一种方法比价麻烦,要新建一个新的类来做

图片 7

  获取密钥

 

图片 8

如下:

 

    设计原则:

D87IQPUU3Q-eyJsaWNlbnNlSWQiOiJEODdJUVBVVTNRIiwibGljZW5zZWVOYW1lIjoiTnNzIEltIiwiYXNzaWduZWVOYW1lIjoiIiwiYXNzaWduZWVFbWFpbCI6IiIsImxpY2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4ICAQC9WZuYgQedSuOc5TOUSrRigMw4/+wuC5EtZBfvdl4HT/8vzMW/oUlIP4YCvA0XKyBaCJ2iX+ZCDKoPfiYXiaSiH+HxAPV6J79vvouxKrWg2XV6ShFtPLP+0gPdGq3x9R3+kJbmAm8w+FOdlWqAfJrLvpzMGNeDU14YGXiZ9bVzmIQbwrBA+c/F4tlK/DV07dsNExihqFoibnqDiVNTGombaU2dDup2gwKdL81ua8EIcGNExHe82kjF4zwfadHk3bQVvbfdAwxcDy4xBjs3L4raPLU3yenSzr/OEur1+jfOxnQSmEcMXKXgrAQ9U55gwjcOFKrgOxEdek/Sk1VfOjvS+nuM4eyEruFMfaZHzoQiuw4IqgGc45ohFH0UUyjYcuFxxDSU9lMCv8qdHKm+wnPRb0l9l5vXsCBDuhAGYD6ss+Ga+aDY6f/qXZuUCEUOH3QUNbbCUlviSz6+GiRnt1kA9N2Qachl+2yBfaqUqr8h7Z2gsx5LcIf5kYNsqJ0GavXTVyWh7PYiKX4bs354ZQLUwwa/cG++2+wNWP+HtBhVxMRNTdVhSm38AknZlD+PTAsWGu9GyLmhti2EnVwGybSD2Dxmhxk3IPCkhKAK+pl0eWYGZWG3tJ9mZ7SowcXLWDFAk0lRJnKGFMTggrWjV8GYpw5bq23VmIqqDLgkNzuoog==

2. 第2种排序方法,使用委托来排序

    1.通用操作与业务无关代码在基类里面实现

 

peoples.Sort(delegate (People p1, People p2) { return p1.age.CompareTo(p2.age); });

    2.对于个性化业务可以重写基类方法实现

 图片 9

看委托的方式很方便,不用新建类这么麻烦。

    开发流程:

4.字体调节

 

    1.制作Excel导入模版

图片 10

 

  2.添加继承ExcelImport业务导入类

 

3. 第2种排序方法,使用Lambda表达式来排序

    3.在ExcelImportMapper中添加枚举和该业务枚举对应模版路径地址

peoples.Sort( (a, b) => a.age.CompareTo(b.age) );

   
4.业务导入类重写Type,DictFields,SavaImportData,根据模版决定是否重写GetExportTemplate方法

 

 

 

回到顶部

 

 1.返回导入模版

发表评论

电子邮件地址不会被公开。 必填项已用*标注