Delphi 6 - 从delphi应用程序创建Excel图表 - 同一页面上的数据和图表

时间:2012-04-20 11:11:28

标签: excel delphi

我需要在delphi程序的excel页面中创建一个excel图表。

我到达该行时遇到“未找到会员”错误。

ch1.Chart.SeriesCollection.Item[0].Values := Sheets.Item['Delphi Data'].Range['E5:E15'];

你能帮我解决一下吗

下面给出了使用的代码。

procedure TForm1.ChartData;
var
     ARange,Sheets,ch1 : Variant; 
     SSeries : Series; 
     num : integer; 
     ChartAxis : Axis; 
     lcid :  Cardinal; 
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  
                         2,            //  4  PlotBy  
                         8,            //  5  CategoryLabels  
                         3,            //  6  SeriesLabels  
                         True,        //  7  HasLegend - 'true' to include a legend. 
                         'Sijos Report',  // 8  Title - The Chart control title text. 
                         'Y Legend',      // 9  CategoryTitle - The category axis title text. 
                         'X Legend',      // 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.Chart.SetSourceData(Sheets.Item['Delphi Data'].Range['D5:D15'],xlColumns); 

ch1.Chart.SeriesCollection.Item[0].Values := Sheets.Item['Delphi Data'].Range['E5:E15']; 
ch1.Chart.SeriesCollection.Item[0].XValues := Sheets.Item['Delphi Data'].Range['F5:F15'];  
End;

1 个答案:

答案 0 :(得分:4)

我认为您需要ch1.Chart.SeriesCollection(1)而不是ch1.Chart.SeriesCollection.Item[0],因为Excel使用基于1的索引。

我也无法获取您的代码,使用后期绑定的COM在访问系列对象时完全可以工作。但是,如果你切换到使用早期绑定的COM,那么它很好。例如,您需要将Excel2000添加到uses子句中。

var
  S: Series;
....
S := IUnknown(ch1.Chart.SeriesCollection(1)) as Series;
S.Values := Sheets.Item['Delphi Data'].Range['E5:E15']; 
S.XValues := Sheets.Item['Delphi Data'].Range['F5:F15'];  

如果我是你,我会把整个代码切换到早期。

我认为您的代码基于此Delphi 3 example from Charlie Calvert。我无法在Delphi 6上工作。也许德尔福改变了。也许Excel改变了。无论如何,使它适用于我的事情是切换到早期绑定的COM。