Opening XLSX with Excel and saving it resolves the file issue but when I run my program it somehow brokes the excel file again on the second time. It's weird. So this error comes in third time when I run the program.
CODE:
public static void SetComment(ExcelRange cell, string text)
{
if (text != "")
{
if (cell.Comment == null) cell.AddComment(text, "CRM"); else cell.Comment.Text = text.Replace("<","").Replace(">","");
cell.Comment.AutoFit = true;
}
}
TRACE:
Unhandled Exception: System.Xml.XmlException: Unexpected end tag. Line 8721, pos
ition 7.
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean prese
rveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at OfficeOpenXml.XmlHelper.LoadXmlSafe(XmlDocument xmlDoc, Stream stream)
at OfficeOpenXml.Drawing.Vml.ExcelVmlDrawingBaseCollection..ctor(ExcelPackage
pck, ExcelWorksheet ws, Uri uri)
at OfficeOpenXml.Drawing.Vml.ExcelVmlDrawingCommentCollection..ctor(ExcelPack
age pck, ExcelWorksheet ws, Uri uri)
at OfficeOpenXml.ExcelWorksheet.CreateVmlCollection()
at OfficeOpenXml.ExcelWorksheet.get_Comments()
at OfficeOpenXml.ExcelRangeBase.get_Comment()
at CashflowUpdater.Program.SetComment(ExcelRange cell, String text)
at CashflowUpdater.Program.SaveData(ExcelWorkbook& wb)
at CashflowUpdater.Program.UpdateCashFlowXLS(DateTime StartDate, String FileP
ath)
at CashflowUpdater.Program.Main(String[] args)
Comments: I get the same error trying to access the Comment property of a Cell or trying to add a new comment using the AddComment(string, string) method.
worksheet.Cells["A1:A1"].AddComment("Testing", "Testing")' threw an exception of type 'System.Xml.XmlException'
base {System.SystemException}: {"The 'br' start tag on line 19 position 47 does not match the end tag of 'font'. Line 20, position 9."}
LineNumber: 20
LinePosition: 9
Message: "The 'br' start tag on line 19 position 47 does not match the end tag of 'font'. Line 20, position 9."
SourceUri: ""
Yet I can add and remove comments in the same worksheet using Microsoft Excel without any problems.
The full stack trace is:
at System.Xml.XmlTextReaderImpl.Throw(Exception e)
at System.Xml.XmlTextReaderImpl.Throw(String res, String[] args)
at System.Xml.XmlTextReaderImpl.ThrowTagMismatch(NodeData startTag)
at System.Xml.XmlTextReaderImpl.ParseEndElement()
at System.Xml.XmlTextReaderImpl.ParseElementContent()
at System.Xml.XmlTextReaderImpl.Read()
at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
at System.Xml.XmlDocument.Load(XmlReader reader)
at OfficeOpenXml.XmlHelper.LoadXmlSafe(XmlDocument xmlDoc, Stream stream)
at OfficeOpenXml.Drawing.Vml.ExcelVmlDrawingBaseCollection..ctor(ExcelPackage pck, ExcelWorksheet ws, Uri uri)
at OfficeOpenXml.Drawing.Vml.ExcelVmlDrawingCommentCollection..ctor(ExcelPackage pck, ExcelWorksheet ws, Uri uri)
at OfficeOpenXml.ExcelWorksheet.CreateVmlCollection()
at OfficeOpenXml.ExcelWorksheet.get_Comments()
at OfficeOpenXml.ExcelRangeBase.get_Comment()
at
(My code has been omitted from the trace)
This code does work if I have a brand new empty workbook with a brand new empty worksheet.
This workbook is an .xlsm with some VBA macro code and a button on the worksheet that activates the VBA macro code.
It also has some drop-down menus with lookups to data tables in other sheets and some various color, border and cell background highlighting.
The workbook doesn't really have anything too unusual and I don't know why EPPlus is choking when trying to read/write comments to a cell.
Please look into this and if absolutely necessary, I could supply the Excel workbook and some sample code that causes the error.
Thank you.