Excel 图表导出到 PowerPoint 导出
我在尝试通过以下方式在 PowerPoint 中嵌入 Excel 图表时遇到问题 使用 C# 的互操作 API。我需要让它在 Office2003 上工作。
我遵循的过程是从模板创建 PowerPoint 使用互操作 API。然后我为每个图表插入一张新幻灯片,然后 在这些幻灯片中我添加了 Excel.Chart OLE 对象。到目前为止一切都很好。然后我就走了 进入该工作簿,更改数据,然后使用图表向导更新 第一张纸上的图表。最后我关闭Excel工作簿。
问题是图表在生成的中无法正确显示 PowerPoint 幻灯片。该图表已根据更改后的数据进行了调整,但所有 对图表本身的操作,例如更改标题、数据范围、 图表类型等未显示。
它变得陌生了。在 PowerPoint 中,我单击(错误呈现) 生成图表并选择打开或编辑,图表会立即刷新到 正确的设置。当我关闭图表并返回到 powerpoint 时, 图表渲染正确。
我花了好几个小时来弄清楚发生了什么事,但不知何故 似乎没有任何作用。我尝试
- 在所有图表之后调用:PowerPoint.Application.ActivePresenation.UpdateLinks() 生成(这是 OLE 问题吗?)。
- 图表.刷新();
- 工作簿.RefreshAll();
有人可以给我一个提示,告诉我需要做什么吗?我将发布我的代码 插入下面的图表。
//Add the Workbook
Excel.Workbook workbook =
(Excel.Workbook)slide.Shapes.AddOLEObject(basePoint.X, basePoint.Y,
chartSize.Width, chartSize.Height,
MsoTriState.msoFalse).OLEFormat.Object;
//update the worksheet with data
Excel.Worksheet datasheet =
(Excel.Worksheet)workbook.Worksheets["Sheet1"];
datasheet.Cells.ClearContents();
//set table contents
//<<SNIP code that fills data sheet with contents from our custom
dataTable object>>
//fetch chart
Excel.Chart chart = (Excel.Chart)workbook.Charts["Chart1"];
Excel.Range range = datasheet.get_Range(datasheet.Cells[1, 1],
datasheet.Cells[dataTable.RowCount + 1, dataTable.ColumnCount + 1]);
chart.ChartWizard(range, Excel.XlChartType.xlBarStacked, Type.Missing,
Excel.XlRowCol.xlRows, 1, 1, true, dataTable.ChartTitle, "", "", "");
//release objects
datasheet = null;
chart = null;
range = null;
workbook.Close(false, Type.Missing, Type.Missing);
workbook = null;
I have an issue with trying to embed a Excel Charts in PowerPoint via the
Interop API's using C#. I need to get this to work for Office2003.
The process I'm following is that I create a PowerPoint from a template
using the interop API's. Then I insert a new slide for each chart, and on
these slides I add a Excel.Chart OLE Object. All is well so far. Then I go
into that workbook, change the data, and use the chartWizard to update the
chart on the first sheet. Finally I close the excel workbook.
The issue is that the chart does not show correctly in the generated
PowerPoint slides. The chart is adapted with the changed data, but all
actions on the chart itself, such as changing the title, the data range,
chart type, etc. are not shown.
It becomes stranger. When in PowerPoint I click the (incorrectly rendered)
generated chart and choose open or edit, the chart will immediatly refresh to
the correct settings. When I then close the chart and go back to powerpoint,
the chart is rendered correctly.
I've spend hours and hours to figure out what is going on, but somehow
nothing seems to work. I've tried to call:
- PowerPoint.Application.ActivePresenation.UpdateLinks() after all charts
are generated (is it a OLE issue?). - Chart.Refresh();
- Workbook.RefreshAll();
Could someone give me a hint on what needs to be done? I'll post my code
that inserts the charts below.
//Add the Workbook
Excel.Workbook workbook =
(Excel.Workbook)slide.Shapes.AddOLEObject(basePoint.X, basePoint.Y,
chartSize.Width, chartSize.Height,
MsoTriState.msoFalse).OLEFormat.Object;
//update the worksheet with data
Excel.Worksheet datasheet =
(Excel.Worksheet)workbook.Worksheets["Sheet1"];
datasheet.Cells.ClearContents();
//set table contents
//<<SNIP code that fills data sheet with contents from our custom
dataTable object>>
//fetch chart
Excel.Chart chart = (Excel.Chart)workbook.Charts["Chart1"];
Excel.Range range = datasheet.get_Range(datasheet.Cells[1, 1],
datasheet.Cells[dataTable.RowCount + 1, dataTable.ColumnCount + 1]);
chart.ChartWizard(range, Excel.XlChartType.xlBarStacked, Type.Missing,
Excel.XlRowCol.xlRows, 1, 1, true, dataTable.ChartTitle, "", "", "");
//release objects
datasheet = null;
chart = null;
range = null;
workbook.Close(false, Type.Missing, Type.Missing);
workbook = null;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在更新现有图表时,这似乎对我有用:
我有兴趣看看这在插入新图表时是否对您有用。
This seemed to work for me when updating an existing chart:
I'd be interested to see if this works for you while inserting a new chart.