COM - comunicacao com excel |
Top Previous Next |
// testado em 09/06/2004 - WinXP, Delphi 7 - Excel XP - OK
uses COMObj;
procedure TForm1.Button1Click(Sender: TObject); var Excel: Variant; begin try Excel:= CreateOleObject('Excel.Application'); Excel.Workbooks.Add(1); except ShowMessage('Versão do Excel incompatível'); end;
try Excel.Cells[1, 1] := 'Finalizadora'; Excel.Cells[1, 2] := 'Valor';
Excel.Cells[3, 1] := 'DINHEIRO'; Excel.Cells[4, 1] := 'CHEQUE'; Excel.Cells[5, 1] := 'CARTÃO';
Excel.Cells[3, 2] := 100.34; Excel.Cells[4, 2] := 1234.5; Excel.Cells[5, 2] := 1.99;
Excel.Columns.AutoFit; Excel.Visible := True; except ShowMessage('Aconteceu um erro durante a transferencia para o Excel'); end; end;
--------------------------------- exemplo 2------------------------
Procedure TForm1.GerarExcel(Consulta:TQuery); var coluna, linha: integer; excel: variant; valor: string; begin try excel:=CreateOleObject('Excel.Application'); excel.Workbooks.add(1); except Application.MessageBox ('Versão do Ms-Excel'+ 'Incompatível','Erro',MB_OK+MB_ICONEXCLAMATION); end; Consulta.First; try for linha:=0 to Consulta.RecordCount-1 do begin for coluna:=1 to Consulta.FieldCount do begin valor:= Consulta.Fields[coluna-1].AsString; excel.cells [linha+2,coluna]:=valor; end; Consulta.Next; end; for coluna:=1 to Consulta.FieldCount do begin valor:= Consulta.Fields[coluna-1].DisplayLabel; excel.cells[1,coluna]:=valor; end; excel.columns.AutoFit; excel.visible:=true; except Application.MessageBox ('Aconteceu um erro desconhecido durante a conversão'+ 'da tabela para o Ms-Excel','Erro',MB_OK+MB_ICONEXCLAMATION); end; end;
------------------------------exemplo 2---------------------------- Como enviar dados do Delphi para o Excel procedure TFormCTEmbarque.SpeedButton1Click(Sender: TObject); var Excel : Variant; Linha:Integer; begin Excel := CreateOleObject('Excel.Application'); Excel.Visible :=True; {Excel.Workbooks.Add;} Excel.WorkBooks.Open('\\SERVIDOR\Cotacao\Gerar.xls'); Excel.WorkBooks[1].Sheets[1].Cells[2,7]:=Now; Excel.WorkBooks[1].Sheets[1].Cells[3,2]:=DMCotacao.TBLiberaRemetente.Value; Excel.WorkBooks[1].Sheets[1].Cells[3,5]:=DMCotacao.TBLiberaColeta.Value + '-' +DMCotacao.TBLiberaUF_Coleta.Value; Excel.WorkBooks[1].Sheets[1].Cells[4,2]:=DMCotacao.TBLiberaDestinatario.Value; Excel.WorkBooks[1].Sheets[1].Cells[4,5]:=DMCotacao.TBLiberaDestino.Value + '-' +DMCotacao.TBLiberaUF_Destino.Value; Excel.WorkBooks[1].Sheets[1].Cells[5,2]:=DMCotacao.TBLiberaQuantidade.AsString; Excel.WorkBooks[1].Sheets[1].Cells[5,5]:=DMCotacao.TBLiberaFreteEmpresa.AsString; Excel.WorkBooks[1].Sheets[1].Cells[5,7]:=DMCotacao.TBLiberaContrato.AsString; Excel.WorkBooks[1].Sheets[1].Cells[6,2]:=FormCTEmbarque.Edit2.Text; Excel.WorkBooks[1].Sheets[1].Cells[6,5]:=FormCTEmbarque.Edit3.Text; Excel.WorkBooks[1].Sheets[1].Cells[6,7]:=FormCTEmbarque.Edit4.Text; Excel.WorkBooks[1].Sheets[1].Cells[7,2]:=DMCotacao.TBLiberaObservacao.Value; DmCotacao.QCTEmbarque.Open; Linha:=10; While not DMCotacao.QCTEmbarque.Eof do Begin Excel.WorkBooks[1].Sheets[1].Cells[Linha,2]:=DMCotacao.QCTEmbarqueCTRC.Value; Excel.WorkBooks[1].Sheets[1].Cells[Linha,3]:=DMCotacao.QCTEmbarqueNotaFiscal.Value; Excel.WorkBooks[1].Sheets[1].Cells[Linha,4]:=DMCotacao.QCTEmbarquePeso.Value; Excel.WorkBooks[1].Sheets[1].Cells[Linha,5]:=DMCotacao.QCTEmbarquePlaca.Value; Excel.WorkBooks[1].Sheets[1].Cells[Linha,6]:=DMCotacao.QCTEmbarqueData.Value; DmCotacao.QCTEmbarque.Next; Linha:=Linha+1; end; Excel.WorkBooks[1].SaveAs('\\SERVIDOR\Cotacao\Controle.xls'); DMCotacao.TBCotacao.Refresh; end; |