Browse Source

excel导出不依赖System.Drawing.Common

懒得勤快 3 years ago
parent
commit
c24819b18d
2 changed files with 49 additions and 71 deletions
  1. 46 69
      Masuit.Tools.Excel/ExcelExtension.cs
  2. 3 2
      Masuit.Tools.Excel/Masuit.Tools.Excel.csproj

+ 46 - 69
Masuit.Tools.Excel/ExcelExtension.cs

@@ -1,8 +1,9 @@
 using OfficeOpenXml;
+using OfficeOpenXml.Drawing;
+using SixLabors.ImageSharp;
 using System;
 using System.Collections.Generic;
 using System.Data;
-using System.Drawing;
 using System.IO;
 using System.Linq;
 using System.Text;
@@ -144,27 +145,9 @@ namespace Masuit.Tools.Excel
                             {
                                 if (s.Length > 2)
                                 {
-                                    using var bmp = new Bitmap(s);
-                                    bmp.SetResolution(96, 96);
-                                    var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
-                                    picture.SetPosition(i + startRow, 3, j + startColumn - 1, 5); //设置图片显示位置
-                                    var percent = Math.Min(11000f / bmp.Height, 100);
-                                    picture.SetSize((int)percent);
-                                    sheet.Row(i + startRow + 1).Height = 90;
-                                    sheet.Column(j + startColumn).Width = Math.Max(sheet.Column(j + startColumn).Width, bmp.Width * percent / 600 > 32 ? bmp.Width * percent / 600 : 32);
-                                }
-
-                                sheet.SetValue(i + startRow + 1, j + startColumn, "");
-
-                                break;
-                            }
-
-                        case Bitmap bmp:
-                            {
-                                if (bmp.Width + bmp.Height > 4)
-                                {
-                                    bmp.SetResolution(96, 96);
-                                    var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
+                                    var (pictureType, ms) = Detect(s);
+                                    var bmp = new ExcelImage(ms, pictureType).Bounds;
+                                    var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), ms, pictureType);
                                     picture.SetPosition(i + startRow, 3, j + startColumn - 1, 5); //设置图片显示位置
                                     var percent = Math.Min(11000f / bmp.Height, 100);
                                     picture.SetSize((int)percent);
@@ -182,28 +165,9 @@ namespace Masuit.Tools.Excel
                                 double sumWidth = 0;
                                 foreach (var stream in streams.Where(stream => stream.Length > 2))
                                 {
-                                    using var bmp = new Bitmap(stream);
-                                    bmp.SetResolution(96, 96);
-                                    var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
-                                    picture.SetPosition(i + startRow, 3, j + startColumn - 1, (int)(5 + sumWidth)); //设置图片显示位置
-                                    var percent = Math.Min(11000f / bmp.Height, 100);
-                                    picture.SetSize((int)percent);
-                                    sheet.Row(i + startRow + 1).Height = 90;
-                                    sumWidth += bmp.Width * 1.0 * percent / 100 + 5;
-                                    sheet.Column(j + startColumn).Width = Math.Max(sheet.Column(j + startColumn).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
-                                }
-
-                                sheet.SetValue(i + startRow + 1, j + startColumn, "");
-                                break;
-                            }
-
-                        case IEnumerable<Bitmap> bmps:
-                            {
-                                double sumWidth = 0;
-                                foreach (var bmp in bmps.Where(stream => stream.Width + stream.Height > 4))
-                                {
-                                    bmp.SetResolution(96, 96);
-                                    var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp);
+                                    var (pictureType, ms) = Detect(stream);
+                                    var bmp = new ExcelImage(ms, pictureType).Bounds;
+                                    var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), ms, pictureType);
                                     picture.SetPosition(i + startRow, 3, j + startColumn - 1, (int)(5 + sumWidth)); //设置图片显示位置
                                     var percent = Math.Min(11000f / bmp.Height, 100);
                                     picture.SetSize((int)percent);
@@ -221,9 +185,9 @@ namespace Masuit.Tools.Excel
                                 double sumWidth = 0;
                                 foreach (var kv in dic.Where(kv => kv.Value.Length > 2))
                                 {
-                                    using var bmp = new Bitmap(kv.Value);
-                                    bmp.SetResolution(96, 96);
-                                    var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp, new Uri(kv.Key));
+                                    var (pictureType, ms) = Detect(kv.Value);
+                                    var bmp = new ExcelImage(ms, pictureType).Bounds;
+                                    var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), ms, pictureType, new Uri(kv.Key));
                                     picture.SetPosition(i + startRow, 3, j + startColumn - 1, (int)(5 + sumWidth)); //设置图片显示位置
                                     var percent = Math.Min(11000f / bmp.Height, 100);
                                     picture.SetSize((int)percent);
