Use a variant array to write data to Excel in one go (Views: 28)
Problem/Question/Abstract: 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: { ... } var ArrV: OleVariant; i: integer; StartCell: OleVariant; { ... } {Create a variant array with 5 rows (0 to 4) and 125 columns (0 to 124)} ArrV := VarArrayCreate([0, 4, 0, 124], varVariant); { ... } for i := 0 to 124 do begin ArrV[0, i] := i; ArrV[1, i] := '2'; ArrV[2, i] := 3; ArrV[3, i] := 4; ArrV[4, i] := 5; end; StartCell := WS.Cells.Item[8, 3]; WS.Range[StartCell, StartCell.Offset[4, 125]].Value2 := ArrV; {Create a variant array with 4 rows (0 to 3) and 2 columns (0 to 1)} ArrV := VarArrayCreate([0, 3, 0, 1], varOleStr); ArrV[0, 0] := 'First Column'; ArrV[0, 1] := 'Second Column'; ArrV[1, 0] := 'Second Row'; ArrV[2, 0] := 'Third Row'; ArrV[3, 0] := 'Fourth row'; ArrV[3, 1] := 'Fourth row, second column'; {Write the array to a worksheet} WS.Range['A1', 'B4'].Value := ArrV; { ... } |