简体   繁体   中英

Excel Chart export into PowerPoint export

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;

This seemed to work for me when updating an existing chart:

Chart.Application.Update();

I'd be interested to see if this works for you while inserting a new chart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM