Skip to content

Commit 93f7167

Browse files
committed
Added comment/threaded comment sample for v5.3
1 parent 2cdcde0 commit 93f7167

File tree

8 files changed

+257
-5
lines changed

8 files changed

+257
-5
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*************************************************************************************************
2+
Required Notice: Copyright (C) EPPlus Software AB.
3+
This software is licensed under PolyForm Noncommercial License 1.0.0
4+
and may only be used for noncommercial purposes
5+
https://polyformproject.org/licenses/noncommercial/1.0.0/
6+
7+
A commercial license to use this software can be purchased at https://epplussoftware.com
8+
*************************************************************************************************
9+
Date Author Change
10+
*************************************************************************************************
11+
07/22/2020 EPPlus Software AB EPPlus 5.2.1
12+
*************************************************************************************************/
13+
using Newtonsoft.Json;
14+
using Newtonsoft.Json.Linq;
15+
using OfficeOpenXml;
16+
using OfficeOpenXml.LoadFunctions.Params;
17+
using OfficeOpenXml.Table;
18+
using System;
19+
using System.Collections.Generic;
20+
using System.Dynamic;
21+
using System.IO;
22+
using System.Text;
23+
24+
namespace EPPlusSamples.LoadingData
25+
{
26+
public static class LoadingDataWithDynamicObjects
27+
{
28+
public static void Run()
29+
{
30+
// Create a list of dynamic objects
31+
dynamic p1 = new ExpandoObject();
32+
p1.Id = 1;
33+
p1.FirstName = "Ivan";
34+
p1.LastName = "Horvat";
35+
p1.Age = 21;
36+
dynamic p2 = new ExpandoObject();
37+
p2.Id = 2;
38+
p2.FirstName = "John";
39+
p2.LastName = "Doe";
40+
p2.Age = 45;
41+
dynamic p3 = new ExpandoObject();
42+
p3.Id = 3;
43+
p3.FirstName = "Sven";
44+
p3.LastName = "Svensson";
45+
p3.Age = 68;
46+
47+
List<ExpandoObject> items = new List<ExpandoObject>()
48+
{
49+
p1,
50+
p2,
51+
p3
52+
};
53+
54+
// Create a workbook with a worksheet and load the data into a table
55+
using(var package = new ExcelPackage(FileOutputUtil.GetFileInfo("04-LoadDynamicObjects.xlsx")))
56+
{
57+
var sheet = package.Workbook.Worksheets.Add("Dynamic");
58+
sheet.Cells["A1"].LoadFromDictionaries(items, c =>
59+
{
60+
// Print headers using the property names
61+
c.PrintHeaders = true;
62+
// insert a space before each capital letter in the header
63+
c.HeaderParsingType = HeaderParsingTypes.CamelCaseToSpace;
64+
// when TableStyle is not TableStyles.None the data will be loaded into a table with the
65+
// selected style.
66+
c.TableStyle = TableStyles.Medium1;
67+
});
68+
package.Save();
69+
}
70+
71+
// Load data from json (in this case a file)
72+
var jsonItems = JsonConvert.DeserializeObject<IEnumerable<ExpandoObject>>(File.ReadAllText(FileInputUtil.GetFileInfo("04-LoadingData", "testdata.json").FullName));
73+
using (var package = new ExcelPackage(FileOutputUtil.GetFileInfo("04-LoadJsonFromFile.xlsx")))
74+
{
75+
var sheet = package.Workbook.Worksheets.Add("Dynamic");
76+
sheet.Cells["A1"].LoadFromDictionaries(jsonItems, c =>
77+
{
78+
// Print headers using the property names
79+
c.PrintHeaders = true;
80+
// insert a space before each capital letter in the header
81+
c.HeaderParsingType = HeaderParsingTypes.CamelCaseToSpace;
82+
// when TableStyle is not TableStyles.None the data will be loaded into a table with the
83+
// selected style.
84+
c.TableStyle = TableStyles.Medium1;
85+
});
86+
sheet.Cells["D:D"].Style.Numberformat.Format = "yyyy-mm-dd";
87+
sheet.Cells[1, 1, sheet.Dimension.End.Row, sheet.Dimension.End.Column].AutoFitColumns();
88+
package.Save();
89+
}
90+
}
91+
}
92+
}
File renamed without changes.

