The word document cannot be opened after adding excel charts to it using OpenXml

会有一股神秘感。 提交于 2019-12-25 01:14:20

问题


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 ?

来源:https://stackoverflow.com/questions/57391847/the-word-document-cannot-be-opened-after-adding-excel-charts-to-it-using-openxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!