diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs index 20a05d9c..8e9abe6d 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Application/Entities/BQ/VmiAppService.cs @@ -1,10 +1,12 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Data.SqlClient; using System.Diagnostics; using System.Globalization; using System.IO; +using System.IO.Compression; using System.Linq; using System.Linq.Dynamic.Core; using System.Reflection; @@ -14,6 +16,7 @@ using AutoMapper; using ClosedXML.Excel; using Dapper; using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Spreadsheet; using DocumentFormat.OpenXml.Wordprocessing; using EFCore.BulkExtensions; using LinqToDB.EntityFrameworkCore; @@ -30,6 +33,8 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; using Omu.ValueInjecter; using Org.BouncyCastle.Asn1.Cmp; using RestSharp.Extensions; @@ -245,6 +250,13 @@ namespace Win.Sfs.SettleAccount.Entities.BQ input.Sorting= input.Sorting.ToUpper().Replace("BillTime".ToUpper(), "ReceiveTime".ToUpper()) .Replace("Qty".ToUpper(), "SumQty".ToUpper()); } + //var filterConditions = input.Filters.Where(p => p.Column == "SumQty"); + //foreach (var itm in filterConditions) + //{ + // if(itm) + // input.Filters.Add(new FilterCondition() { }); + //} + var first = input.Filters.FirstOrDefault(p => p.Column == "BillTime"); if (first != null) { @@ -278,6 +290,17 @@ namespace Win.Sfs.SettleAccount.Entities.BQ var result = await _exportImporter.ExcelExporter(dtos).ConfigureAwait(false); result.ShouldNotBeNull(); await _excelImportService.SaveBlobAsync(new SaveExcelImportInputDto { Name = fileName, Content = result }).ConfigureAwait(false); + + + + + // var content= ExtendExcel.WriteDataToExcelInParallel( "库存余额", dtos, 500000); + + + + + + return fileName; } @@ -2178,6 +2201,203 @@ namespace Win.Sfs.SettleAccount.Entities.BQ + // 扩展方法类,用于为IEnumerable类型添加额外功能 + public static class EnumerableExtensions + { + // 扩展方法,将源序列按指定大小分割成多个子序列 + public static IEnumerable> ChunkBy(this IEnumerable source, int chunkSize) + { + // 使用Select方法为序列中的每个元素添加索引和值 + return source + .Select((x, i) => new { Index = i, Value = x }) + // 根据索引除以块大小进行分组 + .GroupBy(x => x.Index / chunkSize) + // 从每个分组中选择值形成新的子序列 + .Select(x => x.Select(v => v.Value)); + } + } + + + public class FileInfoGroup + { + public FileInfoGroup(string fileName, string grpup) + { + FileName = fileName; + Grpup = grpup; + } + + public string FileName { get; set; } + public string Grpup { get; set; } + } + + public class ExtendExcel where T : class, new() + { + + static ConcurrentBag excelFiles = new ConcurrentBag(); + + /// + /// + /// + /// 页签名称 + /// 数据 + /// 缓存文件行数 + public static byte[] WriteDataToExcelInParallel(string sheetName, List data, int chunkSize) + { + Stopwatch sw = Stopwatch.StartNew(); + var count = data.Count / 1000000 + ((data.Count % 1000000) > 0 ? 1 : 0); + for (var i = 1; i <= count; i++) + { + var datas = data.Skip((i - 1) * 1000000).Take(1000000).ToList(); + var chunks = datas.ChunkBy(chunkSize); + Parallel.ForEach(chunks, chunk => + { + var fileName = $"{sheetName}_{i - 1}_{Guid.NewGuid()}.xlsx"; + var fileinfogroup = new FileInfoGroup((fileName), $"{sheetName}_{i - 1}"); + WriteDataToExcel(chunk.ToList(), fileName); + excelFiles.Add(fileinfogroup); + }); + } + string[] filePaths = excelFiles.Select(x => x.FileName).ToArray(); + + // 指定ZIP文件的输出路径 + string zipPath = $"{Guid.NewGuid()}.zip"; + + // 使用ZipFile.CreateFromDirectory方法压缩文件 + + // ZipFile.CreateFromDirectory(string.Empty, zipPath, CompressionLevel.Fastest, false, null); + + // 或者使用ZipFile.CreateFromDirectory方法压缩指定文件 + using (var archive = ZipFile.Open(zipPath, ZipArchiveMode.Create)) + { + foreach (var file in filePaths) + { + archive.CreateEntryFromFile(file, Path.GetFileName(file)); + } + } + + + + + + + for (var i = 0; i < count; i++) + { + var filelist = excelFiles.Where(p => p.Grpup == $"{sheetName}_{i}").Select(p => p.FileName).ToList(); + //MergeExcelFiles(filelist.ToArray(), $"{sheetName}.xlsx",filelist.First()); + //foreach (var file in filelist) + //{ + // File.Delete(file); + //} + } + + sw.Stop(); + using (FileStream fileStream = new FileStream(zipPath, FileMode.Open)) + { + return fileStream.GetAllBytes(); + } + + } + + + + + public static void WriteDataToExcel(List data, string fileName) + { + IWorkbook workbook = new XSSFWorkbook(); + ISheet sheet = workbook.CreateSheet("Sheet1"); + var firist = data.FirstOrDefault(); + var properties = typeof(T).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) + { + headerRow.CreateCell(columnCellIndex).SetCellValue(property.Key); + columnCellIndex++; + } + foreach (var item in data) + { + IRow row = sheet.CreateRow(rowIndex++); + int cellIndex = 0; + foreach (var property in item.GetType().GetProperties()) + { + ICell cell = row.CreateCell(cellIndex++); + cell.SetCellValue(property.GetValue(item)?.ToString()); + } + } + using (FileStream fileStream = new FileStream(fileName, FileMode.Create)) + { + workbook.Write(fileStream); + } + } + + public static void MergeExcelFiles(string[] filePaths, string outputFilePath, string sheetName) + { + + IWorkbook workbook = new XSSFWorkbook(); + ISheet sheet = workbook.CreateSheet(sheetName); + + + int rowIndex = 0; + foreach (string filePath in filePaths) + { + using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) + { + IWorkbook sourceWorkbook = new XSSFWorkbook(fileStream); + ISheet sourceSheet = sourceWorkbook.GetSheetAt(0); + + int sourceRowCount = sourceSheet.LastRowNum; + for (int i = 0; i <= sourceRowCount; i++) + { + IRow sourceRow = sourceSheet.GetRow(i); + if (sourceRow != null) + { + IRow targetRow = sheet.CreateRow(rowIndex++); + int cellCount = sourceRow.LastCellNum; + for (int j = 0; j < cellCount; j++) + { + ICell sourceCell = sourceRow.GetCell(j); + if (sourceCell != null) + { + ICell targetCell = targetRow.CreateCell(j); + targetCell.SetCellValue(sourceCell.ToString()); + } + } + } + } + } + } + + if (File.Exists(outputFilePath)) + { + using (FileStream fileStream = new FileStream(outputFilePath, FileMode.Open, FileAccess.ReadWrite)) + { + workbook.Write(fileStream); + } + } + else + { + using (FileStream fileStream = new FileStream(outputFilePath, FileMode.Create)) + { + workbook.Write(fileStream); + } + } + } + + + + + } + + + + + + + + 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 8b73cd09..56fd4e77 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 @@ -1,10 +1,14 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.IO; using System.Linq; using Magicodes.ExporterAndImporter.Excel; using Microsoft.AspNetCore.SignalR; using Microsoft.OpenApi.Extensions; +using NPOI.HPSF; +using NPOI.SS.UserModel; +using NPOI.XSSF.UserModel; using SettleAccount.Domain.BQ; using SettleAccount.Job.SignalR; using Shouldly; @@ -109,6 +113,14 @@ namespace SettleAccount.Job.Services.Report var excelExporter = new ExcelExporter(); var items = _objectMapper.Map, List>(saSeEdiCompareDiffs); + + + + + + + + excelExporter = BindExcelExporter(items, businessTypeDisplayName); var result = excelExporter.ExportAppendDataAsByteArray(); @@ -128,6 +140,38 @@ namespace SettleAccount.Job.Services.Report return id.ToString(); } + public void CreateExcelFile(string filename,List p_list) where T : class + { + + + var excelExporter = new ExcelExporter(); + + var result = excelExporter.ExportAppendDataAsByteArray(); + result.ShouldNotBeNull(); + _fileContainer.SaveAsync(filename, result.Result, true); + + + + // 创建数据 + //for (int i = 0; i < 100000; i++) + //{ + // IRow row = sheet.CreateRow(i); + // for (int j = 0; j < 10; j++) + // { + // row.CreateCell(j).SetCellValue($"Row{i} Col{j}"); + // } + //} + //string currentDirectory = Environment.CurrentDirectory; + //// 保存文件 + //using (FileStream file = new FileStream(filePath, FileMode.Create)) + //{ + // workbook.Write(file); + //} + } + + + + #region 私有方法 /// /// 获取比对数据 diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/MaiDanBBACSaSeCompareExportService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/MaiDanBBACSaSeCompareExportService.cs index 93a5314e..2938a102 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/MaiDanBBACSaSeCompareExportService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/MaiDanBBACSaSeCompareExportService.cs @@ -67,6 +67,10 @@ namespace SettleAccount.Job.Services.Report /// public string ExportFile(Guid id, List exportName, List property) { + var flag=Guid.NewGuid().ToString(); + + _logger.LogInformation($"{flag}导出开始:{DateTime.Now.ToString()}" ); + var version = int.Parse(property.Where(p => p.Name == "Version").FirstOrDefault().Value); var lu = property.Where(p => p.Name == "LU").FirstOrDefault().Value; var pn = property.Where(p => p.Name == "PN").FirstOrDefault().Value; @@ -128,6 +132,10 @@ namespace SettleAccount.Job.Services.Report result.ShouldNotBeNull(); _fileContainer.SaveAsync(filename, result.Result, true); + + + + //比对完成后更新状态 var pubSaEntityList = _settleAccountDbContext.Set().Where(t => t.Version == version && t.BusinessType == businessType).ToList(); foreach (var item in pubSaEntityList) @@ -137,7 +145,7 @@ namespace SettleAccount.Job.Services.Report } _settleAccountDbContext.SaveChanges(); - + _logger.LogInformation($"{flag}导出结束:{DateTime.Now.ToString()}"); Notify(); return id.ToString(); } diff --git a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SaSeCompareExportBaseService.cs b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SaSeCompareExportBaseService.cs index 2ae58924..72b63471 100644 --- a/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SaSeCompareExportBaseService.cs +++ b/code/src/Modules/SettleAccount/src/SettleAccount.Job/Services/Report/SaSeCompareExportBaseService.cs @@ -482,6 +482,7 @@ namespace SettleAccount.Job.Services.Report //有结算有发货明细输出 excelExporter.Append(haveSaHaveSeExports.Take(detailMaxRowNumberOnASheet).ToList(), $"{businessTypeDisplayName}有结算有发货明细输出"); + for (var i = 1; i < haveSaHaveSeExports.Count / detailMaxRowNumberOnASheet + ((haveSaHaveSeExports.Count % detailMaxRowNumberOnASheet) > 0 ? 1 : 0); i++) { var sheetDataItems = haveSaHaveSeExports.Skip(i * detailMaxRowNumberOnASheet).Take(detailMaxRowNumberOnASheet).ToList();