23-Comments/CommentsSample.cs

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using OfficeOpenXml;
2+
using OfficeOpenXml.ThreadedComments;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Drawing;
6+
using System.Text;
7+
using System.Threading;
8+
9+
namespace EPPlusSamples.Comments
10+
{
11+
public class CommentsSample
12+
{
13+
public static void Run()
14+
{
15+
using(var package = new ExcelPackage())
16+
{
17+
// Comments/Notes
18+
var sheet1 = package.Workbook.Worksheets.Add("Comments");
19+
AddComments(sheet1);
20+
// Threaded comments
21+
var sheet2 = package.Workbook.Worksheets.Add("ThreadedComments");
22+
AddAndReadThreadedComments(sheet2);
23+
package.SaveAs(FileOutputUtil.GetFileInfo("23-Comments.xlsx"));
24+
}
25+
}
26+
27+
private static void AddComments(ExcelWorksheet ws)
28+
{
29+
Console.WriteLine("Sample 23 - Comment/Note");
30+
//Add Comments using the range class
31+
var comment = ws.Cells["A3"].AddComment("Jan Källman:\r\n", "JK");
32+
comment.Font.Bold = true;
33+
var rt = comment.RichText.Add("This column contains the extensions.");
34+
rt.Bold = false;
35+
comment.AutoFit = true;
36+
37+
//Add a comment using the Comment collection
38+
comment = ws.Comments.Add(ws.Cells["B3"], "This column contains the size of the files.", "JK");
39+
//This sets the size and position. (The position is only when the comment is visible)
40+
comment.From.Column = 7;
41+
comment.From.Row = 3;
42+
comment.To.Column = 16;
43+
comment.To.Row = 8;
44+
comment.BackgroundColor = Color.White;
45+
comment.RichText.Add("\r\nTo format the numbers use the Numberformat-property like:\r\n");
46+
47+
ws.Cells["B3:B42"].Style.Numberformat.Format = "#,##0";
48+
49+
//Format the code using the RichText Collection
50+
var rc = comment.RichText.Add("//Format the Size and Count column\r\n");
51+
rc.FontName = "Courier New";
52+
rc.Color = Color.FromArgb(0, 128, 0);
53+
rc = comment.RichText.Add("ws.Cells[");
54+
rc.Color = Color.Black;
55+
rc = comment.RichText.Add("\"B3:B42\"");
56+
rc.Color = Color.FromArgb(123, 21, 21);
57+
rc = comment.RichText.Add("].Style.Numberformat.Format = ");
58+
rc.Color = Color.Black;
59+
rc = comment.RichText.Add("\"#,##0\"");
60+
rc.Color = Color.FromArgb(123, 21, 21);
61+
rc = comment.RichText.Add(";");
62+
rc.Color = Color.Black;
63+
Console.WriteLine("Comment added");
64+
Console.WriteLine();
65+
}
66+
67+
private static void AddAndReadThreadedComments(ExcelWorksheet sheet)
68+
{
69+
var persons = sheet.ThreadedComments.Persons;
70+
// Add a threaded comment author
71+
var user1 = persons.Add("Ernest Peter Plus");
72+
73+
74+
75+
// add a threaded comment to cell A1
76+
var thread = sheet.Cells["A1"].AddThreadedComment();
77+
thread.AddComment(user1.Id, "My first comment");
78+
// threaded comments can also be added via the worksheet:
79+
thread.AddComment(user1.Id, "My second comment");
80+
81+
// A workbook might have been opened by previous users that you will find in the ThreadedComments collection, could be from the AD and/or Office365.
82+
// Let's add another fictive user using the user id format of Office365.
83+
var user2 = persons.Add("John Doe", "S::[email protected]::e3e726c6-1401-473b-bc95-cb3e1c892d99", IdentityProvider.Office365);
84+
85+
// The Thread.Sleep(50) statements below is just to avoid that comments get the same timestamp when this sample runs
86+
87+
Thread.Sleep(50);
88+
// now we can add comments with mentions
89+
thread.AddComment(user2.Id, "Really great comments there, {0}", user1);
90+
Thread.Sleep(50);
91+
thread.AddComment(user1.Id, "Many thanks {0}!", user2);
92+
93+
// A third person joins
94+
var user3 = persons.Add("IT Support");
95+
// you can add multiple mentions in one comment like this
96+
Thread.Sleep(50);
97+
thread.AddComment(user3.Id, "Hello {0} and {1}, how can I help?", user1, user2);
98+
99+
Console.WriteLine("*** reading threaded comments ***");
100+
// Read threaded comments in a cell:
101+
foreach (var comment in sheet.Cells["A1"].ThreadedComment.Comments)
102+
{
103+
var author = persons[comment.PersonId];
104+
Console.WriteLine("{0} wrote at {1}", author.DisplayName, comment.DateCreated.ToString());
105+
Console.WriteLine(comment.Text);
106+
if (comment.Mentions != null)
107+
{
108+
foreach (var mention in comment.Mentions)
109+
{
110+
var personMentioned = persons[mention.MentionPersonId];
111+
Console.WriteLine("{0} was mentioned in a comment", personMentioned.DisplayName);
112+
Console.WriteLine("Identity provider: {0}", personMentioned.ProviderId.ToString());
113+
}
114+
}
115+
Console.WriteLine("***************************");
116+
}
117+
118+
// finally close the thread (can be opened again with the ReopenThread method)
119+
thread.ResolveThread();
120+
if (thread.IsResolved)
121+
{
122+
Console.WriteLine("The thread is now resolved!");
123+
}
124+
125+
// for backward compatibility a comment/note is created in a cell containing a threaded comment
126+
// if threaded comments is not supported the user will see this comment instead
127+
var legacyComment = sheet.Cells["A1"].Comment;
128+
Console.WriteLine("Legacy comment text: {0}", legacyComment.Text);
129+
130+
// add a thread in cell B1, add a comment
131+
var thread2 = sheet.ThreadedComments.Add("B1");
132+
var c = thread2.AddComment(user1.Id, "Hello");
133+
Console.WriteLine("B1 now contains a thread with {0} comment", thread2.Comments.Count);
134+
// remove the comment
135+
thread2.Remove(c);
136+
if (thread2.Comments.Count == 0)
137+
Console.WriteLine("B1 is now empty");
138+
}
139+
}
140+
}

