Data: Array;
The Data property determines two-dimensional array of data to be exported within one transaction.
Executing the example requires the Data.xls file in the disk C root directory.
Add links to the Dt, Dal, Db system assemblies to execute the example.
Sub Main;
Var
ExcelConsumer: IDtExcelConsumer;
Fields: IDtFieldDefinitions;
Field: IDtFieldDefinition;
v, v1: Array;
Pack: IDtBulkPacket;
i: integer;
Begin
//Array passed in the first packet
v := New Variant[3, 4];
v[0, 0] := "Buckwheat"; v[1, 0] := 10; v[2, 0] := 313.12;
v[0, 1] := "Milk"; v[1, 1] := 20; v[2, 1] := 301.53;
v[0, 2] := "Sugar"; v[1, 2] := 30; v[2, 2] := 254.13;
v[0, 3] := "Bread"; v[1, 3] := 40; v[2, 3] := 404.11;
//Array passed in the second packet
v1 := New Variant[3, 4];
v1[0, 0] := "Kefir"; v1[1, 0] := 10; v1[2, 0] := 113.12;
v1[0, 1] := "Juice"; v1[1, 1] := 20; v1[2, 1] := 101.53;
v1[0, 2] := "Tea"; v1[1, 2] := 30.45; v1[2, 2] := 154.13;
v1[0, 3] := "Coffee"; v1[1, 3] := 40; v1[2, 3] := "A";
ExcelConsumer := New DtExcelConsumer.Create;
ExcelConsumer.File := "d:\Data.xls";
ExcelConsumer.HasHeader := True;
ExcelConsumer.DriverVersion := "Excel 8.0";
ExcelConsumer.Table := "Sheet1";
Fields := ExcelConsumer.Fields;
Field := Fields.Add;
Field.DataType := DbDataType.String;
Field.Name := "Name";
Field := Fields.Add;
Field.DataType := DbDataType.Integer;
Field.Name := "Identifier";
Field := Fields.Add;
Field.DataType := DbDataType.Float;
Field.Name := "Value";
ExcelConsumer.Open;
Pack := New DtBulkPacket.Create;
Pack.FailPolicy := DatasetFailPolicy.Rollback;
//The first packet
Pack.Data := v;
Pack.StopLimit := 8;
ExcelConsumer.PutBulk(Pack);
If Not Pack.Successful Then
Debug.WriteLine("Error on packet export");
Debug.WriteLine("Records not exported: " + Pack.FailedRows.ToString);
For i := v.GetLowerBound(1) To v.GetUpperBound(1) Do
If Pack.Failed(i) Then
Debug.WriteLine(Pack.ErrorMessage(i));
End If;
End For;
Else
Debug.WriteLine("Packet is exported successfully");
End If;
//The second packet
Pack.Data := v1;
Pack.StopLimit := 8;
ExcelConsumer.PutBulk(Pack);
If Not Pack.Successful Then
Debug.WriteLine("Error on packet export");
Debug.WriteLine("Records not exported: " + Pack.FailedRows.ToString);
For i := v1.GetLowerBound(1) To v1.GetUpperBound(1) Do
If Pack.Failed(i) Then
Debug.WriteLine(Pack.ErrorMessage(i));
End If;
End For;
Else
Debug.WriteLine("Packet is exported successfully");
End If;
ExcelConsumer.Close;
End Sub Main;
After executing the example a new data consumer, that exports data in Excel file, is created. Values of two arrays are in turns written in file. Information about export results is displayed in the development environment console. If there are any records that are not exported by any reason, the error message is displayed in the console window.
See also: