فهرست منبع

Excel导出支持多张图片平铺显示

懒得勤快 4 سال پیش
والد
کامیت
e040982efe
3فایلهای تغییر یافته به همراه81 افزوده شده و 33 حذف شده
  1. 10 1
      Masuit.Tools.Core/Masuit.Tools.Core.csproj
  2. 68 29
      Masuit.Tools.Excel/ExcelExtension.cs
  3. 3 3
      Masuit.Tools.Excel/Masuit.Tools.Excel.csproj

+ 10 - 1
Masuit.Tools.Core/Masuit.Tools.Core.csproj

@@ -62,9 +62,18 @@ github:https://github.com/ldqk/Masuit.Tools
         <PackageReference Include="System.Drawing.Common" Version="5.0.2" />
     </ItemGroup>
     <ItemGroup>
-        <Compile Include="..\Masuit.Tools.Abstractions\*\*.*">
+        <Compile Include="..\Masuit.Tools.Abstractions\*\*.cs">
             <Link>%(RecursiveDir)%(FileName)%(Extension)</Link>
         </Compile>
+        <Compile Include="..\Masuit.Tools.Abstractions\Extensions\*\*.cs">
+            <Link>Extensions\%(RecursiveDir)%(FileName)%(Extension)</Link>
+        </Compile>
+    </ItemGroup>
+    <ItemGroup>
+      <Compile Remove="..\Masuit.Tools.Abstractions\Mapping\**" />
+    </ItemGroup>
+    <ItemGroup>
+      <Compile Remove="..\Masuit.Tools.Abstractions\Reflection\ClassHelper.cs" />
     </ItemGroup>
 
     <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">

+ 68 - 29
Masuit.Tools.Excel/ExcelExtension.cs

@@ -4,6 +4,7 @@ using System.Collections.Generic;
 using System.Data;
 using System.Drawing;
 using System.IO;
+using System.Linq;
 using System.Text;
 
 namespace Masuit.Tools.Excel
@@ -113,46 +114,84 @@ namespace Masuit.Tools.Excel
                     switch (table.Rows[i][j])
                     {
                         case Stream s:
-                        {
-                            if (s.Length > 0)
                             {
-                                using var img = Image.FromStream(s);
-                                using var bmp = new Bitmap(img);
-                                bmp.SetResolution(96, 96);
-                                using var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
-                                picture.SetPosition(i + 1, 3, j, 5); //设置图片显示位置
-                                sheet.Row(i + 2).Height = bmp.Height > 24 ? bmp.Height : 24;
-                                sheet.Column(j + 1).Width = bmp.Width > 32 ? bmp.Width : 32;
+                                if (s.Length > 0)
+                                {
+                                    using var bmp = new Bitmap(s);
+                                    bmp.SetResolution(96, 96);
+                                    using var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
+                                    picture.SetPosition(i + 1, 3, j, 5); //设置图片显示位置
+                                    sheet.Row(i + 2).Height = bmp.Height > 24 ? bmp.Height : 24;
+                                    sheet.Column(j + 1).Width = bmp.Width / 6 > 32 ? bmp.Width / 6 : 32;
+                                }
+
+                                break;
                             }
 
-                            break;
-                        }
                         case Bitmap bmp:
-                        {
-                            if (bmp.Width + bmp.Height > 2)
                             {
-                                bmp.SetResolution(96, 96);
-                                using var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
-                                picture.SetPosition(i + 1, 3, j, 5); //设置图片显示位置
-                                sheet.Row(i + 2).Height = bmp.Height > 24 ? bmp.Height : 24;
-                                sheet.Column(j + 1).Width = bmp.Width > 32 ? bmp.Width : 32;
+                                if (bmp.Width + bmp.Height > 2)
+                                {
+                                    bmp.SetResolution(96, 96);
+                                    using var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
+                                    picture.SetPosition(i + 1, 3, j, 5); //设置图片显示位置
+                                    sheet.Row(i + 2).Height = bmp.Height > 24 ? bmp.Height : 24;
+                                    sheet.Column(j + 1).Width = bmp.Width / 6 > 32 ? bmp.Width / 6 : 32;
+                                }
+
+                                break;
                             }
 
-                            break;
-                        }
-                        default:
-                        {
-                            sheet.SetValue(i + 2, j + 1, table.Rows[i][j] ?? "");
+                        case IEnumerable<Stream> streams:
+                            {
+                                int maxHeight = 0;
+                                int sumWidth = 0;
+                                foreach (var stream in streams.Where(stream => stream.Length > 0))
+                                {
+                                    using var bmp = new Bitmap(stream);
+                                    bmp.SetResolution(96, 96);
+                                    using var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
+                                    picture.SetPosition(i + 1, 3, j, 5 + sumWidth); //设置图片显示位置
+                                    maxHeight = Math.Max(maxHeight, bmp.Height > 24 ? bmp.Height : 24);
+                                    sheet.Row(i + 2).Height = maxHeight;
+                                    sumWidth += bmp.Width;
+                                    sheet.Column(j + 1).Width = sumWidth / 6 > 32 ? sumWidth / 6 : 32;
+                                }
+
+                                break;
+                            }
 
-                            // 根据单元格内容长度来自适应调整列宽
-                            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])
+                        case IEnumerable<Bitmap> bmps:
                             {
-                                sheet.Cells[i + 2, j + 1].AutoFitColumns(18, 110); // 自适应最大列宽,最小18,最大110
+                                int maxHeight = 0;
+                                int sumWidth = 0;
+                                foreach (var bmp in bmps.Where(stream => stream.Width + stream.Height > 2))
+                                {
+                                    bmp.SetResolution(96, 96);
+                                    using var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
+                                    picture.SetPosition(i + 1, 3, j, 5 + sumWidth); //设置图片显示位置
+                                    maxHeight = Math.Max(maxHeight, bmp.Height > 24 ? bmp.Height : 24);
+                                    sheet.Row(i + 2).Height = maxHeight;
+                                    sumWidth += bmp.Width;
+                                    sheet.Column(j + 1).Width = sumWidth / 6 > 32 ? sumWidth / 6 : 32;
+                                }
+
+                                break;
                             }
 
-                            break;
-                        }
+                        default:
+                            {
+                                sheet.SetValue(i + 2, j + 1, table.Rows[i][j] ?? "");
+
+                                // 根据单元格内容长度来自适应调整列宽
+                                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
+                                }
+
+                                break;
+                            }
                     }
                 }
             }

+ 3 - 3
Masuit.Tools.Excel/Masuit.Tools.Excel.csproj

@@ -4,7 +4,7 @@
         <LangVersion>latest</LangVersion>
         <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
         <CodeAnalysisRuleSet />
-        <Version>1.0.0</Version>
+        <Version>1.0.1</Version>
         <Authors>懒得勤快</Authors>
         <Description>Masuit.Tools.Excel导出库</Description>
         <Copyright>懒得勤快</Copyright>
@@ -17,9 +17,9 @@
         <RepositoryType>Github</RepositoryType>
         <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
         <PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
-        <FileVersion>1.0</FileVersion>
+        <FileVersion>1.0.1</FileVersion>
         <Company>ldqk.org</Company>
-        <AssemblyVersion>1.0</AssemblyVersion>
+        <AssemblyVersion>1.0.1</AssemblyVersion>
         <PackageLicenseUrl>https://github.com/ldqk/Masuit.Tools/blob/master/LICENSE</PackageLicenseUrl>
         <EmbedUntrackedSources>true</EmbedUntrackedSources>
         <IncludeSymbols>true</IncludeSymbols>