print

  • 格式:doc
  • 大小:6.00 KB
  • 文档页数:6

unit Unit1;interfaceusesWindows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, ShellAPI, ComObj,Word2000,Word97;{Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,ExtCtrls,Grids,StdCtrls,Buttons,ComCtrls,Db,DBTables,ToolWin,ImgList,DBGrids,inifiles,Registry,DateUtils,jpeg,Excel2000,Word2000,Word97,Variants,ComObj, Menus;}typeTForm1 = class(TForm)btn1: TButton;btnOpen: TButton;btnGenWord: TButton;btn2: TButton;procedure btn1Click(Sender: TObject);procedure btnOpenClick(Sender: TObject);procedure btnGenWordClick(Sender: TObject);procedure btn2Click(Sender: TObject);procedure FormCreate(Sender: TObject);privatenCount:integer;{ Private declarations }public{ Public declarations }end;varForm1: TForm1;implementation{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);beginShellExecute(self.Handle, 'print', PChar('c:\document.doc'), nil, nil, SW_HIDE); end;procedure TForm1.btnOpenClick(Sender: TObject);beginShellExecute(self.Handle, 'open', PChar('c:\document.doc'), nil, nil, SW_HIDE); end;procedure TForm1.btnGenWordClick(Sender: TObject);varWordApp:OleVariant;DocApp:OleVariant;BookMark_Name:String;begintry//说明:机器上必须要安装Office 带有Word//判断内存中是否已经运行了WordWordApp := GetActiveOleObject('word.Application');//if(WordApp = nil ) thenexcepttryWordApp := CreateOleObject('Word.Application');exceptexit;end;end;WordApp.Visible:=false;DocApp:=WordApp.Documents.add(ExtractFilePath(Application.ExeName)+'BYYB_Print.d ot');//赋值BookMark_Name := '卡号'; //标签DocApp.Application.Selection.goto(What := wdGoToBookmark, Name := BookMark_Name);DocApp.Application.Selection.Text := '123456';BookMark_Name := '充值金额'; //标签DocApp.Application.Selection.goto(What := wdGoToBookmark, Name := BookMark_Name);DocApp.Application.Selection.Text := '1.00';BookMark_Name := '充值日期'; //标签DocApp.Application.Selection.goto(What := wdGoToBookmark, Name := BookMark_Name);DocApp.Application.Selection.Text :='2014-04-21' ;BookMark_Name := '当前余额'; //标签DocApp.Application.Selection.goto(What := wdGoToBookmark, Name := BookMark_Name);DocApp.Application.Selection.Text :='150.00' ;//DocApp.SaveAs( ExtractFilePath(Application.ExeName)+'BYYB_Print.doc');WordApp.Printout;DocApp:=UnAssigned;WordApp:=UnAssigned;end;procedure TForm1.btn2Click(Sender: TObject);varj: integer;AppPath: string;ExlApp: Variant; //统计报表打印时用xlsApp:OleVariant;nn:Integer;beginInc(nCount);tryExlApp := GetActiveOleObject('Excel.Application');excepttryExlApp := CreateOleObject('Excel.Application');exceptMessageDlg('您的机器里未安装Microsoft Excel!', mtError, [mbOk], 0);exit;end;end;//deletefile(ExtractFilePath(Application.ExeName)+'report\sb\BYSB_YLBGPrint.xls' );ExlApp.Visible := false;//打开已存在地EXCEL模板xlsApp :=ExlApp.WorkBooks.Open(ExtractFilePath(Application.ExeName) + 'Byyb_Print.xlt');//xlsApp.clear;j := 2;{qrycx.first;while not qrycx.Eof doExlApp.Cells[j, 1] := qrycx.Fields[0].Asstring;ExlApp.Cells[j, 2] := qrycx.Fields[1].AsString;ExlApp.Cells[j, 3] := qrycx.Fields[2].AsString;ExlApp.Cells[j, 4] := qrycx.Fields[3].AsString;ExlApp.Cells[j, 5] := qrycx.Fields[4].Asstring;ExlApp.Cells[j, 6] := qrycx.Fields[5].AsString;ExlApp.Cells[j, 7] := qrycx.Fields[6].AsString;ExlApp.Cells[j, 8] := qrycx.Fields[7].AsString;ExlApp.Cells[j, 9] := qrycx.Fields[8].AsString;ExlApp.Cells[j, 10] := qrycx.Fields[9].AsString;ExlApp.Cells[j, 11] := qrycx.Fields[10].AsString;j := j + 1;qrycx.Next;end;//显示当前窗口}nn := nCount mod 3;case nn of0:beginExlApp.Cells[2, 1] := '123' + '序号:' + IntToStr(nCount); ExlApp.Cells[2, 2] := '测试姓名1';ExlApp.Cells[3, 1] := '123' + '序号:' + IntToStr(nCount); ExlApp.Cells[3, 2] := '测试姓名2';ExlApp.Cells[4, 1] := '123' + '序号:' + IntToStr(nCount); ExlApp.Cells[4, 2] := '测试姓名3';end;1:beginExlApp.Cells[2, 1] := '123' + '序号:' + IntToStr(nCount); ExlApp.Cells[2, 2] := '测试姓名4';ExlApp.Cells[3, 1] := '123' + '序号:' + IntToStr(nCount); ExlApp.Cells[3, 2] := '测试姓名5';end;2:beginExlApp.Cells[2, 1] := '123' + '序号:' + IntToStr(nCount); ExlApp.Cells[2, 2] := '测试姓名6';end;ExlApp.ActiveSheet.PrintOut;ExlApp.ActiveWorkBook.Saved := false;//奇怪的,在关闭时,会提示是否保存的对话框?//ExlApp.WorkBooks.Close;//ExlApp.Quit;//另外,由于没有关闭,execl一直在内存中运行,导致后面的数据一直覆盖前面的数据,因此,如果//第二次运行该功能的时候,当第一次如果打印的数据比第二次打印的数据多,(比如:上面的例子,//第一次打印了3行,第二次打印了2行,此时,实际上第二次打印了3行,第3行是第一次的数据,第1,2行的数据被改写了//因此,这里可以变通一下,将数据清空case nn of0:beginExlApp.Cells[2, 1] := '';ExlApp.Cells[2, 2] := '';ExlApp.Cells[3, 1] := '';ExlApp.Cells[3, 2] := '';ExlApp.Cells[4, 1] := '';ExlApp.Cells[4, 2] := '';end;1:beginExlApp.Cells[2, 1] := '';ExlApp.Cells[2, 2] := '';ExlApp.Cells[3, 1] := '';ExlApp.Cells[3, 2] := '';end;2:beginExlApp.Cells[2, 1] := '';ExlApp.Cells[2, 2] := '';end;end;ExlApp := Unassigned;end;procedure TForm1.FormCreate(Sender: TObject); beginnCount :=0;end;end.。