Skip to content

Commit a4b1660

Browse files
committed
Initial fix
1 parent 3995501 commit a4b1660

4 files changed

Lines changed: 60 additions & 2 deletions

File tree

src/EPPlus/Export/HtmlExport/Enums/ePictureInclude.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ public enum ePictureInclude
2929
/// <summary>
3030
/// Include the images in the html export.
3131
/// </summary>
32-
Include
32+
Include,
3333
}
3434
}

src/EPPlus/Export/HtmlExport/Exporters/Internal/HtmlExporterBaseInternal.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@ Date Author Change
1111
6/4/2022 EPPlus Software AB ExcelTable Html Export
1212
*************************************************************************************************/
1313
using OfficeOpenXml.Core;
14+
using OfficeOpenXml.Drawing;
1415
using OfficeOpenXml.Drawing.Interfaces;
1516
using OfficeOpenXml.Export.HtmlExport.Accessibility;
1617
using OfficeOpenXml.Export.HtmlExport.HtmlCollections;
1718
using OfficeOpenXml.Export.HtmlExport.Parsers;
19+
using OfficeOpenXml.Export.HtmlExport.Translators;
1820
using OfficeOpenXml.Table;
1921
using OfficeOpenXml.Utils;
2022
using OfficeOpenXml.Utils.String;
@@ -381,6 +383,24 @@ protected void AddContent(ExcelRangeBase cell, HTMLElement contentElement)
381383
}
382384
}
383385

386+
387+
private object GetContentType(ePictureType type)
388+
{
389+
switch (type)
390+
{
391+
case ePictureType.Ico:
392+
return "image/vnd.microsoft.icon";
393+
case ePictureType.Jpg:
394+
return "image/jpeg";
395+
case ePictureType.Svg:
396+
return "image/svg+xml";
397+
case ePictureType.Tif:
398+
return "image/tiff";
399+
default:
400+
return $"image/{type}";
401+
}
402+
}
403+
384404
protected void AddImage(HTMLElement parent, HtmlExportSettings settings, HtmlImage image, object value)
385405
{
386406
if (image != null)
@@ -393,7 +413,18 @@ protected void AddImage(HTMLElement parent, HtmlExportSettings settings, HtmlIma
393413
{
394414
child.AddAttribute("id", imageName);
395415
}
396-
child.AddAttribute("class", $"{settings.StyleClassPrefix}image-{name} {settings.StyleClassPrefix}image-prop-{imageName}");
416+
417+
if(settings.Pictures.EmbedImagesInHtml)
418+
{
419+
ePictureType? type;
420+
var _encodedImage = ImageEncoder.EncodeImage(image, out type);
421+
422+
child.AddAttribute("src", $"data:{GetContentType(type.Value)};base64,{_encodedImage}");
423+
}
424+
else
425+
{
426+
child.AddAttribute("class", $"{settings.StyleClassPrefix}image-{name} {settings.StyleClassPrefix}image-prop-{imageName}");
427+
}
397428
parent._childElements.Add(child);
398429
}
399430
}

src/EPPlus/Export/HtmlExport/Settings/HtmlPictureSettings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ internal HtmlPictureSettings()
2222
{
2323

2424
}
25+
26+
public bool EmbedImagesInHtml = false;
27+
2528
/// <summary>
2629
/// If picture drawings should be included in the html. Default is <see cref="ePictureInclude.Exclude"/>
2730
/// </summary>

src/EPPlusTest/Export/HtmlExport/RangeExporterTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,30 @@ public async Task WriteImagesAsync()
195195
Assert.AreEqual(html, htmlAsync);
196196
}
197197
}
198+
199+
[TestMethod]
200+
public async Task WriteImagesAsyncBoolEmbed()
201+
{
202+
using (var p = OpenTemplatePackage("20-CreateAFileSystemReport.xlsx"))
203+
{
204+
var sheet = p.Workbook.Worksheets[0];
205+
var exporter = sheet.Cells["A1:E30"].CreateHtmlExporter();
206+
207+
exporter.Settings.SetColumnWidth = true;
208+
exporter.Settings.SetRowHeight = true;
209+
exporter.Settings.Pictures.Include = ePictureInclude.Include;
210+
exporter.Settings.Minify = false;
211+
exporter.Settings.Encoding = Encoding.UTF8;
212+
exporter.Settings.Pictures.EmbedImagesInHtml = true;
213+
214+
var html = exporter.GetSinglePage();
215+
var htmlAsync = await exporter.GetSinglePageAsync();
216+
File.WriteAllText("c:\\temp\\" + sheet.Name + ".html", html);
217+
File.WriteAllText("c:\\temp\\" + sheet.Name + "-async.html", htmlAsync);
218+
Assert.AreEqual(html, htmlAsync);
219+
}
220+
}
221+
198222
[TestMethod]
199223
public void ExportMultipleRanges()
200224
{

0 commit comments

Comments
 (0)