I am working on a project which can create a new powerpoint through c# window application.
I meet some problems during insert a chart into a slide.
when you want to insert a chart into a slide, there is a default data source for you, I already realize change the data in the excel, but I can't change the data range which really bothers me.
the code is :
slide = pptApp.Presentations[presentationCount].Slides[slideCount];
shapeCount = slide.Shapes.Count;
PPT.Shape shape;
shape = slide.Shapes.AddChart2(typenum, type, left, top, width, height,false);
PPT.Chart chart = slide.Shapes[shapeCount + 1].Chart;
object missing = System.Reflection.Missing.Value;
chart.ChartData.Activate();
int num = kinds.Count();
Excel.Workbook workbook = chart.ChartData.Workbook;
Excel.Worksheet sheet = chart.ChartData.Workbook.Worksheets["Sheet1"];
sheet.Cells.Clear();
Excel.Range range;
object[] objHeaders = { "数量", "数据1" };
range = sheet.get_Range("A1", "B1");
range.set_Value(Type.Missing, objHeaders);
var data = new object[num, 2];
foreach(int n in Enumerable.Range(0,num))
{
data[n, 0] = kinds[n];
data[n, 1] = values[n];
}
range = sheet.get_Range("A2", "B" + (num + 1));
sheet.get_Range("A2", "B" + (num + 1)).Value = data;
sheet.get_Range("B1").Value = title;
chart.ChartWizard(sheet.get_Range("A1", "B5"), missing, missing, Microsoft.Office.Interop.Excel.XlRowCol.xlColumns, 1, 1, true, "多糖商品销量分析", "月份", "销量", missing);
for (int i = 1; i < 2; i++)
{
PPT.Series series = chart.SeriesCollection(i);
for (int j = 1; j <= num; j++)
{
PPT.Point point = series.Points(j);
point.Format.Fill.ForeColor.RGB = color[j-1];
}
}
I want to use chart.ChartWizard(), but there is always a mistake:
Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))