唉下面代码你就慢慢看吧,其中有一些是我写的函数不理它就可以了 最好是看DELPHI的帮助都有的 var strdwn,stropenfile,strvalue:string; DocInx,oFileName,CfCversions,oReadOnly,AddToRctFiles,PswDocument, PswTemplate,oRevert,WPswDocument,WPswTemplate,oFormat: OleVariant; FileName,SaveChanges, OriginalFormat, RouteDocument: OleVariant; iRow,iCol:integer; // myCell:Cell; // myRow:Row; adoetzifile,adoETchanpinfile,adotuntinfo,adodatazhpath,adost:_recordset; begin if (not FCustomerDM.ADOQryET_zizhifile.Active) or ( not FCustomerDM.ADOQryET_chanpinfile.Active ) then begin MessageDlg('没有输出的企业资质信息!', mtInformation, [mbOk], 0); Exit; end; if selectdb('dbstate','*','u_cid='+''''+edit1.Text+''''+' and '+'I_itemID='+''''+edit2.Text +'''',adost)=0 then begin if adost.Fields[6].Value ='true' then begin ShowMessage('项目还没有结束,不需要输出吧!'); Exit; end; end; Screen.Cursor := crHourGlass; try WordApp.Connect ; except MessageDlg('您的WORD没有安装!', mtError, [mbOk], 0); exit; Abort; end; WordApp.Visible :=False; //True; dlgOpen.FileName:=GAppPath+'moban\资质审核结果moban.doc' ; //FileName := dlgOpen.FileName;//附加 //if not dlgOpen.Execute then //Exit; DocInx:=1; oFileName := dlgOpen.filename; oReadOnly:=true; //打开WORD的属性 CfCversions := EmptyParam; AddToRctFiles:= EmptyParam; PswDocument:= EmptyParam; PswTemplate:= EmptyParam; oRevert:= EmptyParam; WPswDocument:= EmptyParam; WPswTemplate:= EmptyParam; oFormat:= EmptyParam; //encoding:=EmptyParam; // visible:=false; // ===== 打开文件 ===== WordApp.Documents.Openold(oFileName,cfcversions,oReadOnly,addtorctfiles,pswdocument,pswtemplate,orevert, wpswdocument,wpswtemplate,oformat) ; //open // ===== 关联文件 ===== WordDoc.ConnectTo(WordApp.Documents.Item(DocInx)); //方法(1)==> 规则表 // WordDoc.Tables.Item.Rows:=87; WordDoc.Words.Item(1).Text :=Label1.Caption;//输出文档的标题啊,啊哈哈 //WordDoc.Words.Item(200).Text:='ssadfsad'; //连接数据库 if selectdb('ET_zizhifile','*','',adoetzifile)=0 then begin adoetzifile.MoveFirst ; For iRow := 1 To adoetzifile.RecordCount do begin strvalue:= adoetzifile.Fields[0].Value; if selectdb('Tunitinfo','*','T_cID='+''''+strvalue +'''',adotuntinfo)=0 then begin WordDoc.Tables.Item(1).Rows.Add(EmptyParam); For icol := 1 To WordDoc.Tables.Item(1).Columns.Count do //第 iCol列 begin WordDoc.Tables.Item(1).Cell(iRow+1,icol).Range.Text:=adotuntinfo.Fields[icol].Value; end; end; adoetzifile.MoveNext; end; adoetzifile.MoveFirst ; For iRow := 2 To adoetzifile.RecordCount+1 do begin WordDoc.Tables.Item(2).Rows.Add(EmptyParam); For icol := 1 To WordDoc.Tables.Item(2).Columns.Count do //第 iCol列 begin WordDoc.Tables.Item(2).Cell(iRow+1,icol).Range.Text:=adoetzifile.Fields[icol+1].Value; end; adoetzifile.MoveNext; end; end else begin showmessage('没有输出的记录啊,笨笨,重新去验证吧,'); Screen.Cursor := crDefault; exit; end; //******************* Screen.Cursor := crHourGlass; if selectdb('ET_chanpinfile','*','',adoETchanpinfile)=0 then begin adoETchanpinfile.MoveFirst ; For iRow := 1 To adoETchanpinfile.RecordCount do begin WordDoc.Tables.Item(3).Rows.Add(EmptyParam); For icol := 1 To WordDoc.Tables.Item(3).Columns.Count do //第 iCol列 begin if icol=1 then begin WordDoc.Tables.Item(3).Cell(iRow+1,icol).Range.Text:=adoETchanpinfile.Fields[icol+2].Value; Continue; end; if icol=2 then begin WordDoc.Tables.Item(3).Cell(iRow+1,icol).Range.Text:=adoETchanpinfile.Fields[icol].Value; Continue; end; WordDoc.Tables.Item(3).Cell(iRow+1,icol).Range.Text:=adoETchanpinfile.Fields[icol+1].Value; end; adoETchanpinfile.MoveNext; end; end else begin showmessage('没有输出的记录啊,笨笨,重新去验证吧,'); Screen.Cursor := crDefault; exit; end; // end;// else // begin // ShowMessage('输出企业的基本信息错误!'); // Exit; // end; //***************************************************start if selectdb('zhaotreepath','*','U_cid='+''''+edit1.Text+'''',adodatazhpath)=0 then begin strdwn:=strReplace(adodatazhpath.Fields[1].Value,'\','/')+'/'+edit2.Text+'.doc'; if UpdateDb('zhaouptoudow','downRpath',strdwn,'U_cid='+''''+edit1.Text+''''+' and freeItemid='+''''+edit2.Text+'''')=0 then begin //if SaveDialog1.Execute then FileName := GAppPath+'bs\'+adodatazhpath.Fields[1].Value+'\'+edit2.Text+'.doc'; stropenfile:=FileName; // SaveDialog1.FileName; // FileName:='d:\haha.doc'; WordDoc.SaveAs(FileName) ; end else begin showmessage('没成功!'); exit; end; end else begin ShowMessage('招标单位的上传下载路径没有分配!'); exit; end; //***************************************************end SaveChanges := wdDoNotSaveChanges; //wdDoNotSaveChanges或 wdSaveChanges常量:) OriginalFormat := UnAssigned; RouteDocument := UnAssigned; try WordApp.Quit(SaveChanges, OriginalFormat, RouteDocument); WordApp.Disconnect; cursor:=crDefault; except on E: Exception do begin Showmessage(E.Message); WordApp.Disconnect; cursor:=crDefault; end; end; Screen.Cursor := crDefault; if MessageDlg('验证结果输出完毕!需要打开看看?',mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin try ShellExecute(application.Handle,nil,pchar(stropenfile),nil,nil,SW_SHOWNORMAL) except Messagebox(application.Handle, '打开文件时错误!',' :( ',MB_ICONERROR,); end; end; end;
最好是看DELPHI的帮助都有的
var
strdwn,stropenfile,strvalue:string;
DocInx,oFileName,CfCversions,oReadOnly,AddToRctFiles,PswDocument,
PswTemplate,oRevert,WPswDocument,WPswTemplate,oFormat: OleVariant;
FileName,SaveChanges, OriginalFormat, RouteDocument: OleVariant;
iRow,iCol:integer;
// myCell:Cell;
// myRow:Row;
adoetzifile,adoETchanpinfile,adotuntinfo,adodatazhpath,adost:_recordset;
begin
if (not FCustomerDM.ADOQryET_zizhifile.Active) or ( not FCustomerDM.ADOQryET_chanpinfile.Active ) then
begin
MessageDlg('没有输出的企业资质信息!', mtInformation, [mbOk], 0);
Exit;
end; if selectdb('dbstate','*','u_cid='+''''+edit1.Text+''''+' and '+'I_itemID='+''''+edit2.Text +'''',adost)=0 then
begin
if adost.Fields[6].Value ='true' then
begin
ShowMessage('项目还没有结束,不需要输出吧!');
Exit;
end;
end; Screen.Cursor := crHourGlass;
try
WordApp.Connect ;
except
MessageDlg('您的WORD没有安装!', mtError, [mbOk], 0);
exit;
Abort;
end;
WordApp.Visible :=False; //True;
dlgOpen.FileName:=GAppPath+'moban\资质审核结果moban.doc' ;
//FileName := dlgOpen.FileName;//附加
//if not dlgOpen.Execute then
//Exit;
DocInx:=1;
oFileName := dlgOpen.filename;
oReadOnly:=true; //打开WORD的属性
CfCversions := EmptyParam;
AddToRctFiles:= EmptyParam;
PswDocument:= EmptyParam;
PswTemplate:= EmptyParam;
oRevert:= EmptyParam;
WPswDocument:= EmptyParam;
WPswTemplate:= EmptyParam;
oFormat:= EmptyParam;
//encoding:=EmptyParam;
// visible:=false;
// ===== 打开文件 ===== WordApp.Documents.Openold(oFileName,cfcversions,oReadOnly,addtorctfiles,pswdocument,pswtemplate,orevert,
wpswdocument,wpswtemplate,oformat) ; //open
// ===== 关联文件 =====
WordDoc.ConnectTo(WordApp.Documents.Item(DocInx));
//方法(1)==> 规则表 // WordDoc.Tables.Item.Rows:=87; WordDoc.Words.Item(1).Text :=Label1.Caption;//输出文档的标题啊,啊哈哈
//WordDoc.Words.Item(200).Text:='ssadfsad'; //连接数据库
if selectdb('ET_zizhifile','*','',adoetzifile)=0 then
begin
adoetzifile.MoveFirst ;
For iRow := 1 To adoetzifile.RecordCount do
begin
strvalue:= adoetzifile.Fields[0].Value;
if selectdb('Tunitinfo','*','T_cID='+''''+strvalue +'''',adotuntinfo)=0 then
begin
WordDoc.Tables.Item(1).Rows.Add(EmptyParam);
For icol := 1 To WordDoc.Tables.Item(1).Columns.Count do //第 iCol列
begin
WordDoc.Tables.Item(1).Cell(iRow+1,icol).Range.Text:=adotuntinfo.Fields[icol].Value;
end;
end;
adoetzifile.MoveNext;
end;
adoetzifile.MoveFirst ;
For iRow := 2 To adoetzifile.RecordCount+1 do
begin
WordDoc.Tables.Item(2).Rows.Add(EmptyParam);
For icol := 1 To WordDoc.Tables.Item(2).Columns.Count do //第 iCol列
begin
WordDoc.Tables.Item(2).Cell(iRow+1,icol).Range.Text:=adoetzifile.Fields[icol+1].Value;
end;
adoetzifile.MoveNext;
end;
end else
begin
showmessage('没有输出的记录啊,笨笨,重新去验证吧,');
Screen.Cursor := crDefault;
exit;
end;
//*******************
Screen.Cursor := crHourGlass;
if selectdb('ET_chanpinfile','*','',adoETchanpinfile)=0 then
begin
adoETchanpinfile.MoveFirst ;
For iRow := 1 To adoETchanpinfile.RecordCount do
begin
WordDoc.Tables.Item(3).Rows.Add(EmptyParam);
For icol := 1 To WordDoc.Tables.Item(3).Columns.Count do //第 iCol列
begin
if icol=1 then
begin
WordDoc.Tables.Item(3).Cell(iRow+1,icol).Range.Text:=adoETchanpinfile.Fields[icol+2].Value;
Continue;
end;
if icol=2 then
begin
WordDoc.Tables.Item(3).Cell(iRow+1,icol).Range.Text:=adoETchanpinfile.Fields[icol].Value;
Continue;
end;
WordDoc.Tables.Item(3).Cell(iRow+1,icol).Range.Text:=adoETchanpinfile.Fields[icol+1].Value; end;
adoETchanpinfile.MoveNext;
end;
end else
begin
showmessage('没有输出的记录啊,笨笨,重新去验证吧,');
Screen.Cursor := crDefault;
exit;
end;
// end;// else
// begin
// ShowMessage('输出企业的基本信息错误!');
// Exit;
// end;
//***************************************************start
if selectdb('zhaotreepath','*','U_cid='+''''+edit1.Text+'''',adodatazhpath)=0 then
begin
strdwn:=strReplace(adodatazhpath.Fields[1].Value,'\','/')+'/'+edit2.Text+'.doc';
if UpdateDb('zhaouptoudow','downRpath',strdwn,'U_cid='+''''+edit1.Text+''''+' and freeItemid='+''''+edit2.Text+'''')=0 then
begin
//if SaveDialog1.Execute then
FileName := GAppPath+'bs\'+adodatazhpath.Fields[1].Value+'\'+edit2.Text+'.doc';
stropenfile:=FileName;
// SaveDialog1.FileName;
// FileName:='d:\haha.doc';
WordDoc.SaveAs(FileName) ;
end else
begin
showmessage('没成功!');
exit;
end;
end else
begin
ShowMessage('招标单位的上传下载路径没有分配!');
exit;
end;
//***************************************************end
SaveChanges := wdDoNotSaveChanges; //wdDoNotSaveChanges或 wdSaveChanges常量:)
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
try
WordApp.Quit(SaveChanges, OriginalFormat, RouteDocument);
WordApp.Disconnect;
cursor:=crDefault;
except
on E: Exception do
begin
Showmessage(E.Message);
WordApp.Disconnect;
cursor:=crDefault;
end; end;
Screen.Cursor := crDefault; if MessageDlg('验证结果输出完毕!需要打开看看?',mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
try
ShellExecute(application.Handle,nil,pchar(stropenfile),nil,nil,SW_SHOWNORMAL)
except
Messagebox(application.Handle, '打开文件时错误!',' :( ',MB_ICONERROR,);
end;
end;
end;
form上放置以下控件:一个Tadoquery组件,名称为ADOQuery1;
一个TWordFont组件,名称为WordFont1;
一个TWordApplication组件,名称为WordApplication1;
一个TWordDocument1组件,名称为Doc;
一个TWordParagraphFormat组件,名称为ParaFmt;
一个TIMAGE组件,名称为IMAGE1;
一个TBUTTON组件,名称为Button1;
procedure TFrm_TravilRDXL.EnterText;//自定义函数
var
S: Selection;
MyFormat : Word;
// Bitmap : TBitMap;
AData :cardinal;
APalette : hpalette;
begin
S := WordApplication1.Selection;
WordFont1.ConnectTo(S.Font);
S.TypeText('甘肃省兰州市旅游管理局热点线路统计表');
//加回车键,形成第一个段落:Doc.Paragraphs.Item(1)
S.TypeParagraph;
//输入文字
S.TypeText(txt_DateFrom.Text+'至'+txt_DateTo.text);
//加回车键,形成第二个段落:Doc.Paragraphs.Item(2)
S.TypeParagraph;
S.TypeText('使用部门:'+'销售部'+#13);
S.TypeText('时间范围:'+'2002-8-8至2002-8-28'+#13 );
S.TypeText('线路类型:'+'团体线'+#13);
s.TypeText('线路等级:'+'豪华线'+#13);
//加回车键,形成第三个段落:Doc.Paragraphs.Item(3)
S.TypeParagraph;
//--------------------------设置段落1属性-------------------//
ParaFmt.ConnectTo(Doc.Paragraphs.Item(1).Format);
//绑定WordFont1与Doc.Paragraphs.Item(1).Range.Font
WordFont1.ConnectTo(Doc.Paragraphs.Item(1).Range.Font);
//设置段落对齐方式
ParaFmt.Alignment := wdAlignParagraphCenter;
//设置段落行距(磅)
ParaFmt.SpaceAfter := 24;
//画单水平线
ParaFmt.Borders.Item(3).LineStyle := wdLineStyleSingle;
WordFont1.Size := 18;
WordFont1.Name := 'Arial';
WordFont1.Bold := integer(True);
WordFont1.ColorIndex:=5; //粉红色字体
//--------------------------设置段落2属性-------------------//
ParaFmt.ConnectTo(Doc.Paragraphs.Item(2).Format);
//绑定WordFont1与Doc.Paragraphs.Item(1).Range.Font
WordFont1.ConnectTo(Doc.Paragraphs.Item(2).Range.Font);
//设置段落对齐方式
ParaFmt.Alignment := wdAlignParagraphCenter;
//设置段落行距(磅)
ParaFmt.SpaceAfter := 12;
WordFont1.Size := 18;
WordFont1.Name := 'Arial';
end;
procedure TFrm_TravilRDXL.Button1Click(Sender: TObject);
var
i,j:integer;
begin
i:=2;//初始化
j:=1;
WordApplication1.Connect;
Doc.ConnectTo(WordApplication1.Documents.Add(EmptyParam, EmptyParam));
WordApplication1.Visible := True;//显示word文档
EnterText;//自定义函数---添加文本
WordApplication1.Selection.SetRange(100,100);//设定段落范围 doc.Tables.Add(WordApplication1.Selection.Range,ADOQuery1.RecordCount+1,5);//创建表格
doc.Tables.Item(1).Columns.Item(1).Width:=120;//设定第一列宽度
//--------------------------设定第一列字体属性-----------------------//
while j<6 do begin
doc.Tables.item(1).cell(1,j).Range.Bold:=integer(true);
WordFont1.ConnectTo(doc.Tables.item(1).cell(1,j).Range.Font);
WordFont1.ColorIndex:=10;
j:=j+1;
end;
//--------------------------向表格中添加数据----------------------------------------//
doc.Tables.Item(1).Cell(1,1).Range.Text:='线路名称';
doc.Tables.Item(1).cell(1,2).Range.text:='人数';
doc.Tables.item(1).cell(1,3).Range.text:='销售额(元)';
doc.Tables.item(1).cell(1,4).Range.text:='毛利润';
doc.Tables.Item(1).cell(1,5).Range.text:='毛利润率';
while not ADOQuery1.Eof do
begin doc.Tables.Item(1).Cell(i,1).Range.Text:=ADOQuery1.fieldbyname('linename').asstring; doc.Tables.Item(1).cell(i,2).Range.text:=ADOQuery1.Fieldbyname('STUFFNUM').asstring; doc.Tables.Item(1).cell(i,3).Range.text:=ADOQuery1.Fieldbyname('TOTALACCOUNT').asstring; doc.Tables.Item(1).cell(i,4).Range.text:=ADOQuery1.Fieldbyname('TOTAL').asstring; doc.Tables.Item(1).cell(i,5).Range.text:=ADOQuery1.Fieldbyname('ration').asstring;
ADOQuery1.Next;
i:=i+1;
end;
//----------------------------------------------------------------------//
WordApplication1.Selection.SetRange(350,350);//设定段落范围
ParaFmt.ConnectTo(WordApplication1.Selection.Paragraphs.item(1).Format);
ParaFmt.Borders.Item(3).ColorIndex:=6;
ParaFmt.Borders.Item(3).LineStyle:=wdLineStyleSingle;//画底线
//-----------设置段落字体----------------------------------------// WordFont1.ConnectTo(WordApplication1.Selection.Paragraphs.Item(1).Range.Font);
WordFont1.Bold:=integer(true);
WordFont1.Size:=16;
//-----------------------------粘贴图形-------------------------//
Image1.Picture.SaveToClipboardFormat(MyFormat,AData,APalette);
Clipboard.SetAsHandle(MyFormat,AData);
doc.Sentences.Last.Paste;
//--------------------------插入记录-------------------------------------//
doc.Range.InsertAfter(#13+'操作员:'+ G_username);//G_username为全局变量
end;