Author: Jonas Bilinkevicius
How to use a variant array to write data to Excel in one go
Answer:
Here are a couple of examples of using a variant array to write data to Excel in
one go:
1 { ... }
2 var
3 ArrV: OleVariant;
4 i: integer;
5 StartCell: OleVariant;
6 { ... }
7
8 {Create a variant array with 5 rows (0 to 4) and 125 columns (0 to 124)}
9 ArrV := VarArrayCreate([0, 4, 0, 124], varVariant);
10 { ... }
11 for i := 0 to 124 do
12 begin
13 ArrV[0, i] := i;
14 ArrV[1, i] := '2';
15 ArrV[2, i] := 3;
16 ArrV[3, i] := 4;
17 ArrV[4, i] := 5;
18 end;
19 StartCell := WS.Cells.Item[8, 3];
20 WS.Range[StartCell, StartCell.Offset[4, 125]].Value2 := ArrV;
21
22 {Create a variant array with 4 rows (0 to 3) and 2 columns (0 to 1)}
23 ArrV := VarArrayCreate([0, 3, 0, 1], varOleStr);
24 ArrV[0, 0] := 'First Column';
25 ArrV[0, 1] := 'Second Column';
26 ArrV[1, 0] := 'Second Row';
27 ArrV[2, 0] := 'Third Row';
28 ArrV[3, 0] := 'Fourth row';
29 ArrV[3, 1] := 'Fourth row, second column';
30
31 {Write the array to a worksheet}
32 WS.Range['A1', 'B4'].Value := ArrV;
33 { ... }
|