Provider: IDtProvider;
The Provider property determines table data provider containing areas.
The rest of properties of the interface are used to set up parameters of areas containing in data provider.
Executing the example requires that the file system contains the D:\Data.xls file including data split into areas. Data in the file is located on the Sheet 1 sheet. The file system must also contain the D:\DataTransform.txt file where data from C:\Data.xls will be exported.
Add a link to the Dt system assembly.
Sub UserProc;
Var
XlsProvider: IDtExcelProvider;
UnpivotProvider: IDtUnpivotProvider;
FixCells: Array Of Integer;
TextConsumer: IDtTextConsumer;
ProviderFields, ConsumerFields: IDtFieldDefinitions;
i, Row, Col: integer;
Field, NewField: IDtFieldDefinition;
Begin
// Create an object to import data from Microsoft Excel files
XlsProvider := New DtExcelProvider.Create;
// Specify file with data
XlsProvider.File := "D:\Data.xls";
// Specify Microsoft Excel driver version
XlsProvider.DriverVersion := "Excel 8.0";
// Set query for data sampling
XlsProvider.Query := "Select * From [Sheet1$]";
// Create an object for working with table data source divided into areas
UnpivotProvider := New DtUnpivotProvider.Create;
// Set Microsoft Excel file as a table data source
UnpivotProvider.Provider := XlsProvider;
// Set parameters of A4 area
UnpivotProvider.DataBottom := 10;
UnpivotProvider.DataRight := 9;
UnpivotProvider.DataTop := 7;
UnpivotProvider.DataWidth := 3;
// Set parameters of A2 area
UnpivotProvider.HeadBottom := 4;
UnpivotProvider.HeadColumn := 1;
UnpivotProvider.HeadTop := 1;
// Set parameters of A3 area
UnpivotProvider.LeftBegin := 1;
UnpivotProvider.LeftEnd := 3;
// Set fixed cells
FixCells := New Integer[2, 1];
FixCells[0, 0] := 1;
FixCells[1, 0] := 1;
UnpivotProvider.FixedCells := FixCells;
If UnpivotProvider.IsCorrect Then
// Get data provider fields
UnpivotProvider.FieldsFromFile;
// Open data provider
UnpivotProvider.Open;
// Create an object to export data to text file
TextConsumer := New DtTextConsumer.Create;
// Specify output file
TextConsumer.File := "D:\DataTransform.txt";
// Set export options
TextConsumer.WriteHeader := True;
// Set exported fields
ProviderFields := UnpivotProvider.Fields;
ConsumerFields := TextConsumer.Fields;
For i := 0 To ProviderFields.Count - 1 Do
Field := ProviderFields.Item(i);
NewField := ConsumerFields.Add;
NewField.DataType := Field.DataType;
NewField.Name := Field.Name;
NewField.Precision := Field.Precision;
NewField.Size := Field.Size;
End For;
// Open and clear export file
TextConsumer.Open;
TextConsumer.Clear;
// Execute export from table data provider divided into areas
TextConsumer.PutProvider(UnpivotProvider);
// Get correspondence between cell in output file and cell in source file
UnpivotProvider.ToSource(1, 0, Row, Col);
Debug.WriteLine("In output file the cell in the row with the 1 index and " +
"the column with the 0 index corresponds with the cell in the source file in the row with the "
+ Row.ToString + " index and in the column with the " + Col.ToString) index;
// Get correspondence between cell in source file and cell in output file
UnpivotProvider.FromSource(7, 1, Row, Col);
Debug.WriteLine("In source file the cell in the row with the 7 index and " +
"the column with the 1 index corresponds with the cell in the row with the "
+ Row.ToString + " index and the column with the " + Col.ToString) index;
// Close data provider and exported file
UnpivotProvider.Close;
TextConsumer.Close;
End If;
End Sub UserProc;
After executing the example the data from the specified file will be transformed and exported to the text file.
See also: