2013-02-28 46 views
2

我想將excel圖表導出爲Word(Excel圖表),而不是圖片。由於圖片是非常容易與Chart.CopyPictureC#將Excel圖表導出爲Word對象(不是圖片)

我使用AddOleObject,但它給出了關於不能鏈接文件的錯誤。這是代碼:

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); 

我看過很多關於將excel導入word的帖子,但沒有任何uptodate或那個作品。

例如:

This one是什麼我正嘗試做,但不是1張與圖表的工作簿時,我有一本書有許多圖表,所以我插入圖表像file.xls! chartSheetname

This other使用複製和粘貼,只有它插入單元格,而不是圖表。使用圖表,複製方法將圖表複製到工作簿中的其他位置...

並使用this one將單詞添加到ole對象,將該對象轉換爲工作簿,創建工作表並將數據添加到該工作表。但我已經有了圖表...我只想插入現有的圖表...

回答

0

問題是我在使用Office 2010,當您打開.doc時,它會在兼容模式下打開,看來你不能在兼容模式下將.xlsx中的對象插入到.doc中。因此,我將.doc轉換爲.docx,將其打開爲.docx,然後插入它:

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);