I tried to append excel charts to a word document using OpenXml. But after that when I'm trying to open the document it says "Sorry we can't open the file.docx because we found a problem with its contents" This is my code
public void excelToImages(string excelSource, string wordSource)
{using (SpreadsheetDocument document = SpreadsheetDocument.Open(excelSource, true))
{
WorkbookPart workbookPart = document.WorkbookPart;
string relId = workbookPart.Workbook.Descendants<Sheet>().First(s => "Report".Equals(s.Name)).Id;
var workSheet = (WorksheetPart)workbookPart.GetPartById(relId);
var chartParts = workSheet.DrawingsPart.ChartParts;
using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(wordSource, true))
{
MainDocumentPart mainPartWordDoc = wdDoc.MainDocumentPart;
foreach (var chart in chartParts)
{
mainPartWordDoc.Document.Body.Append((chart as ChartPart).ChartSpace as OpenXmlElement);
}
mainPartWordDoc.Document.Save();
}
}
}
Why can't I open the file ?