Einzelnen Beitrag anzeigen

nahpets
(Gast)

n/a Beiträge
 
#2

AW: DBGrid in MS-Word exportieren und Text einfügen

  Alt 16. Mai 2017, 12:43
ungetestet:
Delphi-Quellcode:
procedure GridToWord(WordApp : OleVariant; Grid :TDBGrid ; FormatNum :integer);
var
  x : integer;
  y : integer;
  GColCount : integer;
  GRowCount : integer;
begin
  GColCount := Grid.Columns.Count;
  GRowCount := Grid.DataSource.DataSet.RecordCount;
  WordApp.Range.Font.Size := Grid.Font.Size;
  WordApp.PageSetup.Orientation := 1;
  WordApp.Tables.Add( WordDokument.Range,GRowCount+1,GColCount);
  WordApp.Range.InsertAfter('Date ' + Datetimetostr(Now));
  WordApp.Range.Tables.Item(1).AutoFormat(FormatNum,1,1,1,1,1,0,0,0,1);
  for y := 1 to GColCount do begin
    WordApp.Tables.Item(1).Cell(1,y).Range.InsertAfter(Grid.Columns[y-1].Title.Caption);
  end;
  x := 1;
  Grid.DataSource.DataSet.First;
  while not Grid.DataSource.DataSet.Eof do begin
    x := x + 1;
    for y := 1 to GColCount do begin
      WordApp.Tables.Item(1).Cell(x,y).Range.InsertAfter(Grid.DataSource.DataSet.FieldByName(Grid.Columns[y - 1].FieldName).Asstring);
      // Würde hier nicht dashier ausreichen?
      WordApp.Tables.Item(1).Cell(x,y).Range.InsertAfter(Grid.Columns[y - 1].Field.AsString);
    end;
    Grid.DataSource.DataSet.Next;
  end;
  WordApp.Range.Tables.Item(1).UpdateAutoFormat;
end;

begin
  try
    WordApp := CreateOleObject('Word.Application');
  except
    on e : Exception do begin
      ShowMessage(e.Message);
      exit;
    end;
  end;
  WordApp.Visible := true;
  WordDokument := WordApp.Application.Documents.Add;
   // Schriftart für die Überschrift
  WordApp.Selection.Font.Name := 'Calibri';
  WordApp.Selection.Font.Size := 20;
  WordApp.Selection.ParagraphFormat.Alignment := 1;
  // Überschrift
  WordApp.Selection.TypeText('EinText');
  // Schriftart für restlichen Text
  WordApp.Selection.Font.Size := 11;
  WordApp.Selection.ParagraphFormat.Alignment := 0;
  // Text einfügen
  WordApp.Selection.TypeText('Firma: Musterfirma GmbH');
  WordApp.Selection.TypeParagraph;
  WordApp.Selection.TypeText(Format('Mitarbeiter: %s %s'[Vorname,Nachname]));
  WordApp.Selection.TypeParagraph;
  WordApp.Selection.TypeText(Format('Personalnummer: %d',[PersonalNr]));
  WordApp.Selection.TypeParagraph;
  WordApp.Selection.TypeParagraph;
  GridToWord(WordApp, Grid, FormatNum);
end;
  Mit Zitat antworten Zitat