|
|
|
| Read an Excel Spreadsheet in Delphi using OLE | |
|
Add a StringGrid to your project. In this example, I named it Sg.
procedure LoadGrid;
var
R: _Application;
Sheet: _WorkSheet;
W: WorkBook;
begin
R := CreateCOMObject(Class_Application) as _Application;
W := R.WorkBooks.Open(OpenDialog1.FileName, '', False, '', '', False, True, 0, 0, True, False, '', False, 0);
Sheet := W.Worksheets[1] as _WorkSheet;
for J := 1 to 4 do
for I := 1 to Sheet.Cells.Count do
begin
Forms.Application.ProcessMessages;
A := Sheet.Cells.Item[I, J];
Sg.Cells[J - 1, I] := A;
if I > Z then Break;
end;
R.WorkBooks.Close(0);
end;
|
|
|
Notes: Excel can cause all sorts of confusion during this process. For example, a user may have the same file open as read-only. That user may be you if you forgot a Workbooks.Close(). Z is the size of your StringGrid |