The word document cannot be opened after adding ex

2019-08-19 00:44发布

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 ?

0条回答
登录 后发表回答