AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Object-Pascal / Delphi-Language DBGrid in MS-Word exportieren und Text einfügen
Thema durchsuchen
Ansicht
Themen-Optionen

DBGrid in MS-Word exportieren und Text einfügen

Ein Thema von localh0st · begonnen am 16. Mai 2017 · letzter Beitrag vom 16. Mai 2017
Antwort Antwort
localh0st

Registriert seit: 10. Apr 2017
4 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#1

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

  Alt 16. Mai 2017, 14:19
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;
Super, Dankeschön
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 08:29 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz