我需要使用 vs 2010、c# 以编程方式在 Word 2007 文档中插入或编辑饼图...我发现的所有内容都显示了如何将这些旧的丑陋图表放入 Word 文档中。所以我现在想知道是否有可能操纵更新、更好看的图表。
这个展示了如何做我想要的事情,只是它是旧的丑陋的图表...... http://msdn.microsoft.com/en-us/library/ms178766.aspx。它告诉您插入一个 OLE 对象,这是古老的 msgraph.chart.8 的东西。
除了使用更新的图表样式之外,我已经能够做我需要做的一切。
这是一些代码。我构建了一个新的饼图,现在如何将其插入到 Word 文档中?我的 PieChart3D 类基于这些 http://code.msdn.microsoft.com/mschart
// here's my c#.net
private void CreateChart(string title, Microsoft.Office.Interop.Word.Application oWord, Microsoft.Office.Interop.Word.Document oDoc, ChartType chartType, Hashtable values)
{
PieChart3D chart1 = new PieChart3D(); // using System.Windows.Forms.DataVisualization.Charting
chart1.PieChart3D_Load(values);
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
Microsoft.Office.Interop.Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
// None of these work!!!!
wrdRng.InlineShapes.AddOLEControl(chart1);
wrdRng.InlineShapes.AddChart(chart1);
wrdRng.InlineShapes.AddOLEObject(chart1);
return;
}
// done with code
它在我看来,我只需要最后一步将其注入到文档中。我缺少什么?
I need to programmatically insert, or edit, a pie chart in a Word 2007 document, using vs 2010, c# ... everything I have found shows how to put these old ugly charts into a word doc. so I am now wondering if it is even possible to manipulate the newer and better looking charts.
This one shows how to do just what I want, only it's the old ugly charts.... http://msdn.microsoft.com/en-us/library/ms178766.aspx. It tells you to insert a OLE object, and it's the ancient msgraph.chart.8 stuff.
I've been able to do everything I need to do except use the newer style of charts.
Here's some of the code. I built a new pie chart and now how do I insert it into the Word document? My PieChart3D class is based on these http://code.msdn.microsoft.com/mschart
// here's my c#.net
private void CreateChart(string title, Microsoft.Office.Interop.Word.Application oWord, Microsoft.Office.Interop.Word.Document oDoc, ChartType chartType, Hashtable values)
{
PieChart3D chart1 = new PieChart3D(); // using System.Windows.Forms.DataVisualization.Charting
chart1.PieChart3D_Load(values);
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
Microsoft.Office.Interop.Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
// None of these work!!!!
wrdRng.InlineShapes.AddOLEControl(chart1);
wrdRng.InlineShapes.AddChart(chart1);
wrdRng.InlineShapes.AddOLEObject(chart1);
return;
}
// done with code
It seems to me I only need the last step of injecting it into the document. What am I missing?
发布评论
评论(3)
您可能应该在“AddOLEControl()”调用中添加缺少的参数,以便更好地衡量。
如何使用 AddOLEObject 将 Excel 图表插入到 Word 中
http://support.microsoft.com/kb/316384
You should probably add the missing parameters in your 'AddOLEControl()' call just for good measure.
How to insert an excel chart into Word using AddOLEObject
http://support.microsoft.com/kb/316384
这些选项实际上都不适合您。 AddChart 用于创建 Word 已知的图表 - 您必须使用已知的图表类型之一。 AddOLEControl 和 AddOLEObject 都要求为 COM 注册 Chart 类。当您调用该方法时,它会创建该类的新实例并将其添加到表单中。您无法使用这些方法添加在 C# 中创建的现有图表。
我认为最好的选择是使用 SaveImage 方法将图表保存到文件中,然后使用 AddPicture 方法。您将无法在Word中修改图表,您必须删除它并重新添加它,但至少您可以显示您创建的图表。
None of these options will actually work for you. AddChart is used to create a chart that Word already knows about - you have to use one of the known chart types. AddOLEControl and AddOLEObject both require your Chart class to be registered for COM. When you call the method, it creates a new instance of the class and adds it to the form. You can't use these methods to add an existing chart that you created in C#.
I think your best bet is to save your chart to a file using the SaveImage method, then add it to your WORD document using the AddPicture method. You won't be able to modify the chart in Word, you'll have to delete it and re-add it, but at least you can display the chart you created.
我找到了示例代码。
http://www.codeproject.com/Articles /188909/Updating-Charts-in-Word-Document-using-OpenXML
在此代码中,首先创建一个 Word 文档。您可以在word文档中设置其样式。然后您只需调用一个过程即可更新图表。希望有帮助。
I found a sample code.
http://www.codeproject.com/Articles/188909/Updating-Charts-in-Word-Document-using-OpenXML
In this code create a word cocument first. You can set its style in the word document. Then you can update the chart with just one procedure call. hope it helps.