Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit UPrinting;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, DB, Grids, DBGrids, ADODB, ExtCtrls, DBCtrls, ComCtrls, comobj, OleServer, ExcelXP, UMain;
- type
- TPrinting = class(TForm1)
- procedure CreateWord(const visible: Boolean);
- procedure Print;
- procedure PBar;
- private
- public
- end;
- var
- Printing: TPrinting;
- Word1: Variant;
- pb: TProgressBar;
- implementation
- // uses UMain;
- procedure TPrinting.CreateWord(const visible: Boolean); // «Создание объекта Word.Application».
- begin
- Word1 := CreateOleObject('Word.Application');
- Word1.visible := visible;
- Word1.Documents.Add;
- end;
- procedure TPrinting.PBar;
- var
- pb: TProgressBar;
- begin
- pb := TProgressBar.create(Self);
- pb.parent := Form1;
- pb.Top := 350;
- pb.Left := 350;
- pb.Smooth := True;
- pb.Max := Form1.ADOQuery1.RecordCount;
- pb.Visible := True;
- Form1.ADOQuery1.RecNo := 1;
- end;
- procedure TPrinting.Print;
- var
- WordTable, WordRow: OleVariant;
- I: Integer;
- begin
- CreateWord(True); //«Создание отчета в новом документе Word».;
- {
- «InsertBefore вставляет текст в начало содержимого Range - объекта работы с текстом»
- и «InsertAfter вставляет текст в конец содержимого Range».
- }
- // Word1.ActiveDocument.tables.item(1).columns.item(<номер столбца>).delete // удалить столбцы
- if ADOQuery1.FieldCount > 2 then
- Word1.activeDocument.PageSetup.Orientation := 1; // Альбомная
- //Word1.activeDocument.PageSetup.Orientation := 0; // Книжная
- Word1.ActiveDocument.Range.InsertBefore (Form1.Caption + ' ');
- Word1.ActiveDocument.Range.InsertAfter (#13+#10+'cпециальности: "Программное обеспечение"'+#13+#10);
- Word1.ActiveDocument.Range.InsertAfter ('вычислительной техники и автоматизированных систем'+#13+#10+#13+#10);
- //«Изменение параметров абзаца и шрифта».
- Word1.ActiveDocument.Range.ParagraphFormat.Alignment := 1;
- Word1.ActiveDocument.Range.Font.Name := 'Times New Roman';
- Word1.ActiveDocument.Range.Font.Size := 16;
- Word1.ActiveDocument.Range.Font.Bold := True;
- Word1.ActiveDocument.Range.Characters.Last.Font.Size := 12;; //Изменение формата абзаца со следующей строки
- Word1.ActiveDocument.Range.Characters.Last.Font.Bold := False;
- //Создание таблицы
- Word1.ActiveDocument.Tables.Add(Word1.ActiveDocument.Range.Characters.Last,1,adoQuery1.Fields.Count);
- WordTable := Word1.ActiveDocument.Tables.Item(1);
- // Изменение ширины столбцов
- if ADOQuery1.FieldCount = 9 then
- begin
- Word1.ActiveDocument.tables.item(1).columns.item(1).PreferredWidth := 50;
- Word1.ActiveDocument.tables.item(1).columns.item(2).PreferredWidth := 90;
- Word1.ActiveDocument.tables.item(1).columns.item(3).PreferredWidth := 90;
- Word1.ActiveDocument.tables.item(1).columns.item(4).PreferredWidth := 100;
- Word1.ActiveDocument.tables.item(1).columns.item(7).PreferredWidth := 110;
- end;
- if ADOQuery1.FieldCount = 5 then
- begin
- //
- end;
- for I := 1 to DBGrid1.fieldcount do
- begin
- WordTable.Cell(1,i).range.text := (DBGrid1.Columns[i-1].title.caption);
- WordTable.Cell(1,i).range.font.bold := True;
- end;
- with adoquery1 do
- while not eof do begin
- pb.Position := ADOQuery1.RecNo;
- //Добавляя новые строки
- WordRow := WordTable.Rows.Add;
- if ADOQuery1.FieldCount = 9 then
- begin
- WordTable.Cell(WordRow.Index, 2).Range.ParagraphFormat.Alignment := 0;
- WordTable.Cell(WordRow.Index, 3).Range.ParagraphFormat.Alignment := 0;
- WordTable.Cell(WordRow.Index, 4).Range.ParagraphFormat.Alignment := 0;
- WordTable.Cell(WordRow.Index, 9).Range.ParagraphFormat.Alignment := 0;
- end;
- if ADOQuery1.FieldCount = 5 then
- begin
- WordTable.Cell(WordRow.Index, 1).Range.ParagraphFormat.Alignment := 0;
- WordTable.Cell(WordRow.Index, 2).Range.ParagraphFormat.Alignment := 0;
- WordTable.Cell(WordRow.Index, 3).Range.ParagraphFormat.Alignment := 0;
- end;
- for I := 1 to FieldCount do
- // Вписываем в ячейки данные
- begin
- WordTable.Cell(WordRow.Index, i).Range.Text := Fields[i-1].AsString;
- WordTable.Cell(WordRow.Index,i).range.font.bold := False;
- // WordTable.Cell(WordRow.Index,i).range.ParagraphFormat.Alignment := 0;
- end;
- adoquery1.Next;
- end;
- WordTable.borders.enable := True;
- Word1.ActiveDocument.Range.Characters.Last.ParagraphFormat.Alignment := 0;
- Word1.ActiveDocument.Range.InsertAfter(#13+#10);
- Word1.ActiveDocument.Range.InsertAfter(#13+#10);
- Word1.ActiveDocument.Range.InsertAfter ('Подпись________________ ');
- Word1.ActiveDocument.Range.InsertAfter (Date);
- pb.Destroy;
- end;
- end.
Add Comment
Please, Sign In to add comment