Below are examples to check for XLS file structure: fields names and its data.
Executing the example requires an XLS file named Data in root of the disk C. The document must contain sheet named ByRow.
Add links to the Dal, Dt, Metabase, Ui system assemblies.
Sub UserProc;
Var
MB: IMetabase;
Dtxlsprov: IDtExcelProvider;
Fields: IDtFieldDefinitions;
Field: IDtFieldDefinition;
namField, Val: string;
v: Array;
i: Integer;
s: double;
rez: boolean;
Begin
// Get repository
MB := MetabaseClass.Active;
// Create object for data import from XLS file
Try
dtxlsprov := New DtExcelProvider.Create;
// Specify data source
dtXlsProv.File := "C:\Data.xls";
// Request from data source from the ByRow sheet
dtXlsProv.Query := "select * from [" + "ByRaw" + "$]";
// Enable mode of import from columns containing various data types
dtXlsProv.ImexMode := 1 As DtExcelImexMode;
dtXlsProv.CheckFieldName := True;
// Open data source
dtXlsProv.Open;
// Display number of fields of opened source to the console
Fields := dtXlsProv.Fields;
Debug.WriteLine("Document is opened = " + dtXlsProv.Active.ToString);
Debug.WriteLine("Fields number = " + Fields.Count.ToString);
// Display source fildes names to the console
namField := "Fields names - ";
For Each Field In Fields Do
namField := namField + Field.Name + "|" + Field.DataType.ToString + "; "
End For;
Debug.WriteLine(namField);
// Display source fields data to the console
Debug.WriteLine("Data:");
Except On e: exception Do
WinApplication.InformationBox("Error. Data are not read");
End Try;
While Not dtXlsProv.Eof Do
Val := "";
dtXlsProv.Fetch(v);
For i := 0 To v.Length - 1 Do
val := val + v[i] + " ; ";
End For;
Debug.WriteLine(val);
End While;
// Close data source
dtXlsProv.Close;
End Sub UserProc;
After executing the example the console displays information about data in the file. Displayed information:
Number of fields.
Name of fields.
Data in fields of specified file.
If data of specified file are set incorrectly and are not read, the error message is displayed.
See also: