I am having some problems working with comments in Excel docs using EPPlus. I can add comments and remove them, however sometimes it isn’t successful.
I am using the comments to display validation messages from the result of processing the Excel file, and when this file gets re-processed, I want to clear any existing comments before running the validation again.
My current method of clearing the comments is this:
_sheet.Cells.Style.Fill.PatternType = ExcelFillStyle.None;
while (_sheet.Comments.Count > 0)
{
// Note: not using _sheet.Comments.RemoveAt(0) since this can throw [Exception: Key does not exist] OfficeOpenXml.RangeCollection.Delete(UInt64 key)
_sheet.Comments.RemoveAt(_sheet.Comments.Count - 1);
}
Which seems to clear them fine. But when I try to add a comment as below:
const string author = "...";
cell.Style.Fill.PatternType = ExcelFillStyle.Solid;
cell.Style.Fill.BackgroundColor.SetColor(Color.LemonChiffon);
if (cell.Comment == null)
{
cell.AddComment(message, author);
}
else
{
cell.Comment.Text = message;
cell.Comment.Author = author;
}
where cell is an ExcelRange instance for a specific cell, I get the below stack trace:
System.NullReferenceException : Object reference not set to an instance of an object.
at OfficeOpenXml.Drawing.Vml.ExcelVmlDrawingCommentCollection.AddDrawing(ExcelRangeBase cell)
at OfficeOpenXml.Drawing.Vml.ExcelVmlDrawingCommentCollection.Add(ExcelRangeBase cell)
at OfficeOpenXml.ExcelComment..ctor(XmlNamespaceManager ns, XmlNode commentTopNode, ExcelRangeBase cell)
at OfficeOpenXml.ExcelCommentCollection.Add(ExcelRangeBase cell, String Text, String author)
at OfficeOpenXml.ExcelRangeBase.Set_Comment(Object value, Int32 row, Int32 col)
at OfficeOpenXml.ExcelRangeBase.SetSingle(_setValue valueMethod, Object value)
at OfficeOpenXml.ExcelRangeBase.AddComment(String Text, String Author)
Can anyone help me with this? What am I doing wrong?
Thanks,
James
I contacted Jan Kallman regarding this, and it turned out to be a bug with the VML drawings not being sorted, and was fixed in the patch / commit 5027f556c029 of the library.