ソースを参照

标准化 Aspose.Cells 命名空间并精简导出方法

统一使用 Aspose.Cells.Workbook 类型,移除未用的 using 指令,规范正则相关类型前缀。删除多种 Excel 导出服务方法,优化代码结构,无业务逻辑变更。
Lyyyi 1 ヶ月 前
コミット
38bda63181
共有1 個のファイルを変更した10 個の追加176 個の削除を含む
  1. 10 176
      OASystem/OASystem.Api/OAMethodLib/GeneralMethod.cs

+ 10 - 176
OASystem/OASystem.Api/OAMethodLib/GeneralMethod.cs

@@ -1,17 +1,12 @@
-
-using Aspose.Cells;
+using Aspose.Cells;
 using Aspose.Words;
 using Aspose.Words.Layout;
 using Aspose.Words.Saving;
 using Aspose.Words.Tables;
-using Humanizer;
 using Microsoft.AspNetCore.SignalR;
 using Microsoft.Graph.Models;
 using Microsoft.International.Converters.PinYinConverter;
-using Microsoft.Kiota.Abstractions;
 using NodaTime;
-using NPOI.SS.Formula;
-using NPOI.SS.Formula.Functions;
 using OASystem.API.OAMethodLib.File;
 using OASystem.API.OAMethodLib.Hub.HubClients;
 using OASystem.API.OAMethodLib.Hub.Hubs;
@@ -30,14 +25,9 @@ using OASystem.Domain.ViewModels.JuHeExchangeRate;
 using OASystem.Domain.ViewModels.PersonnelModule;
 using OASystem.Infrastructure.Repositories.CRM;
 using OASystem.Infrastructure.Repositories.Groups;
-using System.ComponentModel;
-using System.ComponentModel.DataAnnotations;
 using System.Data;
 using System.IdentityModel.Tokens.Jwt;
 using System.Security.Claims;
-using static Azure.Core.HttpHeader;
-using static QRCoder.PayloadGenerator;
-using static QRCoder.QRCodeGenerator;
 
 namespace OASystem.API.OAMethodLib
 {
@@ -3153,7 +3143,7 @@ namespace OASystem.API.OAMethodLib
             }
 
             string pattern = @"\+\d+"; // 匹配“+”及其后的数字
