.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);
     }
 }
相关推荐
许泽宇的技术分享3 小时前
当AI Agent遇上.NET:微软Agent Framework的架构奥秘与实战启示
人工智能·microsoft·.net
SEO-狼术6 小时前
DevExpress DXperience Crack
.net
我是唐青枫9 小时前
一文理解 C#.NET Tuples:从基础到高级应用
c#·.net
缺点内向14 小时前
C# 中 Word 文档目录的插入与删除指南
开发语言·c#·word·.net
唐青枫15 小时前
告别 if-else:C#.NET 模式匹配让代码更优雅的正确方式
c#·.net
绿荫阿广1 天前
使用.NET开发并上线一个小智AI对话机器人的MCP服务转接平台
.net·asp.net core·mcp
棉晗榜1 天前
无法解析位于...\global.json 的 global.json 中指定的 .NET SDK 版本
.net
时光追逐者1 天前
C#/.NET/.NET Core技术前沿周刊 | 第 62 期(2025年11.17-11.23)
c#·.net·.netcore
宝桥南山1 天前
.NET 10 - Blazor web assembly应用的一些诊断方式
microsoft·微软·c#·asp.net·.net·.netcore
sz老兄闯1 天前
对 .NET FileSystemWatcher引发内存碎片化的 反思
.net