Advertisement
leandropintor

DataSet To Excel - Delphi

Aug 9th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.73 KB | None | 0 0
  1. Procedure TExport.ExportExcel2(CodEmp, NumOrp: Integer; CodOri: String; Relatorio: SmallInt;
  2.   DataSet: TFdquery; Arq: String);
  3. Var
  4.   ExcApp : OleVariant;
  5.   I, J, L: Integer;
  6. Begin
  7.   ExcApp         := CreateOleObject('Excel.Application');
  8.   ExcApp.Visible := False;
  9.   ExcApp.WorkBooks.Add;
  10.   DataSet.Close;
  11.   DataSet.Params.ParamByName('CODEMP').AsInteger     := CodEmp;
  12.   DataSet.Params.ParamByName('CODORI').AsString      := CodOri;
  13.   DataSet.Params.ParamByName('NUMORP').AsInteger     := NumOrp;
  14.   DataSet.Params.ParamByName('RELATORIO').AsSmallInt := Relatorio;
  15.   DataSet.SQL.Text;
  16.   DataSet.Open;
  17.   DataSet.First;
  18.   FrmMenu.PnProgresso.Visible := True;
  19.   Application.ProcessMessages;
  20.   If DataSet.RecordCount > 0 Then
  21.     Begin
  22.       L := 1;
  23.       DataSet.First;
  24.       FrmMenu.ProgressBar1.Max := DataSet.RecordCount;
  25.       For J                    := 0 To DataSet.Fields.Count - 1 Do
  26.         Begin
  27.           ExcApp.WorkBooks[1].Sheets[1].Cells[L, J + 1] := DataSet.Fields[J].DisplayName;
  28.         End;
  29.       L := 2;
  30.       While Not DataSet.EOF Do
  31.         Begin
  32.           Application.ProcessMessages;
  33.           FrmMenu.ProgressBar1.Position := FrmMenu.ProgressBar1.Position + 1;
  34.           FrmMenu.LblChapa.Caption      := 'Processando chapa... ' +
  35.             IntToStr(FrmMenu.ProgressBar1.Position);
  36.           Application.ProcessMessages;
  37.           For I := 0 To DataSet.Fields.Count - 1 Do
  38.             Begin
  39.               If (I = 5) Or (I = 6) Or (I = 7) Or (I = 10) Or (I = 11) Or (I = 12) Or (I = 15) Then
  40.                 Begin
  41.                   // Esp Milimetro Esquerdo
  42.                   If I = 5 Then
  43.                     Begin
  44.                       ExcApp.WorkBooks[1].Sheets[1].Cells[L, I + 1] :=
  45.                         FormatFloat('00.00', DataSet.Fields[I].AsFloat);
  46.                     End;
  47.                   // Esp Milimetro Direito
  48.                   If I = 6 Then
  49.                     Begin
  50.                       ExcApp.WorkBooks[1].Sheets[1].Cells[L, I + 1] :=
  51.                         FormatFloat('00.00', DataSet.Fields[I].AsFloat);
  52.                     End;
  53.                   If I = 7 Then
  54.                     ExcApp.WorkBooks[1].Sheets[1].Cells[L, I + 1] := 'Polida';
  55.                   // Comp. Bruto
  56.                   If I = 10 Then
  57.                     Begin
  58.                       ExcApp.WorkBooks[1].Sheets[1].Cells[L, I + 1] :=
  59.                         DataSet.Fields[I].DisplayText;
  60.                       ExcApp.WorkBooks[1].Sheets[1].Cells[L, I + 1].NumberFormat := '##0,00_);';
  61.                     End;
  62.                   // Alt. Bruta
  63.                   If I = 11 Then
  64.                     Begin
  65.                       ExcApp.WorkBooks[1].Sheets[1].Cells[L, I + 1] :=
  66.                         DataSet.Fields[I].DisplayText;
  67.                       ExcApp.WorkBooks[1].Sheets[1].Cells[L, I + 1].NumberFormat := '##0,00_);';
  68.                     End;
  69.                   // Area bruta
  70.                   If I = 12 Then
  71.                     Begin
  72.                       ExcApp.WorkBooks[1].Sheets[1].Cells[L, I + 1] := '=K' + L.ToString + '*L' +
  73.                         L.ToString;
  74.                       ExcApp.WorkBooks[1].Sheets[1].Cells[L, I + 1].NumberFormat := '##0,0000_);';
  75.                     End;
  76.                   // Area liquida
  77.                   If I = 15 Then
  78.                     Begin
  79.                       ExcApp.WorkBooks[1].Sheets[1].Cells[L, I + 1] := '=N' + L.ToString + '*O' +
  80.                         L.ToString;
  81.                       ExcApp.WorkBooks[1].Sheets[1].Cells[L, I + 1].NumberFormat := '##0,0000_);';
  82.                     End;
  83.                 End
  84.               Else
  85.                 ExcApp.WorkBooks[1].Sheets[1].Cells[L, I + 1] := DataSet.Fields[I].DisplayText;
  86.             End;
  87.           DataSet.Next;
  88.           L := L + 1;
  89.           Application.ProcessMessages;
  90.         End;
  91.       ExcApp.WorkBooks[1].WorkSheets[1].Range['A1', 'Y500'].Columns.AutoFit;
  92.       Arq := 'PBA-Inspeção de Chapas GRD ' + DataSet.FieldByName('Material').AsString + '-' +
  93.         DataSet.FieldByName('Bloco').AsString;
  94.       ExcApp.WorkBooks[1].SaveAs(DM.CaminhoExcel + '\' + Arq);
  95.       { Application.MessageBox(Pchar('Arquivo gerado com sucesso!' + #13 + #10 + DM.CaminhoExcel + '\'
  96.         + Arq), 'Aviso', MB_ICONWARNING); }
  97.       FrmMenu.AbrirPasta(DM.CaminhoExcel);
  98.       ExcApp.Quit;
  99.       DM.FdAtuImpresso.Params.ParamByName('codemp').AsInteger := CodEmp;
  100.       DM.FdAtuImpresso.Params.ParamByName('codori').AsString := CodOri;
  101.       DM.FdAtuImpresso.Params.ParamByName('numorp').AsInteger := NumOrp;
  102.       DM.FdAtuImpresso.Execute();
  103.       Application.ProcessMessages;
  104.     End
  105.   Else
  106.     Begin
  107.       Application.MessageBox('Nenhum arquivo gerado.', 'Aviso', MB_ICONWARNING);
  108.       ExcApp.Quit;
  109.     End;
  110. End;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement