diff --git a/src/EPPlus/Attributes/EpplusTableColumnAttributeBase.cs b/src/EPPlus/Attributes/EpplusTableColumnAttributeBase.cs index 47d62bad2..3d55eadf9 100644 --- a/src/EPPlus/Attributes/EpplusTableColumnAttributeBase.cs +++ b/src/EPPlus/Attributes/EpplusTableColumnAttributeBase.cs @@ -60,6 +60,15 @@ public bool Hidden set; } + /// + /// Indicates whether the Built in (default) hyperlink style should be + /// applied to hyperlinks or not. Default value is true. + /// + public bool UseBuiltInHyperlinkStyle + { + get; set; + } = true; + /// /// If not the last cell in the column (the totals row) will contain a formula of the specified type. /// diff --git a/src/EPPlus/ExcelWorksheet.cs b/src/EPPlus/ExcelWorksheet.cs index 419be6ac5..704cc7ed6 100644 --- a/src/EPPlus/ExcelWorksheet.cs +++ b/src/EPPlus/ExcelWorksheet.cs @@ -3490,12 +3490,13 @@ internal void SetValueStyleIdInner(int row, int col, object value, int styleId) /// end row /// end column /// set values + /// Will add built in styles for hyperlinks /// If the value is of type Uri or ExcelHyperlink the Hyperlink property is set. - internal void SetRangeValueInner(int fromRow, int fromColumn, int toRow, int toColumn, object[,] values, bool setHyperLinkFromValue) + internal void SetRangeValueInner(int fromRow, int fromColumn, int toRow, int toColumn, object[,] values, bool setHyperLinkFromValue, bool addHyperlinkStyles = false) { if (setHyperLinkFromValue) { - SetValuesWithHyperLink(fromRow, fromColumn, values); + SetValuesWithHyperLink(fromRow, fromColumn, values, addHyperlinkStyles); } else { @@ -3507,11 +3508,12 @@ internal void SetRangeValueInner(int fromRow, int fromColumn, int toRow, int toC _metadataStore.Clear(fromRow, fromColumn, values.GetUpperBound(0) + 1, values.GetUpperBound(1) + 1); } - private void SetValuesWithHyperLink(int fromRow, int fromColumn, object[,] values) + private void SetValuesWithHyperLink(int fromRow, int fromColumn, object[,] values, bool addHyperlinkStyles) { var rowBound = values.GetUpperBound(0); var colBound = values.GetUpperBound(1); + var hyperlinkStylesAdded = false; for (int r = 0; r <= rowBound; r++) { for (int c = 0; c <= colBound; c++) @@ -3527,6 +3529,18 @@ private void SetValuesWithHyperLink(int fromRow, int fromColumn, object[,] value var t = v.GetType(); if (t == typeof(Uri) || t == typeof(ExcelHyperLink)) { + if (!hyperlinkStylesAdded && addHyperlinkStyles) + { + if (!Workbook.Styles.NamedStyles.ExistsKey("Hyperlink")) + { + var hls = Workbook.Styles.CreateNamedStyle("Hyperlink"); + hls.BuildInId = 8; + hls.Style.Font.UnderLine = true; + hls.Style.Font.Color.SetColor(System.Drawing.Color.FromArgb(0x0563C1)); + } + hyperlinkStylesAdded = true; + } + Cells[row, col].StyleName = "Hyperlink"; _hyperLinks.SetValue(row, col, (Uri)v); if (v is ExcelHyperLink hl) { diff --git a/src/EPPlus/LoadFunctions/ColumnInfo.cs b/src/EPPlus/LoadFunctions/ColumnInfo.cs index 6c67376cd..6e0df8809 100644 --- a/src/EPPlus/LoadFunctions/ColumnInfo.cs +++ b/src/EPPlus/LoadFunctions/ColumnInfo.cs @@ -45,6 +45,8 @@ public ColumnInfo() public string NumberFormat { get; set; } + public bool UseBuiltInHyperlinkStyle { get; set; } + public RowFunctions TotalsRowFunction { get; set; } public string TotalsRowFormula { get; set; } diff --git a/src/EPPlus/LoadFunctions/LoadFunctionBase.cs b/src/EPPlus/LoadFunctions/LoadFunctionBase.cs index cefa46ddc..f03142da5 100644 --- a/src/EPPlus/LoadFunctions/LoadFunctionBase.cs +++ b/src/EPPlus/LoadFunctions/LoadFunctionBase.cs @@ -29,8 +29,12 @@ public LoadFunctionBase(ExcelRangeBase range, LoadFunctionFunctionParamsBase par PrintHeaders = parameters.PrintHeaders; TableStyle = parameters.TableStyle; TableName = parameters.TableName?.Trim(); + + _useBuiltInStylesForHyperlinks = parameters.UseBuiltInStylesForHyperlinks; } + private readonly bool _useBuiltInStylesForHyperlinks; + /// /// The range to which the data should be loaded /// @@ -95,7 +99,7 @@ internal ExcelRangeBase Load() } else { - ws.SetRangeValueInner(Range._fromRow, Range._fromCol, Range._fromRow + nRows - 1, Range._fromCol + nCols - 1, values, true); + ws.SetRangeValueInner(Range._fromRow, Range._fromCol, Range._fromRow + nRows - 1, Range._fromCol + nCols - 1, values, true, _useBuiltInStylesForHyperlinks); } diff --git a/src/EPPlus/LoadFunctions/Params/LoadFunctionFunctionParamsBase.cs b/src/EPPlus/LoadFunctions/Params/LoadFunctionFunctionParamsBase.cs index 2d89aa9a3..8587c22c5 100644 --- a/src/EPPlus/LoadFunctions/Params/LoadFunctionFunctionParamsBase.cs +++ b/src/EPPlus/LoadFunctions/Params/LoadFunctionFunctionParamsBase.cs @@ -46,5 +46,15 @@ public TableStyles? TableStyle { get; set; } = null; + + /// + /// If true, EPPlus will add the built in (default) styles for hyperlinks and apply them on any member + /// that is of the or types. Default value is true. + /// + public bool UseBuiltInStylesForHyperlinks + { + get; + set; + } = true; } } diff --git a/src/EPPlusTest/LoadFunctions/LoadFromCollectionTests.cs b/src/EPPlusTest/LoadFunctions/LoadFromCollectionTests.cs index fd7deec81..1fd302a80 100644 --- a/src/EPPlusTest/LoadFunctions/LoadFromCollectionTests.cs +++ b/src/EPPlusTest/LoadFunctions/LoadFromCollectionTests.cs @@ -45,6 +45,30 @@ namespace EPPlusTest.LoadFunctions [TestClass] public class LoadFromCollectionTests : TestBase { + [EpplusTable(AutofitColumns = true, PrintHeaders = true, TableStyle = TableStyles.Light10)] + internal class Company + { + public Company(int id, string name, Uri url) + { + Id = id; + Name = name; + Url = url; + } + + [EpplusTableColumn(Header = "Id", Order = 1)] + public int Id + { + get; set; + } + + [EpplusTableColumn(Header = "Name", Order = 2)] + public string Name { get; set; } + + [EpplusTableColumn(Header = "Homepage", Order = 3)] + public Uri Url { get; set; } + + } + internal abstract class BaseClass { public string Id { get; set; } @@ -504,5 +528,19 @@ public void LoadListOfClassWithEnumWithDescription() } } + [TestMethod] + public void LoadWithAttributesTest() + { + var l = new List(); + l.Add(new Company(1, "EPPlus Software AB", new Uri("https://epplussoftware.com"))); + + using (var package = OpenPackage("LoadFromCollectionAttr.xlsx", true)) + { + var sheet = package.Workbook.Worksheets.Add("test"); + sheet.Cells["A1"].LoadFromCollection(l, x => x.UseBuiltInStylesForHyperlinks = true); + + SaveAndCleanup(package); + } + } } }