-            Match match = Regex.Match(input, pattern);
+            System.Text.RegularExpressions.Match match = Regex.Match(input, pattern);
 
             if (match.Success)
             {
@@ -4895,7 +4885,7 @@ namespace OASystem.API.OAMethodLib
         /// <param name="fontSize"><字体大小/param>
         public static void AsposeWordSetFooter(DocumentBuilder builder, string footerLabel, string font, int fontSize)
         {
-            Section currentSection = builder.CurrentSection;
+            Aspose.Words.Section currentSection = builder.CurrentSection;
             var pageSetup = currentSection.PageSetup;
             pageSetup.DifferentFirstPageHeaderFooter = true;
 
@@ -5849,7 +5839,7 @@ namespace OASystem.API.OAMethodLib
                         string tempPath = (AppSettingsHelper.Get("WordBasePath") + "EnterExitCost/Temp/四川省商务厅出国经费财政先行审核表.xls");
                         //载入模板
                         WorkbookDesigner designer = new WorkbookDesigner();
-                        designer.Workbook = new Workbook(tempPath);
+                        designer.Workbook = new Aspose.Cells.Workbook(tempPath);
 
                         Dictionary<string, string> dic = new Dictionary<string, string>();
 
@@ -6040,7 +6030,7 @@ namespace OASystem.API.OAMethodLib
                         designer.SetDataSource("cellSum4", (enterExitCosts.OutsideJJPay + enterExitCosts.OutsideGWPay).ToString("#0.00"));
                         designer.SetDataSource("celllastStr", celllastStr);
 
-                        Workbook wb = designer.Workbook;
+                        Aspose.Cells.Workbook wb = designer.Workbook;
                         var sheet = wb.Worksheets[0];
 
                         //绑定datatable数据集
@@ -6683,7 +6673,7 @@ namespace OASystem.API.OAMethodLib
         #endregion
 
         #region Excel导出服务
-        
+
         #region 类
         public class FileExportSettings
         {
@@ -6762,165 +6752,9 @@ namespace OASystem.API.OAMethodLib
             }
         }
 
-        public static async Task<ExportResult> ExportWithTemplate<T>(IEnumerable<T> data, byte[] templateBytes, string fileName, string sheetName = "Sheet1")
-        {
-            try
-            {
-                using var templateStream = new MemoryStream(templateBytes);
-                using var workbook = new Workbook(templateStream);
-
-                var worksheet = workbook.Worksheets[sheetName] ?? workbook.Worksheets[0];
-
-                // 查找数据起始行(可以根据模板中的标记来定位)
-                int startRow = FindDataStartRow(worksheet);
-
-                // 填充数据
-                FillWorksheetWithData(worksheet, data, startRow);
-
-                using var outputStream = new MemoryStream();
-                workbook.Save(outputStream, Aspose.Cells.SaveFormat.Xlsx);
-                var excelBytes = outputStream.ToArray();
-
-                var filePath = await SaveFileAsync(excelBytes, fileName, "excel/templates");
-                var downloadUrl = $"wwwroot/exports/{filePath}";
-
-                return new ExportResult
-                {
-                    Success = true,
-                    FilePath = filePath,
-                    DownloadUrl = downloadUrl,
-                    FileName = Path.GetFileName(filePath),
-                    FileSize = excelBytes.Length,
-                    MimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
-                };
-            }
-            catch (Exception ex)
-            {
-                return new ExportResult
-                {
-                    Success = false,
-                    ErrorMessage = ex.Message
-                };
-            }
-        }
-
-        public static async Task<ExportResult> ExportMultipleSheets<T>(Dictionary<string, IEnumerable<T>> sheetsData, string fileName)
-        {
-            try
-            {
-                using var workbook = new Workbook();
-                workbook.Worksheets.Clear(); // 清除默认工作表
-
-                foreach (var sheet in sheetsData)
-                {
-                    var worksheet = workbook.Worksheets.Add(sheet.Key);
-                    var data = sheet.Value;
-
-                    if (data != null && data.Any())
-                    {
-                        FillWorksheetWithData(worksheet, data, 0);
-                        worksheet.AutoFitColumns();
-                    }
-                }
-
-                using var stream = new MemoryStream();
-                workbook.Save(stream, Aspose.Cells.SaveFormat.Xlsx);
-                var excelBytes = stream.ToArray();
-
-                var filePath = await SaveFileAsync(excelBytes, fileName, "excel/multi-sheets");
-                var downloadUrl = $"wwwroot/exports/{filePath}";
-
-                return new ExportResult
-                {
-                    Success = true,
-                    FilePath = filePath,
-                    DownloadUrl = downloadUrl,
-                    FileName = Path.GetFileName(filePath),
-                    FileSize = excelBytes.Length,
-                    MimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
-                };
-            }
-            catch (Exception ex)
-            {
-                return new ExportResult
-                {
-                    Success = false,
-                    ErrorMessage = ex.Message
-                };
-            }
-        }
-
-        public static async Task<ExportResult> ExportWithCustomHeaders<T>(IEnumerable<T> data, Dictionary<string, string> headers, string fileName, string sheetName = "Sheet1")
-        {
-            try
-            {
-                using var workbook = new Workbook();
-                var worksheet = workbook.Worksheets[0];
-                worksheet.Name = sheetName;
-
-                // 设置自定义表头
-                int colIndex = 0;
-                foreach (var header in headers)
-                {
-                    worksheet.Cells[0, colIndex].PutValue(header.Value);
-                    colIndex++;
-                }
-
-                // 填充数据
-                if (data != null && data.Any())
-                {
-                    int rowIndex = 1;
-                    var properties = typeof(T).GetProperties();
-
-                    foreach (var item in data)
-                    {
-                        colIndex = 0;
-                        foreach (var header in headers)
-                        {
-                            var property = properties.FirstOrDefault(p => p.Name == header.Key);
-                            if (property != null)
-                            {
-                                var value = property.GetValue(item);
-                                worksheet.Cells[rowIndex, colIndex].PutValue(value);
-                            }
-                            colIndex++;
-                        }
-                        rowIndex++;
-                    }
-                }
-
-                worksheet.AutoFitColumns();
-
-                using var stream = new MemoryStream();
-                workbook.Save(stream, Aspose.Cells.SaveFormat.Xlsx);
-                var excelBytes = stream.ToArray();
-
-                var filePath = await SaveFileAsync(excelBytes, fileName, "excel/custom-headers");
-                var downloadUrl = $"filePath";
-
-                return new ExportResult
-                {
-                    Success = true,
-                    FilePath = filePath,
-                    DownloadUrl = downloadUrl,
-                    FileName = Path.GetFileName(filePath),
-                    FileSize = excelBytes.Length,
-                    MimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
-                };
-            }
-            catch (Exception ex)
-            {
-                return new ExportResult
-                {
-                    Success = false,
-                    ErrorMessage = ex.Message
-                };
-            }
-        }
-
         private static byte[] GenerateDefaultExcel<T>(IEnumerable<T> data, string sheetName)
         {
-            using var workbook = new Workbook();
+            using var workbook = new Aspose.Cells.Workbook();
             var worksheet = workbook.Worksheets[0];
             worksheet.Name = sheetName;
 
@@ -6935,7 +6769,7 @@ namespace OASystem.API.OAMethodLib
 
         private static byte[] GenerateStyledExcel<T>(IEnumerable<T> data, string sheetName)
         {
-            using var workbook = new Workbook();
+            using var workbook = new Aspose.Cells.Workbook();
             var worksheet = workbook.Worksheets[0];
             worksheet.Name = sheetName;
 
@@ -6962,7 +6796,7 @@ namespace OASystem.API.OAMethodLib
 
         private static byte[] GenerateExcelWithFormulas<T>(IEnumerable<T> data, string sheetName)
         {
-            using var workbook = new Workbook();
+            using var workbook = new Aspose.Cells.Workbook();
             var worksheet = workbook.Worksheets[0];
             worksheet.Name = sheetName;
 
@@ -7030,7 +6864,7 @@ namespace OASystem.API.OAMethodLib
             return 1; // 默认从第2行开始
         }
 
-        private static byte[] SaveWorkbookToBytes(Workbook workbook)
+        private static byte[] SaveWorkbookToBytes(Aspose.Cells.Workbook workbook)
         {
             using var stream = new MemoryStream();
             workbook.Save(stream, Aspose.Cells.SaveFormat.Xlsx);