Excel com Delphi - Excel.exe fica aberto no gerenciador de tarefas
faço o seguinte para carregar o excel em um grid:
function TfrmImportacaoExcel.fXlsToGrid(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 grid
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;
porém o excel.exe fica aberto no gerenciador de tarefas. algm ja passou por isso?
abs;
function TfrmImportacaoExcel.fXlsToGrid(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 grid
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;
porém o excel.exe fica aberto no gerenciador de tarefas. algm ja passou por isso?
abs;
Gxf
Curtidas 0
Respostas
Emerson Nascimento
18/11/2021
tente assim:
function TfrmImportacaoExcel.fXlsToGrid(AGrid: TStringGrid; AXLSFile: string): Boolean;
const
xlCellTypeLastCell = $0000000B;
var
XLApp, Sheet: OLEVariant;
RangeMatrix: Variant;
x, y, k, r: Integer;
begin
Result := False;
try
//Cria Excel- OLE Object
XLApp := CreateOleObject('Excel.Application');
//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 grid
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;
Result := True;
except
end;
try
XLApp.Workbooks.Close; // Fecha o arquivo
except
end;
try
XLApp.Quit; // Fecha o Excel
except
end;
XLAPP := Unassigned;
Sheet := Unassigned;
end;GOSTEI 0
Gxf
18/11/2021
mt obrigado emerson. mas ainda continuou aberto no gerenciador de tarefas :/
GOSTEI 0
Emerson Nascimento
18/11/2021
Gxf, estou usando Delphi 11 Enterprise com Office 365 no Windows 11 e funciona certinho.
Desculpe por eu não poder ajudar mais.
GOSTEI 0
Gxf
18/11/2021
criando um projeto do zero, da certo.
porém em um projeto com varios formularios, ele ainda fica no gerenciador.
porém em um projeto com varios formularios, ele ainda fica no gerenciador.
GOSTEI 0