From 45e4a57c2050a7f28b9a65827899a07d5fdf2761 Mon Sep 17 00:00:00 2001 From: Wei Date: Fri, 21 Jan 2022 12:01:22 +0800 Subject: [PATCH] [New] SaveAs support image #304 --- README.md | 21 ++++++++++++++ README.zh-CN.md | 23 +++++++++++++++ README.zh-Hant.md | 21 ++++++++++++++ docs/README.md | 3 ++ docs/README.zh-CN.md | 3 ++ docs/README.zh-Hant.md | 2 ++ samples/images/google_logo.png | Bin 0 -> 557 bytes samples/images/microsoft_logo.png | Bin 0 -> 198 bytes samples/images/reddit_logo.png | Bin 0 -> 754 bytes samples/images/statck_overflow_logo.png | Bin 0 -> 440 bytes .../OpenXml/ExcelOpenXmlSheetWriter.cs | 27 +++++++++++------- tests/MiniExcelTests/MiniExcelIssueTests.cs | 22 +++++++++----- tests/MiniExcelTests/Utils/PathHelper.cs | 2 +- 13 files changed, 105 insertions(+), 19 deletions(-) create mode 100644 samples/images/google_logo.png create mode 100644 samples/images/microsoft_logo.png create mode 100644 samples/images/reddit_logo.png create mode 100644 samples/images/statck_overflow_logo.png diff --git a/README.md b/README.md index 1ad8a9d..81f4d64 100644 --- a/README.md +++ b/README.md @@ -469,6 +469,27 @@ MiniExcel.SaveAs(path, value, configuration: new OpenXmlConfiguration() { AutoFi +#### 10. Create Image + +```csharp +var value = new[] { + new { Name="github",Image=File.ReadAllBytes(PathHelper.GetFile("images/github_logo.png"))}, + new { Name="google",Image=File.ReadAllBytes(PathHelper.GetFile("images/google_logo.png"))}, + new { Name="microsoft",Image=File.ReadAllBytes(PathHelper.GetFile("images/microsoft_logo.png"))}, + new { Name="reddit",Image=File.ReadAllBytes(PathHelper.GetFile("images/reddit_logo.png"))}, + new { Name="statck_overflow",Image=File.ReadAllBytes(PathHelper.GetFile("images/statck_overflow_logo.png"))}, +}; +MiniExcel.SaveAs(path, value); +``` + +![image](https://user-images.githubusercontent.com/12729184/150462383-ad9931b3-ed8d-4221-a1d6-66f799743433.png) + + + + + + + ### Fill Data To Excel Template diff --git a/README.zh-CN.md b/README.zh-CN.md index be81dc7..481d399 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -473,6 +473,29 @@ MiniExcel.SaveAs(path, value, configuration: new OpenXmlConfiguration() { AutoFi +#### 10. 图片生成 + +```csharp +var value = new[] { + new { Name="github",Image=File.ReadAllBytes(PathHelper.GetFile("images/github_logo.png"))}, + new { Name="google",Image=File.ReadAllBytes(PathHelper.GetFile("images/google_logo.png"))}, + new { Name="microsoft",Image=File.ReadAllBytes(PathHelper.GetFile("images/microsoft_logo.png"))}, + new { Name="reddit",Image=File.ReadAllBytes(PathHelper.GetFile("images/reddit_logo.png"))}, + new { Name="statck_overflow",Image=File.ReadAllBytes(PathHelper.GetFile("images/statck_overflow_logo.png"))}, +}; +MiniExcel.SaveAs(path, value); +``` + +![image](https://user-images.githubusercontent.com/12729184/150462383-ad9931b3-ed8d-4221-a1d6-66f799743433.png) + + + + + + + + + ### 模板填充 Excel diff --git a/README.zh-Hant.md b/README.zh-Hant.md index 472485a..6f22cd1 100644 --- a/README.zh-Hant.md +++ b/README.zh-Hant.md @@ -479,6 +479,27 @@ MiniExcel.SaveAs(path, value, configuration: new OpenXmlConfiguration() { AutoFi +#### 10. 圖片生成 + +```csharp +var value = new[] { + new { Name="github",Image=File.ReadAllBytes(PathHelper.GetFile("images/github_logo.png"))}, + new { Name="google",Image=File.ReadAllBytes(PathHelper.GetFile("images/google_logo.png"))}, + new { Name="microsoft",Image=File.ReadAllBytes(PathHelper.GetFile("images/microsoft_logo.png"))}, + new { Name="reddit",Image=File.ReadAllBytes(PathHelper.GetFile("images/reddit_logo.png"))}, + new { Name="statck_overflow",Image=File.ReadAllBytes(PathHelper.GetFile("images/statck_overflow_logo.png"))}, +}; +MiniExcel.SaveAs(path, value); +``` + +![image](https://user-images.githubusercontent.com/12729184/150462383-ad9931b3-ed8d-4221-a1d6-66f799743433.png) + + + + + + + diff --git a/docs/README.md b/docs/README.md index 5c08197..0e84198 100644 --- a/docs/README.md +++ b/docs/README.md @@ -16,6 +16,9 @@ --- +### 0.20.0 +- [New] SaveAs support image #304 + ### 0.19.3-beta - [Fix] Excelnumberformat 1.1.0 valid date expired (Valid from: 2018-04-10 08:00:00 to 2021-04-14 20:00:00) [link](https://github.com/andersnm/ExcelNumberFormat/issues/34) diff --git a/docs/README.zh-CN.md b/docs/README.zh-CN.md index 9ec929f..465a88d 100644 --- a/docs/README.zh-CN.md +++ b/docs/README.zh-CN.md @@ -23,6 +23,9 @@ --- +### 0.20.0 +- [New] SaveAs 支持图片生成 #304 + ### 0.19.3-beta - [Fix] Excelnumberformat 1.1.0 凭证过期 (Valid from: 2018-04-10 08:00:00 to 2021-04-14 20:00:00) [link](https://github.com/andersnm/ExcelNumberFormat/issues/34) diff --git a/docs/README.zh-Hant.md b/docs/README.zh-Hant.md index 5c805f8..1a5e5f2 100644 --- a/docs/README.zh-Hant.md +++ b/docs/README.zh-Hant.md @@ -17,6 +17,8 @@ --- +### 0.20.0 +- [New] SaveAs 支持圖片生成 #304 ### 0.19.3-beta - [Fix] Excelnumberformat 1.1.0 憑證過期 (Valid from: 2018-04-10 08:00:00 to 2021-04-14 20:00:00) [link](https://github.com/andersnm/ExcelNumberFormat/issues/34) diff --git a/samples/images/google_logo.png b/samples/images/google_logo.png new file mode 100644 index 0000000000000000000000000000000000000000..8efaf819119a303439a2f1c0563565783f520609 GIT binary patch literal 557 zcmV+|0@D47P)eh4bY)!^&fNe20m4Z{K~zYIWBmXB zKLfP^6V*)|HIycXvJh19|NnpF6vD{J$jHcuUkf9iB=!IQe_ZA!#fn3$OU z|N8Y~^O_%fcl>>L|Nq~AEbNQ=C9l=eqFhMBK`L-reZXJ#ibBWG5%W2n{f5-r$kn5eH3ZbaKst{I3w!+ z*K15%zgexk;Udm$Uy&79sxr5Pa$wbqJMlvz5O;$6^#`X`oG$&wuJq@})5Fhh_=|W! zMD|v3Lm18Le;&C0M}i+`I>q8rNEqD_ij96})p7IW`PbKwWp6(JcjV?D1_lN*Wfle; z2>@q~`t|pB-0t$bA0M$Xv4*R8SxXu*F*2Qfb!Gke_un6ftBJEsPvpdvqVOgDSKr>| zA8Nhx@e!J=3cK|L#{zMFMqG&>r%NHZ@YlcJ>+bB@_VCc%kB=DsGsyEvxyV{Ys`+rT vu;a=8G$~N<84A}=&7zNNFm$Su5pE~|#7G<1`muvf00000NkvXXu0mjfoc9sc literal 0 HcmV?d00001 diff --git a/samples/images/microsoft_logo.png b/samples/images/microsoft_logo.png new file mode 100644 index 0000000000000000000000000000000000000000..1973db7db1173c8c7446e53e4bcc245ee26caa43 GIT binary patch literal 198 zcmeAS@N?(olHy`uVBq!ia0vp^G9b*s1SJ3FdmIK*oCO|{#S9F5M?jcysy3fAP*AYM zHKHUqKdq!Zu_%=xI4`pxvA85PR{Y#upk%bCi(`mKXY!x_|LvK#G74Yyl`);7*0w2u z>+gr@$3>pp_^~0;TzOeVv-Y$j#wB~~voyXkD(iBI=)9a#_>$e{;QxzEJi9k631-`V qvBWA%gKL4Om8{i0dDAJs7#QBFi5R*qs#F46!{F)a=d#Wzp$PzW@eh4bY)!^&fNe20*6UNK~zYI?UhSN zR8bhmf9KwDX4D>vSR~N{${>|OUHPaCgft&5%7su-P^l$Ft+F1f9DlUF z4mS|HB&;i04D2t%PX!~#$XfmwyJa0;2bIDe10rBP>krH0;XV8=AD=JV9p zdm6DwNM>Uumk#0^p{$nUS3}+53gvOp+lr-QnTTN15sl@E*qwB?@%1CY0H%Sd(RH1- z&!U4qXu=8t1T5xY(qzJ7RVv3I|%B`s$` kShSh`hW?)Keh4bY)!^&fNe20ZmCnK~zYIWBmXB zKLfP^6V*)|HFOXd`v1cd>KpofapLb23y83s6!7=u&Cj*+e_!9luZ$=|e;uFy``S(@ z`|ymf{dWKV{=uW3DDN|gYy4Q1^K)|z!@s|*HtCF9g1>fmQo;NGzP$Z5)Bo!vmw)dc z|NHdfbFKWJho{N({@>Skzb{SwbMH8W`*Tmv=UVySH+KKNwDHHbCNd5E_x!5|Ck%(qWR)WtP-pVCDLKW+{uR14V}Zdv^Knv&;XUU;Fp`%HQYL7=C_ck}>4U z+J@g-IKy<_;+0r}_{&>Jq+^wZ0~Y*NelFy~6y)vyhF=!}Lka39+0gk*R*_*M1Hp(Q i4-A4rdeqPXGZX;!NF>sI*cy`n0000{v}"); else @@ -582,15 +587,15 @@ namespace MiniExcelLibs.OpenXml { drawing.Append($@" - 0 + {i.CellIndex- 1/* why -1 : https://user-images.githubusercontent.com/12729184/150460189-f08ed939-44d4-44e1-be6e-9c533ece6be8.png*/} 0 - 0 + {i.RowIndex-1} 0 - + @@ -641,7 +646,7 @@ namespace MiniExcelLibs.OpenXml CreateZipEntry(@"xl/workbook.xml", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml", _defaultWorkbookXml.Replace("{{sheets}}", workbookXml.ToString())); CreateZipEntry(@"xl/_rels/workbook.xml.rels", "", - _defaultWorkbookXmlRels.Replace("{{sheets}}", workbookRelsXml.ToString())); + _defaultWorkbookXmlRels.Replace("{{sheets}}", workbookRelsXml.ToString())); } //[Content_Types].xml diff --git a/tests/MiniExcelTests/MiniExcelIssueTests.cs b/tests/MiniExcelTests/MiniExcelIssueTests.cs index 2335178..82342a3 100644 --- a/tests/MiniExcelTests/MiniExcelIssueTests.cs +++ b/tests/MiniExcelTests/MiniExcelIssueTests.cs @@ -34,15 +34,23 @@ namespace MiniExcelLibs.Tests [Fact] public void TestIssue304() { - var imagePath = PathHelper.GetFile("images/github_logo.png"); - var image = File.ReadAllBytes(imagePath); - var value = Enumerable.Range(1, 5).Select(s => new { image }); - var path = PathHelper.GetRandomPath(); + var path = PathHelper.GetTempFilePath(); + var value = new[] { + new { Name="github",Image=File.ReadAllBytes(PathHelper.GetFile("images/github_logo.png"))}, + new { Name="google",Image=File.ReadAllBytes(PathHelper.GetFile("images/google_logo.png"))}, + new { Name="microsoft",Image=File.ReadAllBytes(PathHelper.GetFile("images/microsoft_logo.png"))}, + new { Name="reddit",Image=File.ReadAllBytes(PathHelper.GetFile("images/reddit_logo.png"))}, + new { Name="statck_overflow",Image=File.ReadAllBytes(PathHelper.GetFile("images/statck_overflow_logo.png"))}, + }; MiniExcel.SaveAs(path, value); - - //TODO: Read from base 64 not work - //var image = Convert.FromBase64String("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQAAAAA3bvkkAAAAEElEQVR4nGJgAQAAAP//AwAABgAFV7+r1AAAAABJRU5ErkJggg=="); + { + Assert.Contains("/xl/media/image", Helpers.GetZipFileContent(path, "xl/drawings/_rels/drawing1.xml.rels")); + Assert.Contains("ext cx=\"609600\" cy=\"190500\"", Helpers.GetZipFileContent(path, "xl/drawings/drawing1.xml")); + Assert.Contains("/xl/drawings/drawing1.xml", Helpers.GetZipFileContent(path, "[Content_Types].xml")); + Assert.Contains("drawing r:id=", Helpers.GetZipFileContent(path, "xl/worksheets/sheet1.xml")); + Assert.Contains("../drawings/drawing1.xml", Helpers.GetZipFileContent(path, "xl/worksheets/_rels/sheet1.xml.rels")); + } } /// diff --git a/tests/MiniExcelTests/Utils/PathHelper.cs b/tests/MiniExcelTests/Utils/PathHelper.cs index a3f9153..059f687 100644 --- a/tests/MiniExcelTests/Utils/PathHelper.cs +++ b/tests/MiniExcelTests/Utils/PathHelper.cs @@ -20,7 +20,7 @@ return path; } - public static string GetRandomPath(string extension = "xlsx") + public static string GetTempFilePath(string extension = "xlsx") { return Path.GetTempPath() + Guid.NewGuid().ToString() + "." + extension; }