Kann man das Ganze nicht gefühlt sauberer eindampfen zu:
Delphi-Quellcode:
function Xls_To_StringGrid(AGrid: TStringGrid; AXLSFile: string): Boolean;
var
XLApp, workbook, Sheet: OLEVariant;
row, col: Integer;
begin
Result := False;
XLApp := CreateOleObject('Excel.Application');
try
XLApp.Visible := False;
Workbook:=XLApp.Workbooks.Open(AXLSFile);
Sheet := Workbook.WorkSheets[1];
// Set Stringgrid's row &col dimensions.
AGrid.RowCount := Sheet.UsedRange.Rows.Count;
AGrid.ColCount := Sheet.UsedRange.Columns.Count;
// Define the loop for filling in the TStringGrid
for row = 1 to Sheet.UsedRange.Rows.Count do
for col = 1 to Sheet.UsedRange.Columns.Count do
AGrid.Cells[(row - 1), (col - 1)] := Sheet.Cells[row,col].value;
finally
// Quit Excel
if not VarIsEmpty(XLApp) then
begin
XLApp.Quit;
XLAPP := Unassigned;
Sheet := Unassigned;
Result := True;
end;
end;
end;