Skip to content

Commit 6589101

Browse files
authored
Bug/s1035 (#2334)
* Initial fix * Better solution using existing data-structures
1 parent b333523 commit 6589101

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public enum ePictureInclude
2929
/// <summary>
3030
/// Include the images in the html export.
3131
/// </summary>
32-
Include
32+
Include,
33+
/// <summary>
34+
/// Include the images only in the HTML.
35+
/// </summary>
36+
IncludeInHtmlOnly
3337
}
3438
}

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

Lines changed: 34 additions & 3 deletions
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;
@@ -146,7 +148,7 @@ protected HTMLElement GetThead(ExcelRangeBase range, List<string> headers = null
146148

147149
AddTableData(table, contentElement, col);
148150

149-
if (Settings.Pictures.Include == ePictureInclude.Include)
151+
if (Settings.Pictures.Include == (ePictureInclude.Include | ePictureInclude.IncludeInHtmlOnly))
150152
{
151153
image = GetImage(cell.Worksheet.PositionId, cell._fromRow, cell._fromCol);
152154
}
@@ -267,7 +269,7 @@ protected HTMLElement GetTableBody(ExcelRangeBase range, int row, int endRow)
267269

268270
SetColRowSpan(range, tblData, cell);
269271

270-
if (Settings.Pictures.Include == ePictureInclude.Include)
272+
if (Settings.Pictures.Include == (ePictureInclude.Include | ePictureInclude.IncludeInHtmlOnly))
271273
{
272274
image = GetImage(cell.Worksheet.PositionId, cell._fromRow, cell._fromCol);
273275
}
@@ -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.Include == ePictureInclude.IncludeInHtmlOnly)
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ internal HtmlPictureSettings()
2222
{
2323

2424
}
25+
2526
/// <summary>
2627
/// If picture drawings should be included in the html. Default is <see cref="ePictureInclude.Exclude"/>
2728
/// </summary>

src/EPPlusTest/Export/HtmlExport/RangeExporterTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,27 @@ public async Task WriteImagesAsync()
195195
Assert.AreEqual(html, htmlAsync);
196196
}
197197
}
198+
199+
[TestMethod]
200+
public async Task WriteImagesAsyncHTMLOnlyEmbed()
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.IncludeInHtmlOnly;
210+
exporter.Settings.Minify = false;
211+
exporter.Settings.Encoding = Encoding.UTF8;
212+
213+
var html = exporter.GetHtmlString();
214+
var htmlAsync = await exporter.GetSinglePageAsync();
215+
File.WriteAllText("c:\\temp\\" + sheet.Name + "_htmlOnly.html", html);
216+
}
217+
}
218+
198219
[TestMethod]
199220
public void ExportMultipleRanges()
200221
{

0 commit comments

Comments
 (0)