23-Comments/Readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 23 - Comments/Notes and Threaded comments
2+
These samples demonstrates the comments/threaded comments functionality of EPPlus.
3+
4+
### [CommentsSample.cs](CommentsSample.cs)
5+
6+
---
7+
[Back to overview](/Readme.md)

EPPlus.Sample.NET.csproj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,11 @@
4242
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
4343
<HintPath>packages\EntityFramework.6.3.0\lib\net45\EntityFramework.SqlServer.dll</HintPath>
4444
</Reference>
45-
<Reference Include="EPPlus, Version=5.2.0.0, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL">
46-
<HintPath>packages\EPPlus.5.2.0\lib\net45\EPPlus.dll</HintPath>
45+
<Reference Include="EPPlus, Version=5.2.1.259, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL">
46+
<HintPath>packages\EPPlus.5.2.1.259-20200818-develop\lib\net45\EPPlus.dll</HintPath>
47+
</Reference>
48+
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
49+
<HintPath>packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
4750
</Reference>
4851
<Reference Include="PresentationCore" />
4952
<Reference Include="System" />
@@ -73,7 +76,8 @@
7376
<Compile Include="01-GettingStarted\GettingStartedSample.cs" />
7477
<Compile Include="02-ReadWorkbook\ReadWorkbookSample.cs" />
7578
<Compile Include="03-UsingAsyncAwait\UsingAsyncAwaitSample.cs" />
76-
<Compile Include="04-LoadingDataWithTables\LoadingDataWithTablesSample.cs" />
79+
<Compile Include="04-LoadingData\LoadingDataWithDynamicObjects.cs" />
80+
<Compile Include="04-LoadingData\LoadingDataWithTablesSample.cs" />
7781
<Compile Include="05-ImportAndExportCsvFiles\ImportAndExportCsvFilesSample.cs" />
7882
<Compile Include="06-FormulaCalculation\AddFormulaFunction.cs" />
7983
<Compile Include="06-FormulaCalculation\BuildAndCalculateWorkbook.cs" />
@@ -108,6 +112,7 @@
108112
<Compile Include="20-CreateFileSystemReport\CreateAFileSystemReport.cs" />
109113
<Compile Include="21-VBA\WorkingWithVbaSample.cs" />
110114
<Compile Include="22-IgnoreErrors\IgnoreErrorsSample.cs" />
115+
<Compile Include="23-Comments\CommentsSample.cs" />
111116
<Compile Include="FileInputUtil.cs" />
112117
<Compile Include="FileOutputUtil.cs" />
113118
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -118,7 +123,7 @@
118123
<None Include="02-ReadWorkbook\Readme.md" />
119124
<None Include="02-ReadWorkbook\ReadWorkbook.xlsx" />
120125
<None Include="03-UsingAsyncAwait\Readme.md" />
121-
<None Include="04-LoadingDataWithTables\Readme.md" />
126+
<None Include="04-LoadingData\Readme.md" />
122127
<None Include="05-ImportAndExportCsvFiles\Readme.md" />
123128
<None Include="06-FormulaCalculation\FormulaCalcSample.xlsx" />
124129
<None Include="06-FormulaCalculation\Readme.md" />
@@ -141,6 +146,7 @@
141146
<None Include="20-CreateFileSystemReport\Readme.md" />
142147
<None Include="21-VBA\Readme.md" />
143148
<None Include="22-IgnoreErrors\Readme.md" />
149+
<None Include="23-Comments\Readme.md" />
144150
<None Include="App.config" />
145151
<None Include="EPPlusSample.sqlite">
146152
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

