C#将Excel图表作为对象(而不是图片)导出到Word [英] C# Export Excel Chart as object (not as picture) to Word

查看:710
本文介绍了C#将Excel图表作为对象(而不是图片)导出到Word的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Excel图表作为对象(Excel图表)而不是图片导出到Word.由于使用Chart.CopyPicture图片非常容易

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

我正在使用AddOleObject,但它给出了有关无法链接文件的错误.这是代码:

I´m using AddOleObject, but it gives an error about not being able to link the file. This is the code:

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的文章,但是没有最新的内容或有效的方法.

I´ve seen a lot of posts about importing excel to word, but nothing uptodate or that works.

例如:

这一个是我正在尝试的方法,尽管我有一本书包含很多图表,而不是一张一张带有图表的工作簿,所以我插入了诸如file.xls!chartSheetname

This one is what I´m trying to do, although instead of a workbook of 1 sheet with a chart, I have a book with many charts so I insert the chart like file.xls!chartSheetname

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...

并使用此一个,它将为单词添加一个ole对象,将该对象转换为工作簿,创建工作表并将数据添加到该工作表.但是我已经有了图表...我只想插入一个现有的图表...

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...

推荐答案

问题是我正在使用Office 2010,当您打开.doc时,它会以兼容模式打开,并且似乎您不能在兼容模式下从.doc中的.xlsx插入对象.因此,我将.doc转换为.docx,将其打开为.docx,然后将其插入:

The problem was that I was using Office 2010, and when you open a .doc, it opens in Compatibilty Mode, and it seems you can´t insert a object from a .xlsx in a .doc in Compatibility Mode. So, I converted the .doc in .docx, open it as .docx and then insert it:

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

这篇关于C#将Excel图表作为对象(而不是图片)导出到Word的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