为Excel动态添加Chart数据

    技术2022-05-19  21

    往Excel的Chart添加数据,资料大都使用range方法,经查MSDN,发现也可以使用数组,实践了一下: implementationuses ComObj;{$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);var V,ASheet1,AChart,Series: Variant; Path: string; I:Integer; Arr_A,Arr_B: OleVariant; // 不能使用Variant!begin V := CreateOleObject('Excel.Application'); Path := ExtractFilePath(ParamStr(0))+'Tester.xls'; V.WorkBooks.Open(Path);  //DI1.Text := format('DI_1:%d',[Random(110)]); //DI2.Text := format('DI_2:%d',[Random(20)]); //DI3.Text := format('DI_3:%d',[Random(30)]);// DDE交换数据 Arr_A := varArrayCreate([1,30],varVariant); for i := 1 to 30 do Arr_A[i] := Random(50); Arr_B := varArrayCreate([1,30],varVariant); for i := 1 to 30 do Arr_B[i] := 50+Random(50); ASheet1:=V.Workbooks[1].WorkSheets[1]; AChart := ASheet1.ChartObjects.Add(100,100,500,200); Series:=AChart.Chart.SeriesCollection.NewSeries; Series.Values := Arr_A; Series.Name := 'Series A'; Series:=AChart.Chart.SeriesCollection.NewSeries; Series.Values := Arr_B; Series.Name := 'Series B';  AChart.Chart.ChartType := 4; AChart.Chart.HasTitle := TRUE; AChart.Chart.ChartTitle.Characters.Text := 'Test Example!'; V.Visible := TRUE;end;

    最新回复(0)