From 1c09b2934f041f5d311a96888c6084b92909a057 Mon Sep 17 00:00:00 2001 From: ltx <1006158920@qq.com> Date: Fri, 31 Jul 2026 15:44:11 +0800 Subject: [PATCH] =?UTF-8?q?update=EF=BC=9A=E4=BD=9C=E4=B8=9A=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=AF=BC=E5=87=BA=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VOL.Core/Utilities/Tool.cs | 48 ++++++++++++++++--- .../Business/HomeWorkController.cs | 2 +- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/VOL.Core/Utilities/Tool.cs b/VOL.Core/Utilities/Tool.cs index 58d3db3..6f733f8 100644 --- a/VOL.Core/Utilities/Tool.cs +++ b/VOL.Core/Utilities/Tool.cs @@ -89,7 +89,10 @@ namespace VOL.Core.Utilities } } - public static byte[] ExportToExcel(Dictionary> sheetDataList, List fields) + public static byte[] ExportToExcel( + Dictionary> sheetDataList, + List fields, + bool autoFitColumns = true) { using (var package = new ExcelPackage(new FileInfo("filename.xlsx"))) { @@ -101,7 +104,7 @@ namespace VOL.Core.Utilities var sheetName = sheet.Key; var worksheet = package.Workbook.Worksheets.Add(sheetName); - ConfigureWorksheet(worksheet, sheetData, fields); + ConfigureWorksheet(worksheet, sheetData, fields, autoFitColumns); } return package.GetAsByteArray(); @@ -301,7 +304,11 @@ namespace VOL.Core.Utilities /// /// /// - private static void ConfigureWorksheet(ExcelWorksheet worksheet, IEnumerable sheetData, List fields) + private static void ConfigureWorksheet( + ExcelWorksheet worksheet, + IEnumerable sheetData, + List fields, + bool autoFitColumns) { // 添加表头 for (int i = 0; i < fields.Count; i++) @@ -335,8 +342,37 @@ namespace VOL.Core.Utilities } } - // 自动调整列宽 - worksheet.Cells.AutoFitColumns(); + if (autoFitColumns) + { + worksheet.Cells.AutoFitColumns(); + return; + } + + SetColumnWidthsWithoutGdi(worksheet, fields.Count); + } + + private static void SetColumnWidthsWithoutGdi(ExcelWorksheet worksheet, int columnCount) + { + const double minWidth = 10; + const double maxWidth = 40; + + for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) + { + var maxContentWidth = worksheet.Cells + .Where(cell => cell.Start.Column == columnIndex) + .Select(cell => GetExcelContentWidth(cell.Value)) + .DefaultIfEmpty((int)minWidth) + .Max(); + + worksheet.Column(columnIndex).Width = + Math.Min(maxWidth, Math.Max(minWidth, maxContentWidth + 2)); + } + } + + private static int GetExcelContentWidth(object value) + { + var text = value?.ToString() ?? string.Empty; + return text.Sum(character => character > 255 ? 2 : 1); } private static string GetDisplayName(string propertyName) { @@ -749,4 +785,4 @@ namespace VOL.Core.Utilities return chineseNumbers[tens] + "十" + (units == 0 ? "" : chineseNumbers[units]); } } -} \ No newline at end of file +} diff --git a/VOL.WebApi/Controllers/Business/HomeWorkController.cs b/VOL.WebApi/Controllers/Business/HomeWorkController.cs index 8275e19..cba9770 100644 --- a/VOL.WebApi/Controllers/Business/HomeWorkController.cs +++ b/VOL.WebApi/Controllers/Business/HomeWorkController.cs @@ -77,7 +77,7 @@ namespace VOL.WebApi.Controllers.Business nameof(HomeWorkStudentExportModel.Result) }; - var excelBytes = Tool.ExportToExcel(sheets, columns); + var excelBytes = Tool.ExportToExcel(sheets, columns, autoFitColumns: false); var fileName = $"作业学生明细{DateTime.Now:yyyyMMddHHmmss}.xlsx"; return File( excelBytes,