Sei sulla pagina 1di 1

[Resolvido] importar dados do Excel para o delphi - iMasters Fóruns http://forum.imasters.uol.com.br/index.php?/topic/259016-resolvido%...

function XlsToStringGrid(AGrid: TStringGrid; AXLSFile: string): Boolean;


const
xlCellTypeLastCell = $0000000B;
var
XLApp, Sheet: OLEVariant;
RangeMatrix: Variant;
x, y, k, r: Integer;
begin
Result:=False;
//Cria Excel- OLE Object
XLApp:=CreateOleObject('Excel.Application');
try
//Esconde Excel
XLApp.Visible:=False;
//Abre o Workbook
XLApp.Workbooks.Open(AXLSFile);
Sheet:=XLApp.Workbooks[ExtractFileName(AXLSFile)].WorkSheets[1];
Sheet.Cells.SpecialCells(xlCellTypeLastCell, EmptyParam).Activate;
//Pegar o número da última linha
x:=XLApp.ActiveCell.Row;
//Pegar o número da última coluna
y:=XLApp.ActiveCell.Column;
//Seta Stringgrid linha e coluna
AGrid.RowCount:=x;
AGrid.ColCount:=y;
//Associaca a variant WorkSheet com a variant do Delphi
RangeMatrix:=XLApp.Range['A1', XLApp.Cells.Item[X, Y]].Value;
//Cria o loop para listar os registros no TStringGrid
k:=1;
repeat
for r:=1 to y do
AGrid.Cells[(r - 1),(k - 1)]:=RangeMatrix[K, R];
Inc(k,1);
until k > x;
RangeMatrix:=Unassigned;
finally
//Fecha o Excel
if not VarIsEmpty(XLApp) then
begin
XLApp.Quit;
XLAPP:=Unassigned;
Sheet:=Unassigned;
Result:=True;
end;
end;
;end

:Vai retornar se conseguiu ou não carregar, precisa apenas colocar um StringGrid na tela, e chamar a função, algo como

;('XlsToStringGrid(StringGrid1,'c:\arquivo_excel.xls

1 de 1 06/08/2009 21:58

Potrebbero piacerti anche