@@ -241,9 +205,9 @@ namespace Masuit.Tools.Excel
                                 double sumWidth = 0;
                                 foreach (var kv in dic.Where(kv => kv.Value.Length > 2))
                                 {
-                                    using var bmp = new Bitmap(kv.Value);
-                                    bmp.SetResolution(96, 96);
-                                    var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), bmp, new Uri(kv.Key));
+                                    var (pictureType, ms) = Detect(kv.Value);
+                                    var bmp = new ExcelImage(ms, pictureType).Bounds;
+                                    var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), ms, pictureType, new Uri(kv.Key));
                                     picture.SetPosition(i + startRow, 3, j + startColumn - 1, (int)(5 + sumWidth)); //设置图片显示位置
                                     var percent = Math.Min(11000f / bmp.Height, 100);
                                     picture.SetSize((int)percent);
@@ -256,25 +220,6 @@ namespace Masuit.Tools.Excel
                                 break;
                             }
 
-                        case IDictionary<string, Bitmap> bmps:
-                            {
-                                double sumWidth = 0;
-                                foreach (var kv in bmps.Where(kv => kv.Value.Width + kv.Value.Height > 4))
-                                {
-                                    kv.Value.SetResolution(96, 96);
-                                    var picture = sheet.Drawings.AddPicture(Guid.NewGuid().ToString(), kv.Value, new Uri(kv.Key));
-                                    picture.SetPosition(i + startRow, 3, j + startColumn - 1, (int)(5 + sumWidth)); //设置图片显示位置
-                                    var percent = Math.Min(11000f / kv.Value.Height, 100);
-                                    picture.SetSize((int)percent);
-                                    sheet.Row(i + startRow + 1).Height = 90;
-                                    sumWidth += kv.Value.Width * 1.0 * percent / 100 + 5;
-                                    sheet.Column(j + startColumn).Width = Math.Max(sheet.Column(j + startColumn).Width, sumWidth / 6 > 32 ? sumWidth / 6 : 32);
-                                }
-
-                                sheet.SetValue(i + startRow + 1, j + startColumn, "");
-                                break;
-                            }
-
                         default:
                             {
                                 sheet.SetValue(i + startRow + 1, j + startColumn, table.Rows[i][j] ?? "");
@@ -301,6 +246,38 @@ namespace Masuit.Tools.Excel
 
         private static readonly NumberFormater NumberFormater = new("ABCDEFGHIJKLMNOPQRSTUVWXYZ", 1);
 
+        /// <summary>
+        /// 检测图片格式
+        /// </summary>
+        /// <param name="stream"></param>
+        /// <returns></returns>
+        private static (ePictureType type, Stream stream) Detect(Stream stream)
+        {
+            try
+            {
+                var pictureType = Image.DetectFormat(stream).Name switch
+                {
+                    "JPEG" => ePictureType.Jpg,
+                    "PNG" => ePictureType.Png,
+                    "GIF" => ePictureType.Gif,
+                    "BMP" => ePictureType.Bmp,
+                    "SVG" => ePictureType.Svg,
+                    "TIF" => ePictureType.Tif,
+                    "WEBP" => ePictureType.WebP,
+                    _ => default
+                };
+                return (pictureType, stream);
+            }
+            catch
+            {
+                var ms = new MemoryStream();
+                using var image = Image.Load(stream);
+                image.SaveAsWebp(ms);
+                stream.Dispose();
+                return (ePictureType.WebP, ms);
+            }
+        }
+
         /// <summary>
         /// 获取字母列
         /// </summary>

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

@@ -3,7 +3,7 @@
         <TargetFrameworks>netstandard2.0;netstandard2.1;net461;net5;net6</TargetFrameworks>
         <LangVersion>latest</LangVersion>
         <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
-        <Version>1.1.1</Version>
+        <Version>1.1.2</Version>
         <Authors>懒得勤快</Authors>
         <Description>Masuit.Tools.Excel导出库,支持一些简单数据的导出,支持图片列</Description>
         <Copyright>懒得勤快</Copyright>
@@ -30,8 +30,9 @@
     </PropertyGroup>
     <ItemGroup>
         <PackageReference Include="EPPlus" Version="6.0.3" />
-        <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
+        <PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
         <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
+        <PackageReference Include="SixLabors.ImageSharp" Version="2.1.0" />
     </ItemGroup>
 
 </Project>