diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisHBPOSaSeEdiCompareExportService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisHBPOSaSeEdiCompareExportService.cs index ca8a43a3..f5b11f1c 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisHBPOSaSeEdiCompareExportService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisHBPOSaSeEdiCompareExportService.cs @@ -194,7 +194,7 @@ namespace SettleAccount.Job.Services.Report // }; var saGroup = from sa in _settleAccountDbContext.Set() - where sa.BusinessType == businessType + where sa.BusinessType == businessType && sa.Version == version group sa by new { sa.PN, sa.CustomerPartCodeNoSpace } into groupItem select new { @@ -227,7 +227,7 @@ namespace SettleAccount.Job.Services.Report // }).Where(t => t.Qty != 0M); var seGroup = (from se in _settleAccountDbContext.Set() - where se.BusinessType == businessType + where se.BusinessType == businessType && se.State == 0 && se.IsDeleted == false group se by new { se.PN, se.CustomerPartCodeNoSpace } into groupItem select new { @@ -256,7 +256,7 @@ namespace SettleAccount.Job.Services.Report // LU = groupItem.Max(t => t.LU) // }; var ediGroup = from edi in _settleAccountDbContext.Set() - where edi.IsDeleted == false + where edi.IsDeleted == false && edi.State == 0 group edi by new { edi.PN, edi.CustomerPartCodeNoSpace } into groupItem select new { @@ -389,20 +389,35 @@ namespace SettleAccount.Job.Services.Report }; var saSeCompareFullJoin = saSeCompareLeft.Union(saSeCompareRight).ToList(); - var ediCheck= ediGroup.ToList(); - foreach (var item in saSeCompareFullJoin.Where(p=>p.SaCustomerPartCode!=null && p.SeCustomerPartCode==null)) - { - if(!string.IsNullOrEmpty(item.SaCustomerPartCode)&& string.IsNullOrEmpty(item.SeCustomerPartCode)) + //var ediCheck= ediGroup.ToList(); + //foreach (var item in saSeCompareFullJoin.Where(p=>p.SaCustomerPartCode!=null && p.SeCustomerPartCode==null)) + //{ + // if(!string.IsNullOrEmpty(item.SaCustomerPartCode)&& string.IsNullOrEmpty(item.SeCustomerPartCode)) + // { + // var ediEntity= ediCheck.FirstOrDefault(p=>p.PN== item.PN &&p.CustomerPartCodeNoSpace== item.SaCustomerPartCode); + // if(ediEntity!=null) + // { + // item.EdiCustomerPartCode = item.SaCustomerPartCode; + // item.EdiQty = ediEntity.Qty; + // } + // } + + //} + + //优化 + var haveSaNotSe = saSeCompareFullJoin.Where(p => !string.IsNullOrEmpty(p.SaCustomerPartCode) && string.IsNullOrEmpty(p.SeCustomerPartCode)).ToList(); + var ediCheck = ediGroup.ToList().ToDictionary(item => item.PN + item.LU, item => item.Qty); + foreach (var item in haveSaNotSe) + { + + if (ediCheck.TryGetValue(item.PN + item.SaCustomerPartCode, out var v)) { - var ediEntity= ediCheck.FirstOrDefault(p=>p.PN== item.PN &&p.CustomerPartCodeNoSpace== item.SaCustomerPartCode); - if(ediEntity!=null) - { - item.EdiCustomerPartCode = item.SaCustomerPartCode; - item.EdiQty = ediEntity.Qty; - } - } + item.EdiCustomerPartCode = item.SaCustomerPartCode; + item.EdiQty = v; + } } + return saSeCompareFullJoin; } #endregion diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/NopiDataToExcel.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/NopiDataToExcel.cs index 19c8b667..746ead7a 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/NopiDataToExcel.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/NopiDataToExcel.cs @@ -17,6 +17,7 @@ using NPOI.XSSF.UserModel; using SettleAccount.Bases; using Minio; using Win.Sfs.SettleAccount.Reports; +using NPOI.XWPF.UserModel; namespace SettleAccount.Job.Services.Report { @@ -36,7 +37,7 @@ namespace SettleAccount.Job.Services.Report /// 页签名称 /// 数据 /// 缓存文件行数 - public async Task WriteDataToExcelInParallel(string sheetName, List data, int chunkSize,string zipFileName, List sumdata) + public async Task WriteDataToExcelInParallel(string sheetName, List data, int chunkSize,string zipFileName, List sumdata) { var count = data.Count / 1000000 + ((data.Count % 1000000) > 0 ? 1 : 0); for (var i = 1; i <= count; i++) @@ -55,7 +56,7 @@ namespace SettleAccount.Job.Services.Report { var sumFileName = $"{sheetName}_汇总_{Guid.NewGuid()}.xlsx"; var fileinfogroup = new FileInfoGroup((sumFileName), $"{sheetName}_汇总"); - WriteDataToExcel(sumdata, sumFileName); + WriteSumDataToExcel(sumdata, sumFileName); excelFiles.Add(fileinfogroup); } @@ -113,6 +114,9 @@ namespace SettleAccount.Job.Services.Report public void WriteDataToExcel(List data, string fileName) { + // 每列列宽字典 + var dic = new Dictionary(); + IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet("数据"); var firist = data.FirstOrDefault(); @@ -125,7 +129,8 @@ namespace SettleAccount.Job.Services.Report foreach (var property in properties) { headerRow.CreateCell(columnCellIndex).SetCellValue(property.Key); - sheet.AutoSizeColumn(columnCellIndex); + //sheet.AutoSizeColumn(columnCellIndex); + dic.Add(columnCellIndex, Encoding.Default.GetBytes(headerRow.GetCell(columnCellIndex).StringCellValue).Length * 256 + 200); columnCellIndex++; } @@ -135,7 +140,7 @@ namespace SettleAccount.Job.Services.Report int cellIndex = 0; foreach (var property in item.GetType().GetProperties()) { - ICell cell = row.CreateCell(cellIndex++); + NPOI.SS.UserModel.ICell cell = row.CreateCell(cellIndex); var propertyType = property.PropertyType; if (propertyType == typeof(bool)) @@ -157,65 +162,138 @@ namespace SettleAccount.Job.Services.Report cell.SetCellValue(property.GetValue(item)?.ToString()); } - + int length = Encoding.Default.GetBytes(cell.StringCellValue).Length * 256 + 200; + length = length > 15000 ? 15000 : length; + + // 若比已存在列宽更宽则替换,Excel限制最大宽度为15000 + try + { + if (dic[cellIndex] < length) + { + dic[cellIndex] = length; + } + } + catch(Exception ex) + { + } + + cellIndex++; + } } + + for (int i = 0; i < dic.Count; i++) + { + sheet.SetColumnWidth(i, dic[i]); + } + using (FileStream fileStream = new FileStream(fileName, FileMode.Create)) { workbook.Write(fileStream); } } - public void MergeExcelFiles(string[] filePaths, string outputFilePath, string sheetName) + public void WriteSumDataToExcel(List data, string fileName) { - - IWorkbook workbook = new XSSFWorkbook(); - ISheet sheet = workbook.CreateSheet(sheetName); - - int rowIndex = 0; - foreach (string filePath in filePaths) + try { - using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) + // 每列列宽字典 + var dic = new Dictionary(); + + IWorkbook workbook = new XSSFWorkbook(); + ISheet sheet = workbook.CreateSheet("数据"); + var firist = data.FirstOrDefault(); + var properties = typeof(SaSeEdiCompareSumReport).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty) + .Where(o => o.GetAttributes().Any() || o.GetAttributes().Any()) + .ToDictionary(o => o.GetAttribute()?.DisplayName ?? o.GetAttribute()?.Name, o => o); + int columnCellIndex = 0; + int rowIndex = 1; + IRow headerRow = sheet.CreateRow(0); + foreach (var property in properties) { - IWorkbook sourceWorkbook = new XSSFWorkbook(fileStream); - ISheet sourceSheet = sourceWorkbook.GetSheetAt(0); + headerRow.CreateCell(columnCellIndex).SetCellValue(property.Key); + //sheet.AutoSizeColumn(columnCellIndex); + dic.Add(columnCellIndex, Encoding.Default.GetBytes(headerRow.GetCell(columnCellIndex).StringCellValue).Length * 256 + 200); + columnCellIndex++; - int sourceRowCount = sourceSheet.LastRowNum; - for (int i = 0; i <= sourceRowCount; i++) + } + foreach (var item in data) + { + IRow row = sheet.CreateRow(rowIndex++); + int cellIndex = 0; + foreach (var property in item.GetType().GetProperties()) { - IRow sourceRow = sourceSheet.GetRow(i); - if (sourceRow != null) + NPOI.SS.UserModel.ICell cell = row.CreateCell(cellIndex); + + var propertyType = property.PropertyType; + if (propertyType == typeof(bool)) + { + cell.SetCellValue((bool)property.GetValue(item)! ? "是" : "否"); + } + else if (propertyType.IsEnum) + { + var value = property.GetValue(item)?.ToString()?.Trim(); + var enumValue = (System.Enum.Parse(propertyType, value) as System.Enum); + var type = enumValue.GetType(); + var field = type.GetField(enumValue.ToString()); + + cell.SetCellValue(field.GetCustomAttribute()?.Name ?? field.Name); + + } + else if (propertyType.IsValueType) { - IRow targetRow = sheet.CreateRow(rowIndex++); - int cellCount = sourceRow.LastCellNum; - for (int j = 0; j < cellCount; j++) + try { - ICell sourceCell = sourceRow.GetCell(j); - if (sourceCell != null) - { - ICell targetCell = targetRow.CreateCell(j); - targetCell.SetCellValue(sourceCell.ToString()); - } + cell.SetCellValue(double.Parse(property.GetValue(item)?.ToString())); } + catch + { + cell.SetCellValue(property.GetValue(item)?.ToString()); + } + + } + else + { + cell.SetCellValue(property.GetValue(item)?.ToString()); + } + + try + { + int length = Encoding.Default.GetBytes(cell.StringCellValue).Length * 256 + 200; + length = length > 15000 ? 15000 : length; + + // 若比已存在列宽更宽则替换,Excel限制最大宽度为15000 + + if (dic[cellIndex] < length) + { + dic[cellIndex] = length; + } + } + catch (Exception ex) + { } + + cellIndex++; + } } - } - if (File.Exists(outputFilePath)) - { - using (FileStream fileStream = new FileStream(outputFilePath, FileMode.Open, FileAccess.ReadWrite)) + for (int i = 0; i < dic.Count; i++) { - workbook.Write(fileStream); + sheet.SetColumnWidth(i, dic[i]); } - } - else - { - using (FileStream fileStream = new FileStream(outputFilePath, FileMode.Create)) + + using (FileStream fileStream = new FileStream(fileName, FileMode.Create)) { workbook.Write(fileStream); } + + } + catch(Exception ex) + { + } + } } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SaSeEdiCompareExportBaseService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SaSeEdiCompareExportBaseService.cs index d83e50e5..89704b99 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SaSeEdiCompareExportBaseService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SaSeEdiCompareExportBaseService.cs @@ -322,7 +322,7 @@ namespace SettleAccount.Job.Services.Report nopiExcel.EndPoint = _cfg.GetValue("MinIO:EndPoint", "localhost:10684"); - var uploadResult = nopiExcel.WriteDataToExcelInParallel($"{businessTypeDisplayName}结算数据和发货对比", haveSaNotHaveSeExport, 500000, fileName, null).ConfigureAwait(false); + var uploadResult = nopiExcel.WriteDataToExcelInParallel($"{businessTypeDisplayName}结算数据和发货对比", haveSaNotHaveSeExport, 500000, fileName, haveSaHaveSeSumExports).ConfigureAwait(false); return fileName; #endregion