Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
ExcelApplication1 := TExcelApplication.Create(Self);
ExcelWorkbook1 := TExcelWorkbook.Create(ExcelApplication1);
ExcelWorksheet1 := TExcelWorksheet.Create(ExcelWorkbook1);
Application.ProcessMessages;
ExcelApplication1.Connect;
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
ExcelApplication1.Destroy;
ExcelApplication1.Free;
ExcelApplication1 := nil;
procedure CreateDatasetFromExcelFile(CellFrom,
CellTo: string; var Dataset: TDataset);
var
VarArray : Variant;
iCol,
iRow : Integer;
T : TStringField;
sTmpName : string;
iCounter : integer;
iField : Integer;
begin
VarArray := VarArrayOf(['']);
{
Check the function FillVarArrayFromExcelRange for additional information.
It is in this unit.
}
FillVarArrayFromExcelRange(CellFrom, CellTo, VarArray);
{
Create fields to dataset.
}
if Dataset.Active then
begin
Dataset.Close;
end;
iField := Ord('A');
Dataset.FieldDefs.BeginUpdate;
for iCol := VarArrayLowBound(VarArray, 2) to VarArrayHighBound(VarArray, 2) do
begin
T := TStringField.Create(Dataset);
T.FieldName := Chr(iField);
Inc(iField);
sTmpName := Dataset.Name + T.FieldName;
{
Remove spaces (other unwanted (special) chars can be parsed here also )
from column headers.
}
while Pos(' ', sTmpName) > 0 do
sTmpName[Pos(' ', sTmpName)] := '_';
while Pos('.', sTmpName) > 0 do
sTmpName[Pos('.', sTmpName)] := '_';
while Pos(',', sTmpName) > 0 do
sTmpName[Pos(',', sTmpName)] := '_';
T.Name := sTmpName;
T.Index := Dataset.FieldCount;
T.DataSet := Dataset;
Dataset.FieldDefs.Update;
end;
Dataset.FieldDefs.EndUpdate;
Dataset.Open;
{
Add data to dataset.
VarArrayLowBound(VarArray, 1) is the header row so start from the next one. Also there might not be any header row so the can be edited.
}
for iRow := VarArrayLowBound(VarArray, 1) + 1 to VarArrayHighBound(VarArray, 1) do
begin
Dataset.Append;
iCounter := 1;
for iCol := VarArrayLowBound(VarArray, 2) to VarArrayHighBound(VarArray, 2) do
begin
Dataset.Fields[iCounter].AsString := VarToStr(VarArray[iRow, iCol]);
Inc(iCounter);
end;
Dataset.Post;
end;
Dataset.First;
end;
procedure FillVarArrayFromExcelRange(CellFrom, CellTo: string;
var VarArray: variant);
begin
{
Add the workbook. Without it you can do nothing.
}
ExcelApplication1.Workbooks.Add(ExcelFile, 0);
{
Connect to the workbook.
}
ExcelWorkbook1.ConnectTo(ExcelApplication1.ActiveWorkbook);
ExcelWorksheet1.ConnectTo(ExcelApplication1.ActiveSheet as _WorkSheet);
{
Get the values to the Variant. That's about it.
}
VarArray := ExcelWorksheet1.Range[CellFrom, CellTo].Value;
{
Disconnect from worksheet and workbook and close workbook.
}
ExcelWorksheet1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Workbooks.Close(0);
end;
Application.CreateForm(TOLEFunc, OLEFunc);
OLEFunc.ExcelFile := beFile.Text;
OLEFunc.CreateDatasetFromExcelFile(edCellFrom.Text, edCellTo.Text, TDataSet(dxMDLocation));
OLEFunc.Destroy;
Application.ProcessMessages;
var
OleApp : Variant; //requires something to the uses...
OleApp := CreateOleObject('Excel.Application');
OleApp.WorkBooks.Open('FileName'
//This goes to the FillVarArrayFromExcelRange procedure
VarArray := OleApp.Workbooks('WorkBookName').Sheets('SheetName').Range['A1', 'B3'].Value;
//And finally quit the application
OleApp.Quit;