/// <summary> /// 上传 文件 /// </summary> /// <returns></returns> public async Task <JsonResult> FileSave(List <IFormFile> filesss) { var count = 0; var files = Request.Form.Files; long size = files.Sum(f => f.Length); string webRootPath = _hostingEnvironment.WebRootPath; string contentRootPath = _hostingEnvironment.ContentRootPath; Dictionary <string, double> dic = new Dictionary <string, double>(); foreach (var formFile in files) { if (formFile.Length > 0) { string fileExt = FileUtils.GetFileExt(formFile.FileName); //文件扩展名,不含“.” double fileSize = Math.Round(Convert.ToDouble(Convert.ToDouble(formFile.Length) / 1024 / 1024), 2); //获得文件大小,以字节为单位 string newFileName = formFile.FileName.Substring(0, formFile.FileName.LastIndexOf(".", StringComparison.Ordinal) - 1) + $"-{DateTime.Now:yyyy-MM-dd-HH-mm}." + fileExt; //随机生成新的文件名 var filePath = contentRootPath + "/upload/" + newFileName; using (var stream = new FileStream(filePath, FileMode.Create)) { await formFile.CopyToAsync(stream); } //读取Excel ----->DB var importDtos = new List <FixedAssetsDepreciationImportDto>(); #region 读取Excel FileInfo ff = new FileInfo(filePath); using (ExcelPackage package = new ExcelPackage(ff)) { var sheet = package.Workbook.Worksheets.ToList()[0]; importDtos = (from cell in sheet.Cells[sheet.Dimension.Address] where cell.Start.Row > 1 group cell by cell.Start.Row into rr select new FixedAssetsDepreciationImportDto() { CurrentRow = rr.Key, FixedAssetsCode = rr.ToList()[0].Value.ToString(), LineNum = rr.ToList()[1].Value == null ? (int?)null : Convert.ToInt32(rr.ToList()[1].Value), Period = rr.ToList()[2].Value.ToString(), DepartNamePath = rr.ToList()[3].Value.ToString(), Amount = rr.ToList()[4].Value == null ? (decimal?)null : Convert.ToDecimal(rr.ToList()[4].Value), CompanyName = rr.ToList()[5].Value.ToString(), }).ToList(); } #endregion #region 数据校验 //为空校验 var isNull = importDtos.Where(s => s.Amount == null || s.LineNum == null || string.IsNullOrEmpty(s.FixedAssetsCode) || string.IsNullOrEmpty(s.DepartNamePath) || string.IsNullOrEmpty(s.CompanyName) || string.IsNullOrEmpty(s.Period)) .Select(s => s.CurrentRow).Distinct().ToList(); if (isNull.Count > 0) { return(Json(new JsonFlag(0, "有空数据,行号:" + string.Join(',', isNull)))); } //多月数据校验 var nu = importDtos.Select(s => s.Period).Distinct(); if (nu.Count() > 1) { return(Json(new JsonFlag(0, "核算期间存在多月数据,如下:<br/>" + string.Join(",<br/>", nu)))); } //数据准确性校验 var excelDepartNames = importDtos.Select(s => s.DepartNamePath).Distinct(); var departMentsByVersion = _commonDataService.GetDepartMentsByVersion(importDtos.Select(s => s.Period).FirstOrDefault()).ToList(); var exceptDeparts = excelDepartNames.Except(departMentsByVersion.Select(s => s.NamePath));//不在 部门版本中的 部门名称 if (exceptDeparts.Any()) { return(Json(new JsonFlag(0, "部门名称无法找到对应的部门编号,如下:<br/>" + string.Join(",<br/>", exceptDeparts)))); } var excelCompanyNames = importDtos.Select(s => s.CompanyName).Distinct(); var companys = _commonDataService.CompanyInfos().ToList(); var exceptCompanys = excelCompanyNames.Except(companys.Select(s => s.CompanyName)); if (exceptCompanys.Any()) { return(Json(new JsonFlag(0, "签约公司无法找打对应的编号,如下:<br/>" + string.Join(",<br/>", exceptCompanys)))); } #endregion var histories = new List <FixedAssetsDepreciationHistory>(); foreach (var item in importDtos) { var model = new FixedAssetsDepreciationHistory() { FixedAssetsCode = item.FixedAssetsCode, LineNum = item.LineNum.Value, Period = item.Period, DepartID = departMentsByVersion.Where(s => s.NamePath == item.DepartNamePath).Select(s => s.DepartID).FirstOrDefault(), DepartNamePath = item.DepartNamePath, Amount = item.Amount.Value, CompanyCode = companys.Where(s => s.CompanyName == item.CompanyName).Select(s => s.CompanyCode).FirstOrDefault(), CompanyName = item.CompanyName, CreateTime = DateTime.Now, CreateUserID = Convert.ToInt32(User.Claims.FirstOrDefault(c => c.Type == "userId")?.Value), CreateUserName = User.Identity.Name, LastUpdateTime = DateTime.Now }; histories.Add(model); } count = _expenseService.AddFixedAssetsDepreciation(histories); dic.Add(newFileName, fileSize); } } var tem = dic.Select(s => new { newFileName = s.Key, size = s.Value }); return(Json(new JsonFlag(1, $"成功导入{count}条数据,到数据库", tem))); }