Excel com Delphi - Excel.exe fica aberto no gerenciador de tarefas
18/11/2021
0
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
Posts
19/11/2021
Emerson Nascimento
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;
19/11/2021
Gxf
20/11/2021
Emerson Nascimento
Gxf, estou usando Delphi 11 Enterprise com Office 365 no Windows 11 e funciona certinho.
Desculpe por eu não poder ajudar mais.
22/11/2021
Gxf
porém em um projeto com varios formularios, ele ainda fica no gerenciador.
Clique aqui para fazer login e interagir na Comunidade :)