C# Export Excel Chart as object (not as picture) to Word

匿名 (未验证) 提交于 2019-12-03 01:38:01

问题:

I want to export an excel chart to Word as an object (Excel Chart), not as a picture. As picture is very easy with Chart.CopyPicture

Document doc; // Existing document string chartSheet = xlsFilePath + "!" + chartSheetName; string type = "Excel.Chart.8"; InlineShape shape = doc.Content.InlineShapes.AddOLEObject(ClassType: type, FileName: chartSheet, LinkToFile: false, DisplayAsIcon: false);

For example:

This one

This other uses copy and paste, only it inserts cells, not a chart. With chart, copy method copies the chart to antoher location in the workbook...

And with this one it adds an ole object to word, casts the object to a workbook, creates a worksheet and add data to that worksheet. But I already have the chart... I only want to insert an existing chart...

回答1:

The problem was that I was using Office 2010, and when you open a .doc, it opens in Compatibilty Mode

Word.Application wordAPP = new Word.Application(); Word.Document docx = wordAPP.Documents.Open(docFile, false, true, false, Format:Word.WdOpenFormat.wdOpenFormatXMLDocument); Chart chart = xlsx.Charts[chartSheetName]; chart.ChartArea.Select(); chart.ChartArea.Copy(); docx.Application.Selection.PasteAndFormat(Word.WdRecoveryType.wdFormatOriginalFormatting);


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