|
| 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 | +} |
0 commit comments