Sample_Main.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Date Author Change
2828
using EPPlusSamples.PivotTables;
2929
using EPPlusSamples.SalesReport;
3030
using EPPlusSamples.Sparklines;
31+
using EPPlusSamples.Comments;
3132

3233
namespace EPPlusSamples
3334
{
@@ -196,6 +197,11 @@ await ChartsAndThemesSample.RunAsync(connectionStr,
196197
IgnoreErrorsSample.Run();
197198
Console.WriteLine("Sample 22 created {0}", FileOutputUtil.OutputDir.Name);
198199
Console.WriteLine();
200+
201+
//Sample 23 - Comments and Threaded comments
202+
Console.WriteLine("Running sample 23-Comments/Notes and Threaded Comments");
203+
CommentsSample.Run();
204+
Console.WriteLine("Sample 23 created {0}", FileOutputUtil.OutputDir.Name);
199205
}
200206
catch (Exception ex)
201207
{

packages.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="EntityFramework" version="6.3.0" targetFramework="net472" />
4-
<package id="EPPlus" version="5.2.0" targetFramework="net472" />
4+
<package id="EPPlus" version="5.2.1.259-20200818-develop" targetFramework="net472" />
5+
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net472" />
56
<package id="System.Data.SQLite" version="1.0.112.0" targetFramework="net472" />
67
<package id="System.Data.SQLite.Core" version="1.0.112.0" targetFramework="net472" />
78
<package id="System.Data.SQLite.EF6" version="1.0.112.0" targetFramework="net472" />

0 commit comments

Comments
 (0)