.Net 基于MiniExcel的导入功能接口示例

csharp 复制代码
 /// <summary>
 /// 导入
 /// </summary>
 /// <param name="formFile"></param>
 /// <returns></returns>
 [HttpPost("Import")]
 public async Task<ExecResult> Import(IFormFile formFile)
 {
     try
     {
         if (formFile == null) throw new Exception("请选择文件导入!");
         var fileExt = Path.GetExtension(formFile.FileName);
         if (fileExt != ".xlsx") throw new Exception("请导入后缀名为.xlsx的Excel文件!");

         using var stream = new MemoryStream();
         formFile.CopyTo(stream);
         var maintainItems = new List<TpmMaintainItem>();
         var allItems = await _dbcontext.TpmMaintainItems.ToListAsync();
         var equipments = await _dbcontext.Equipment.ToListAsync();
         int num = 1;
         StringBuilder sb = new StringBuilder();
         foreach (IDictionary<string, object> row in stream.Query(true))
         {
             var equipmentCode = row["设备编码"];
             var name = row["维保项名称"];
             var cycle = row["维保项周期(天)"];
             var content = row["维保项内容"];
             var isMustCheck = row["是否必检"];
             var advanceDays = row["提前天数"];
             if (equipmentCode == null) sb.AppendLine($"第{num}行设备编码不存在!");
             if (name == null) sb.AppendLine($"第{num}行维保项名称不能为空!");
             if (cycle == null) sb.AppendLine($"第{num}行维保项周期(天)不能为空!");
             if (content == null) sb.AppendLine($"第{num}行维保项内容不能为空!");
             if (isMustCheck == null)
             {
                 sb.AppendLine($"第{num}行是否必检不能为空!");
             }
             else if(!(isMustCheck.ToString() == "是" || isMustCheck.ToString() == "否"))
             {
                 sb.AppendLine($"第{num}行是否必检为非法字符,只能填写是或否!");
             }
             if (advanceDays == null) sb.AppendLine($"第{num}行提前天数不能为空!");

             var equipment = equipments.FirstOrDefault(s=>s.EquipmentCode == equipmentCode.ToString());
             if(equipment == null) sb.AppendLine($"第{num}行设备编码:{equipmentCode}不存在!");
             var maintainItem = new TpmMaintainItem()
             {
                 EquipmentId = Convert.ToInt32(equipment?.EquipmentID),
                 Name = name.ToString(),
                 Cycle = Convert.ToInt32(cycle),
                 Content = content.ToString(),
                 IsMustCheck = string.Equals(isMustCheck.ToString(), "是") ? true : false,
                 AdvanceDays = Convert.ToInt32(advanceDays),
                 CreateTime = DateTime.Now
             };
             if (allItems.Any(s => s.Name == maintainItem.Name))
                 sb.AppendLine($"第{num}行数据已存在!");
             if(maintainItems.Any(s => s.Name == maintainItem.Name))
                 sb.AppendLine($"第{num}行数据重复!");
             maintainItems.Add(maintainItem);
             num++;
         }
         if(sb.Length > 0)
         {
             throw new Exception(sb.ToString());
         }
         else
         {
             await _dbcontext.TpmMaintainItems.AddRangeAsync(maintainItems);
             await _dbcontext.SaveChangesAsync();
         }

         return new ExecResult(true, "");
     }
     catch (Exception ex)
     {
         _logger.LogError(ex, "");
         return new ExecResult(false, "", ex.Message);
     }
 }
相关推荐
WineMonk13 小时前
.NET WPF CommunityToolkit.Mvvm框架
.net·wpf·mvvm
界面开发小八哥13 小时前
界面控件DevExpress WPF中文教程:Data Grid——卡片视图设置
.net·wpf·界面控件·devexpress·ui开发
九鼎科技-Leo1 天前
了解 .NET 运行时与 .NET 框架:基础概念与相互关系
windows·c#·.net
九鼎科技-Leo1 天前
什么是 ASP.NET Core?与 ASP.NET MVC 有什么区别?
windows·后端·c#·asp.net·mvc·.net
.net开发1 天前
WPF怎么通过RestSharp向后端发请求
前端·c#·.net·wpf
九鼎科技-Leo1 天前
在 C# 中,ICollection 和 IList 接口有什么区别?
windows·c#·.net
时光追逐者1 天前
C#/.NET/.NET Core学习路线集合,学习不迷路!
开发语言·学习·c#·asp.net·.net·.netcore·微软技术
Crazy Struggle1 天前
.NET 全功能流媒体管理控制接口平台
.net·开源项目·流媒体