Browse Source

增加汇总表导出

master
zhouhongjun 4 months ago
parent
commit
2e21fe9cdc
  1. 37
      code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/JisHBPOSaSeEdiCompareExportService.cs
  2. 136
      code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/NopiDataToExcel.cs
  3. 2
      code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SaSeEdiCompareExportBaseService.cs

37
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<HBPO_SA_DETAIL>() var saGroup = from sa in _settleAccountDbContext.Set<HBPO_SA_DETAIL>()
where sa.BusinessType == businessType where sa.BusinessType == businessType && sa.Version == version
group sa by new { sa.PN, sa.CustomerPartCodeNoSpace } into groupItem group sa by new { sa.PN, sa.CustomerPartCodeNoSpace } into groupItem
select new select new
{ {
@ -227,7 +227,7 @@ namespace SettleAccount.Job.Services.Report
// }).Where(t => t.Qty != 0M); // }).Where(t => t.Qty != 0M);
var seGroup = (from se in _settleAccountDbContext.Set<HBPO_SE_DETAIL>() var seGroup = (from se in _settleAccountDbContext.Set<HBPO_SE_DETAIL>()
where se.BusinessType == businessType where se.BusinessType == businessType && se.State == 0 && se.IsDeleted == false
group se by new { se.PN, se.CustomerPartCodeNoSpace } into groupItem group se by new { se.PN, se.CustomerPartCodeNoSpace } into groupItem
select new select new
{ {
@ -256,7 +256,7 @@ namespace SettleAccount.Job.Services.Report
// LU = groupItem.Max(t => t.LU) // LU = groupItem.Max(t => t.LU)
// }; // };
var ediGroup = from edi in _settleAccountDbContext.Set<HBPO_SE_EDI>() var ediGroup = from edi in _settleAccountDbContext.Set<HBPO_SE_EDI>()
where edi.IsDeleted == false where edi.IsDeleted == false && edi.State == 0
group edi by new { edi.PN, edi.CustomerPartCodeNoSpace } into groupItem group edi by new { edi.PN, edi.CustomerPartCodeNoSpace } into groupItem
select new select new
{ {
@ -389,20 +389,35 @@ namespace SettleAccount.Job.Services.Report
}; };
var saSeCompareFullJoin = saSeCompareLeft.Union(saSeCompareRight).ToList(); var saSeCompareFullJoin = saSeCompareLeft.Union(saSeCompareRight).ToList();
var ediCheck= ediGroup.ToList(); //var ediCheck= ediGroup.ToList();
foreach (var item in saSeCompareFullJoin.Where(p=>p.SaCustomerPartCode!=null && p.SeCustomerPartCode==null)) //foreach (var item in saSeCompareFullJoin.Where(p=>p.SaCustomerPartCode!=null && p.SeCustomerPartCode==null))
{ //{
if(!string.IsNullOrEmpty(item.SaCustomerPartCode)&& string.IsNullOrEmpty(item.SeCustomerPartCode)) // 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)
{ {
var ediEntity= ediCheck.FirstOrDefault(p=>p.PN== item.PN &&p.CustomerPartCodeNoSpace== item.SaCustomerPartCode);
if(ediEntity!=null) if (ediCheck.TryGetValue(item.PN + item.SaCustomerPartCode, out var v))
{ {
item.EdiCustomerPartCode = item.SaCustomerPartCode; item.EdiCustomerPartCode = item.SaCustomerPartCode;
item.EdiQty = ediEntity.Qty; item.EdiQty = v;
}
} }
} }
return saSeCompareFullJoin; return saSeCompareFullJoin;
} }
#endregion #endregion

136
code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/NopiDataToExcel.cs

@ -17,6 +17,7 @@ using NPOI.XSSF.UserModel;
using SettleAccount.Bases; using SettleAccount.Bases;
using Minio; using Minio;
using Win.Sfs.SettleAccount.Reports; using Win.Sfs.SettleAccount.Reports;
using NPOI.XWPF.UserModel;
namespace SettleAccount.Job.Services.Report namespace SettleAccount.Job.Services.Report
{ {
@ -36,7 +37,7 @@ namespace SettleAccount.Job.Services.Report
/// <param name="sheetName">页签名称</param> /// <param name="sheetName">页签名称</param>
/// <param name="data">数据</param> /// <param name="data">数据</param>
/// <param name="chunkSize">缓存文件行数</param> /// <param name="chunkSize">缓存文件行数</param>
public async Task<string> WriteDataToExcelInParallel(string sheetName, List<T> data, int chunkSize,string zipFileName, List<T> sumdata) public async Task<string> WriteDataToExcelInParallel(string sheetName, List<T> data, int chunkSize,string zipFileName, List<SaSeEdiCompareSumReport> sumdata)
{ {
var count = data.Count / 1000000 + ((data.Count % 1000000) > 0 ? 1 : 0); var count = data.Count / 1000000 + ((data.Count % 1000000) > 0 ? 1 : 0);
for (var i = 1; i <= count; i++) for (var i = 1; i <= count; i++)
@ -55,7 +56,7 @@ namespace SettleAccount.Job.Services.Report
{ {
var sumFileName = $"{sheetName}_汇总_{Guid.NewGuid()}.xlsx"; var sumFileName = $"{sheetName}_汇总_{Guid.NewGuid()}.xlsx";
var fileinfogroup = new FileInfoGroup((sumFileName), $"{sheetName}_汇总"); var fileinfogroup = new FileInfoGroup((sumFileName), $"{sheetName}_汇总");
WriteDataToExcel(sumdata, sumFileName); WriteSumDataToExcel(sumdata, sumFileName);
excelFiles.Add(fileinfogroup); excelFiles.Add(fileinfogroup);
} }
@ -113,6 +114,9 @@ namespace SettleAccount.Job.Services.Report
public void WriteDataToExcel(List<T> data, string fileName) public void WriteDataToExcel(List<T> data, string fileName)
{ {
// 每列列宽字典
var dic = new Dictionary<int, int>();
IWorkbook workbook = new XSSFWorkbook(); IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet("数据"); ISheet sheet = workbook.CreateSheet("数据");
var firist = data.FirstOrDefault(); var firist = data.FirstOrDefault();
@ -125,7 +129,8 @@ namespace SettleAccount.Job.Services.Report
foreach (var property in properties) foreach (var property in properties)
{ {
headerRow.CreateCell(columnCellIndex).SetCellValue(property.Key); 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++; columnCellIndex++;
} }
@ -135,7 +140,7 @@ namespace SettleAccount.Job.Services.Report
int cellIndex = 0; int cellIndex = 0;
foreach (var property in item.GetType().GetProperties()) foreach (var property in item.GetType().GetProperties())
{ {
ICell cell = row.CreateCell(cellIndex++); NPOI.SS.UserModel.ICell cell = row.CreateCell(cellIndex);
var propertyType = property.PropertyType; var propertyType = property.PropertyType;
if (propertyType == typeof(bool)) if (propertyType == typeof(bool))
@ -157,65 +162,138 @@ namespace SettleAccount.Job.Services.Report
cell.SetCellValue(property.GetValue(item)?.ToString()); 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)) using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
{ {
workbook.Write(fileStream); workbook.Write(fileStream);
} }
} }
public void MergeExcelFiles(string[] filePaths, string outputFilePath, string sheetName) public void WriteSumDataToExcel(List<SaSeEdiCompareSumReport> data, string fileName)
{ {
try
{
// 每列列宽字典
var dic = new Dictionary<int, int>();
IWorkbook workbook = new XSSFWorkbook(); IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet(sheetName); ISheet sheet = workbook.CreateSheet("数据");
var firist = data.FirstOrDefault();
var properties = typeof(SaSeEdiCompareSumReport).GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty)
.Where(o => o.GetAttributes<ExporterHeaderAttribute>().Any() || o.GetAttributes<DisplayAttribute>().Any())
.ToDictionary(o => o.GetAttribute<ExporterHeaderAttribute>()?.DisplayName ?? o.GetAttribute<DisplayAttribute>()?.Name, o => o);
int columnCellIndex = 0;
int rowIndex = 1;
IRow headerRow = sheet.CreateRow(0);
foreach (var property in properties)
{
headerRow.CreateCell(columnCellIndex).SetCellValue(property.Key);
//sheet.AutoSizeColumn(columnCellIndex);
dic.Add(columnCellIndex, Encoding.Default.GetBytes(headerRow.GetCell(columnCellIndex).StringCellValue).Length * 256 + 200);
columnCellIndex++;
int rowIndex = 0; }
foreach (string filePath in filePaths) foreach (var item in data)
{ {
using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) IRow row = sheet.CreateRow(rowIndex++);
int cellIndex = 0;
foreach (var property in item.GetType().GetProperties())
{ {
IWorkbook sourceWorkbook = new XSSFWorkbook(fileStream); NPOI.SS.UserModel.ICell cell = row.CreateCell(cellIndex);
ISheet sourceSheet = sourceWorkbook.GetSheetAt(0);
int sourceRowCount = sourceSheet.LastRowNum; var propertyType = property.PropertyType;
for (int i = 0; i <= sourceRowCount; i++) if (propertyType == typeof(bool))
{ {
IRow sourceRow = sourceSheet.GetRow(i); cell.SetCellValue((bool)property.GetValue(item)! ? "是" : "否");
if (sourceRow != null) }
else if (propertyType.IsEnum)
{ {
IRow targetRow = sheet.CreateRow(rowIndex++); var value = property.GetValue(item)?.ToString()?.Trim();
int cellCount = sourceRow.LastCellNum; var enumValue = (System.Enum.Parse(propertyType, value) as System.Enum);
for (int j = 0; j < cellCount; j++) var type = enumValue.GetType();
var field = type.GetField(enumValue.ToString());
cell.SetCellValue(field.GetCustomAttribute<DisplayAttribute>()?.Name ?? field.Name);
}
else if (propertyType.IsValueType)
{ {
ICell sourceCell = sourceRow.GetCell(j); try
if (sourceCell != null)
{ {
ICell targetCell = targetRow.CreateCell(j); cell.SetCellValue(double.Parse(property.GetValue(item)?.ToString()));
targetCell.SetCellValue(sourceCell.ToString());
}
}
} }
catch
{
cell.SetCellValue(property.GetValue(item)?.ToString());
} }
} }
else
{
cell.SetCellValue(property.GetValue(item)?.ToString());
} }
if (File.Exists(outputFilePath)) try
{ {
using (FileStream fileStream = new FileStream(outputFilePath, FileMode.Open, FileAccess.ReadWrite)) int length = Encoding.Default.GetBytes(cell.StringCellValue).Length * 256 + 200;
length = length > 15000 ? 15000 : length;
// 若比已存在列宽更宽则替换,Excel限制最大宽度为15000
if (dic[cellIndex] < length)
{ {
workbook.Write(fileStream); dic[cellIndex] = length;
} }
} }
else catch (Exception ex)
{
}
cellIndex++;
}
}
for (int i = 0; i < dic.Count; i++)
{ {
using (FileStream fileStream = new FileStream(outputFilePath, FileMode.Create)) sheet.SetColumnWidth(i, dic[i]);
}
using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
{ {
workbook.Write(fileStream); workbook.Write(fileStream);
} }
} }
catch(Exception ex)
{
}
} }
} }

2
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"); 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; return fileName;
#endregion #endregion

Loading…
Cancel
Save