Delphi 6 – 从delphi应用程序创buildExcel图表 – 在同一页面上的数据和图表

我需要基于表中的一些数据在Excel应用程序的Excel中创build一个图表。

我创build了Excel工作表,并从表中填充了n行数据。 现在我需要根据数据制作图表。

我们必须添加两个Legent条目(类别) – D5:D15和E5:E15和一个水平(类别)轴标签-F5:F15

我尝试了以下来获得这个。 当执行时,我得到了错误信息

自动化对象不支持方法'setsourcedata'。

我尝试了SeriesCollection.item.value方法,然后系统给出了相同的消息。

 procedure TForm1.Make_a_Chart; var Sheets,Ch1 : Variant; begin ch1 := XLApp.ActiveWorkBook.Sheets[1].ChartObjects.Add ( 500,100,400,200 ); // creates a new chart in the specified Sheets := XLApp.Sheets; ch1.Chart.ChartWizard ( Sheets.Item['Delphi Data'].Range['D5:D15'], // 1 Source xlBarStacked, // 2 The chart type. 8, // 3 Format - The option number for the built-in autoformats. Can be a number from 1 through 10, depending on the gallery type. 2, // 4 PlotBy - Specifies whether the data for each series is in rows or columns. Can be one of the following XlRowCol constants. 1, // 5 CategoryLabels - An integer specifying the number of rows or columns within the source range that contain category labels. 1, // 6 SeriesLabels - An integer specifying the number of rows or columns within the source range that contain series labels. False, // 7 HasLegend - 'true' to include a legend. 'My Report', // 8 Title - The Chart control title text. 'Y Axis', // 9 CategoryTitle - The category axis title text. 'X Axis', // 10 ValueTitle - The value axis title text 2 // 11 ExtraTitle - The series axis title for 3-D charts or the second value axis title for 2-D charts. ); ch1.setsourcedata(Sheets.Item['Delphi Data'].Range['E5:E15'],xlColumns,2); ch1.Chart.ChartType := xlBarStacked; // ch1.SeriesCollection.Item[1].Values := Sheets.Item['Delphi Data'].Range['E5:E15']; end; 

您在ChartObject对象上调用SetSourceData,而不是Chart对象 。 ChartObject对象是Chart对象的容器。 Chart对象具有方法SetSourceData ,所以使用这个:

 ch1.Chart.SetSourceData(Sheets.Item['Delphi Data'].Range['E5:E15'],xlColumns,2); 

编辑:你的代码样本很混乱。 请在提问时尝试整理您的代码。 我看到很多未使用的variables,并使用未在var部分中声明的variablesch1