| | |
| | | _unitOfWorkManage = unitOfWorkManage; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 读取导入文件的数据返回到前端 |
| | | /// </summary> |
| | | /// <param name="fileInput">文件</param> |
| | | /// <returns>返回读取结果,成功返回数据,失败返回错误信息</returns> |
| | | public WebResponseContent GetImportData(List<IFormFile> fileInput) |
| | | { |
| | | try |
| | | { |
| | | // 判断上传的文件是否为空 |
| | | if (fileInput == null || fileInput.Count == 0) |
| | | return new WebResponseContent { Status = true, Message = "请选择上传的文件" }; |
| | | // 获取上传的文件 |
| | | Microsoft.AspNetCore.Http.IFormFile formFile = fileInput[0]; |
| | | // 获取文件保存路径 |
| | | string dicPath = AppDomain.CurrentDomain.BaseDirectory + $"ExcelImprot/{DateTime.Now.ToString("yyyMMdd")}/{typeof(Dt_DeviceProtocol).Name}/"; |
| | | // 判断路径是否存在,不存在则创建 |
| | | if (!Directory.Exists(dicPath)) Directory.CreateDirectory(dicPath); |
| | | // 生成文件名 |
| | | string fileName = $"{Guid.NewGuid()}_{formFile.FileName}"; |
| | | // 获取文件保存路径 |
| | | dicPath = $"{dicPath}{fileName}"; |
| | | // 将文件保存到指定路径 |
| | | using (FileStream stream = new FileStream(dicPath, FileMode.Create)) |
| | | { |
| | | formFile.CopyTo(stream); |
| | | } |
| | | // 创建Excel导入器 |
| | | ExcelImporter importer = new ExcelImporter(); |
| | | // 导入Excel文件 |
| | | ImportResult<Dt_DeviceProtocol> importResult = importer.Import<Dt_DeviceProtocol>(dicPath, "").Result; |
| | | // 判断导入结果是否有错误 |
| | | if (importResult.HasError) |
| | | { |
| | | // 返回错误信息 |
| | | return WebResponseContent.Instance.Error(importResult.TemplateErrors.Serialize()); |
| | | } |
| | | // 返回导入结果 |
| | | return WebResponseContent.Instance.OK(data: importResult.Data); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | // 返回异常信息 |
| | | return WebResponseContent.Instance.Error(ex.Message); |
| | | } |
| | | } |