懒得勤快 4 gadi atpakaļ
vecāks
revīzija
356f288cb0

+ 9 - 1
Masuit.Tools.Abstractions/Database/DataTableHelper.cs

@@ -88,7 +88,15 @@ namespace Masuit.Tools.Database
             }
 
             var properties = list[0].GetType().GetProperties();
-            result.Columns.AddRange(properties.Select(p => new DataColumn(p.GetCustomAttribute<DescriptionAttribute>()?.Description ?? p.Name, p.PropertyType)).ToArray());
+            result.Columns.AddRange(properties.Select(p =>
+            {
+                if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
+                {
+                    return new DataColumn(p.GetCustomAttribute<DescriptionAttribute>()?.Description ?? p.Name, Nullable.GetUnderlyingType(p.PropertyType));
+                }
+
+                return new DataColumn(p.GetCustomAttribute<DescriptionAttribute>()?.Description ?? p.Name, p.PropertyType);
+            }).ToArray());
             list.ForEach(item => result.LoadDataRow(properties.Select(p => p.GetValue(item)).ToArray(), true));
             return result;
         }

+ 4 - 2
Masuit.Tools.Excel/ExcelExtension.cs

@@ -91,9 +91,11 @@ namespace Masuit.Tools.Excel
             var sheet = pkg.Workbook.Worksheets[table.TableName];
 
             // 填充表头
+            var maxWidth = new int[table.Columns.Count];
             for (var j = 0; j < table.Columns.Count; j++)
             {
                 sheet.SetValue(1, j + 1, table.Columns[j].ColumnName);
+                maxWidth[j] = Encoding.UTF8.GetBytes(table.Columns[j].ColumnName).Length;
             }
 
             sheet.Row(1).Style.Font.Bold = true; // 表头设置为粗体
@@ -126,8 +128,8 @@ namespace Masuit.Tools.Excel
                         sheet.SetValue(i + 2, j + 1, table.Rows[i][j] ?? "");
 
                         // 根据单元格内容长度来自适应调整列宽
-                        var maxWidth = Encoding.UTF8.GetBytes(table.Rows[i][j].ToString()).Length;
-                        if (sheet.Column(j + 1).Width < maxWidth)
+                        maxWidth[j] = Math.Max(Encoding.UTF8.GetBytes(table.Rows[i][j].ToString() ?? string.Empty).Length, maxWidth[j]);
+                        if (sheet.Column(j + 1).Width < maxWidth[j])
                         {
                             sheet.Cells[i + 2, j + 1].AutoFitColumns(18, 110); // 自适应最大列宽,最小18,最大110
                         }

+ 1 - 1
NetCoreTest/Program.cs

@@ -36,7 +36,7 @@ namespace NetCoreTest
             };
             var allParent = myClass.AllParent().Append(myClass);
             var tree = allParent.ToTreeGeneral(c => c.Id, c => c.Pid);
-            tree.ToDataTable().ToExcel().SaveFile(@"Y:\1.xlsx");
+            tree.Flatten(t => t.Children).Select(t => t.Value).ToDataTable().ToExcel().SaveFile(@"Y:\1.xlsx");
             Console.WriteLine(tree.ToJsonString(new JsonSerializerSettings()
             {
                 ReferenceLoopHandling = ReferenceLoopHandling.Ignore,