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.
The requirements and result of the Fore.NET example execution match with those in the Fore example.
Imports Prognoz.Platform.Interop.Dt;
Imports Prognoz.Platform.Interop.Ui;
Imports Prognoz.Platform.Forms.Net;
…
Public Shared Sub Main(Params: StartParams);
Var
MB: IMetabase;
Dtxlsprov: IDtExcelProvider = New DtExcelProviderClass();
Fields: IDtFieldDefinitions;
Field: IDtFieldDefinition;
namField, Val: string;
v: array;
i: integer;
s: double;
rez: boolean;
WinAppCls: WinApplicationClass = New WinApplicationClassClass();
Begin
// Get repository
MB := Params.Metabase;
// Create object for data import from XLS file
Try
// Specify data source
dtXlsProv.File := "C:\Data.xls";
// Rquest 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 fields number of opened source to the console
Fields := dtXlsProv.Fields;
System.Diagnostics.Debug.WriteLine("Document is opened = " + dtXlsProv.Active.ToString());
System.Diagnostics.Debug.WriteLine("Fields number = " + Fields.Count.ToString());
// Display source fields names to the console
namField := "Fields names - ";
For Each Field In Fields Do
namField := namField + Field.Name + "|" + Field.DataType.ToString() + "; "
End For;
System.Diagnostics.Debug.WriteLine(namField);
// Display source fields data to the console
System.Diagnostics.Debug.WriteLine("Data:");
Except On e: System.Exception Do
WinAppCls.InformationBox("Error. Data are not read", New IWin32WindowForeAdapter(Null));
End Try;
While Not dtXlsProv.Eof Do
Val := "";
dtXlsProv.Fetch(Var v);
For i := 0 To v.Length - 1 Do
val := val + v[i] + " ; ";
End For;
System.Diagnostics.Debug.WriteLine(val);
End While;
// Close data source
dtXlsProv.Close();
End Sub;
See also: