Skip to content

Commit 45d69d5

Browse files
committed
Merge branch 'develop' into release/epplus6.2.10
2 parents d2936a9 + 74be58f commit 45d69d5

File tree

6 files changed

+81
-4
lines changed

6 files changed

+81
-4
lines changed

src/EPPlus/Attributes/EpplusTableColumnAttributeBase.cs

+9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ public bool Hidden
6060
set;
6161
}
6262

63+
/// <summary>
64+
/// Indicates whether the Built in (default) hyperlink style should be
65+
/// applied to hyperlinks or not. Default value is true.
66+
/// </summary>
67+
public bool UseBuiltInHyperlinkStyle
68+
{
69+
get; set;
70+
} = true;
71+
6372
/// <summary>
6473
/// If not <see cref="RowFunctions.None"/> the last cell in the column (the totals row) will contain a formula of the specified type.
6574
/// </summary>

src/EPPlus/ExcelWorksheet.cs

+17-3
Original file line numberDiff line numberDiff line change
@@ -3490,12 +3490,13 @@ internal void SetValueStyleIdInner(int row, int col, object value, int styleId)
34903490
/// <param name="toRow">end row</param>
34913491
/// <param name="toColumn">end column</param>
34923492
/// <param name="values">set values</param>
3493+
/// <param name="addHyperlinkStyles">Will add built in styles for hyperlinks</param>
34933494
/// <param name="setHyperLinkFromValue">If the value is of type Uri or ExcelHyperlink the Hyperlink property is set.</param>
3494-
internal void SetRangeValueInner(int fromRow, int fromColumn, int toRow, int toColumn, object[,] values, bool setHyperLinkFromValue)
3495+
internal void SetRangeValueInner(int fromRow, int fromColumn, int toRow, int toColumn, object[,] values, bool setHyperLinkFromValue, bool addHyperlinkStyles = false)
34953496
{
34963497
if (setHyperLinkFromValue)
34973498
{
3498-
SetValuesWithHyperLink(fromRow, fromColumn, values);
3499+
SetValuesWithHyperLink(fromRow, fromColumn, values, addHyperlinkStyles);
34993500
}
35003501
else
35013502
{
@@ -3507,11 +3508,12 @@ internal void SetRangeValueInner(int fromRow, int fromColumn, int toRow, int toC
35073508
_metadataStore.Clear(fromRow, fromColumn, values.GetUpperBound(0) + 1, values.GetUpperBound(1) + 1);
35083509
}
35093510

3510-
private void SetValuesWithHyperLink(int fromRow, int fromColumn, object[,] values)
3511+
private void SetValuesWithHyperLink(int fromRow, int fromColumn, object[,] values, bool addHyperlinkStyles)
35113512
{
35123513
var rowBound = values.GetUpperBound(0);
35133514
var colBound = values.GetUpperBound(1);
35143515

3516+
var hyperlinkStylesAdded = false;
35153517
for (int r = 0; r <= rowBound; r++)
35163518
{
35173519
for (int c = 0; c <= colBound; c++)
@@ -3527,6 +3529,18 @@ private void SetValuesWithHyperLink(int fromRow, int fromColumn, object[,] value
35273529
var t = v.GetType();
35283530
if (t == typeof(Uri) || t == typeof(ExcelHyperLink))
35293531
{
3532+
if (!hyperlinkStylesAdded && addHyperlinkStyles)
3533+
{
3534+
if (!Workbook.Styles.NamedStyles.ExistsKey("Hyperlink"))
3535+
{
3536+
var hls = Workbook.Styles.CreateNamedStyle("Hyperlink");
3537+
hls.BuildInId = 8;
3538+
hls.Style.Font.UnderLine = true;
3539+
hls.Style.Font.Color.SetColor(System.Drawing.Color.FromArgb(0x0563C1));
3540+
}
3541+
hyperlinkStylesAdded = true;
3542+
}
3543+
Cells[row, col].StyleName = "Hyperlink";
35303544
_hyperLinks.SetValue(row, col, (Uri)v);
35313545
if (v is ExcelHyperLink hl)
35323546
{

src/EPPlus/LoadFunctions/ColumnInfo.cs

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public ColumnInfo()
4545

4646
public string NumberFormat { get; set; }
4747

48+
public bool UseBuiltInHyperlinkStyle { get; set; }
49+
4850
public RowFunctions TotalsRowFunction { get; set; }
4951

5052
public string TotalsRowFormula { get; set; }

src/EPPlus/LoadFunctions/LoadFunctionBase.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ public LoadFunctionBase(ExcelRangeBase range, LoadFunctionFunctionParamsBase par
2929
PrintHeaders = parameters.PrintHeaders;
3030
TableStyle = parameters.TableStyle;
3131
TableName = parameters.TableName?.Trim();
32+
33+
_useBuiltInStylesForHyperlinks = parameters.UseBuiltInStylesForHyperlinks;
3234
}
3335

36+
private readonly bool _useBuiltInStylesForHyperlinks;
37+
3438
/// <summary>
3539
/// The range to which the data should be loaded
3640
/// </summary>
@@ -95,7 +99,7 @@ internal ExcelRangeBase Load()
9599
}
96100
else
97101
{
98-
ws.SetRangeValueInner(Range._fromRow, Range._fromCol, Range._fromRow + nRows - 1, Range._fromCol + nCols - 1, values, true);
102+
ws.SetRangeValueInner(Range._fromRow, Range._fromCol, Range._fromRow + nRows - 1, Range._fromCol + nCols - 1, values, true, _useBuiltInStylesForHyperlinks);
99103
}
100104

101105

src/EPPlus/LoadFunctions/Params/LoadFunctionFunctionParamsBase.cs

+10
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,15 @@ public TableStyles? TableStyle
4646
{
4747
get; set;
4848
} = null;
49+
50+
/// <summary>
51+
/// If true, EPPlus will add the built in (default) styles for hyperlinks and apply them on any member
52+
/// that is of the <see cref="Uri"/> or <see cref="ExcelHyperLink"/> types. Default value is true.
53+
/// </summary>
54+
public bool UseBuiltInStylesForHyperlinks
55+
{
56+
get;
57+
set;
58+
} = true;
4959
}
5060
}

src/EPPlusTest/LoadFunctions/LoadFromCollectionTests.cs

+38
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ namespace EPPlusTest.LoadFunctions
4545
[TestClass]
4646
public class LoadFromCollectionTests : TestBase
4747
{
48+
[EpplusTable(AutofitColumns = true, PrintHeaders = true, TableStyle = TableStyles.Light10)]
49+
internal class Company
50+
{
51+
public Company(int id, string name, Uri url)
52+
{
53+
Id = id;
54+
Name = name;
55+
Url = url;
56+
}
57+
58+
[EpplusTableColumn(Header = "Id", Order = 1)]
59+
public int Id
60+
{
61+
get; set;
62+
}
63+
64+
[EpplusTableColumn(Header = "Name", Order = 2)]
65+
public string Name { get; set; }
66+
67+
[EpplusTableColumn(Header = "Homepage", Order = 3)]
68+
public Uri Url { get; set; }
69+
70+
}
71+
4872
internal abstract class BaseClass
4973
{
5074
public string Id { get; set; }
@@ -504,5 +528,19 @@ public void LoadListOfClassWithEnumWithDescription()
504528
}
505529
}
506530

531+
[TestMethod]
532+
public void LoadWithAttributesTest()
533+
{
534+
var l = new List<Company>();
535+
l.Add(new Company(1, "EPPlus Software AB", new Uri("https://epplussoftware.com")));
536+
537+
using (var package = OpenPackage("LoadFromCollectionAttr.xlsx", true))
538+
{
539+
var sheet = package.Workbook.Worksheets.Add("test");
540+
sheet.Cells["A1"].LoadFromCollection(l, x => x.UseBuiltInStylesForHyperlinks = true);
541+
542+
SaveAndCleanup(package);
543+
}
544+
}
507545
}
508546
}

0 commit comments

Comments
 (0)