AdoQuery数据导出到Word

      Delphi技术 2006-3-24 16:54
procedure TFMediaIllegalStat.saveadlist(media: string; begindate,
enddate: Tdate; begintime, endtime: Ttime; ado: Tadoquery; save: boolean;
savedir: string);
var
typename:string;
docs,template,newtemplate,itemindex,savepath:olevariant;
wordtable,fitWeidth:olevariant;
oleUnit,oleCount,OleDirection:olevariant;
i,colcount,RowCount,col,row:integer;
DataSet:TDataSet;
PreType:string;
TempBool:boolean;
begin
try
if not ado.Active then exit;
if ado.RecordCount<=0 then exit;
screen.Cursor:=crhourglass;
template:=emptyparam;
newtemplate:=true;
itemindex:=1;
try
wordapplication.Connect;
except
messagedlg('word未安装',mterror,[mbOk],0);
abort;
end;
if not save then
wordapplication.Visible:=true;
wordapplication.Caption:=media+'广告串播单';
template:=emptyparam;
newtemplate:=false;
docs:=wordapplication.Documents;
docs.add(newtemplate:=true);
worddocument.ConnectTo(wordapplication.Documents.Item(itemindex));
wordapplication.Options.CheckSpellingAsYouType:=false;
wordapplication.Options.CheckGrammarAsYouType:=false;
Wordapplication.Selection.Font.Size:=12;
Wordapplication.Selection.Font.Name := 'gb-2312';
//=====操作WORD的一些参数
oleUnit:=wdCharacter;
oleCount:=1;
OleDirection:=wdCollapseStart;
fitWeidth:=wdAutoFitContent;
//MoveRight(oleunit,olecount,emptyparam);单元格向右移一格
//InsertRows(olecount);Collapse(OleDirection);插入一行
//TypeParagraph;换行
//TypeText('29秒');输出内容
//======================开始导入
With worddocument.Application.Selection do
Begin
{ParagraphFormat.LeftIndent:=6;
ParagraphFormat.SpaceBeforeAuto:=0;
ParagraphFormat.SpaceAfterAuto:=0;
ParagraphFormat.RightIndent:=44;
ParagraphFormat.SpaceBeforeAuto:=0;
ParagraphFormat.SpaceAfterAuto:=0; }
PageSetup.LeftMargin:=30;
PageSetup.RightMargin:=40;
Font.Size:=16;//表头字体
Font.Name:='GB_2312';
Font.Bold:=1;//粗体
ParagraphFormat.Alignment:=wdAlignParagraphCenter;
TypeText(media+'媒体广告串播单');
TypeParagraph; //表头写完换行
Font.Bold:=0;
Font.Size:=12;
ParagraphFormat.Alignment:=wdAlignParagraphJustify;
TypeText('开始日期: '+datetostr(begindate)+' '+timetostr(begintime)+' 结束日期: '+datetostr(enddate)+' '+timetostr(endtime));
TypeParagraph; //换行
PreType:='';
ado.First;
ado.DisableControls;
Tables.Add(paragraphs.Last.Range,1,8,emptyparam,fitWeidth);//插入一行八列的表格
TypeText('播出日期');
MoveRight(oleunit,olecount,emptyparam);//单元格向右移一格
TypeText('时间');
MoveRight(oleunit,olecount,emptyparam);
TypeText('广告名称');
MoveRight(oleunit,olecount,emptyparam);
TypeText('版本');
MoveRight(oleunit,olecount,emptyparam);
TypeText('时长');
MoveRight(oleunit,olecount,emptyparam);
TypeText('播出形式');
MoveRight(oleunit,olecount,emptyparam);
TypeText('工商类别');
MoveRight(oleunit,olecount,emptyparam);
TypeText('子类别');
MoveRight(oleunit,olecount,emptyparam);
While Not ado.Eof DO
Begin
InsertRows(olecount); //增加一行;
Collapse(OleDirection);
TypeText(ado.fieldbyname('DATETIME').AsString);
MoveRight(oleunit,olecount,emptyparam);//单元格向右移一格
TypeText(ado.fieldbyname('TIME').AsString);
MoveRight(oleunit,olecount,emptyparam);
TypeText(ado.fieldbyname('TITLE').AsString);
MoveRight(oleunit,olecount,emptyparam);
TypeText(ado.fieldbyname('VER').AsString);
MoveRight(oleunit,olecount,emptyparam);
TypeText(ado.fieldbyname('HHMMSS').AsString);
MoveRight(oleunit,olecount,emptyparam);
TypeText(ado.fieldbyname('ISSMODENAME').AsString);
MoveRight(oleunit,olecount,emptyparam);
TypeText(ado.fieldbyname('TYPENAME').AsString);
MoveRight(oleunit,olecount,emptyparam);
TypeText(ado.fieldbyname('ADSYZLBMC').AsString);
MoveRight(oleunit,olecount,emptyparam);
ado.Next;
End;
MoveRight(oleunit,olecount,emptyparam);
TypeParagraph;
End;


//======================
if save then
begin
savepath:=savedir;
Worddocument.SaveAs(savepath);
Worddocument.Close;
Worddocument.Disconnect;
end;
ado.EnableControls;
Wordapplication.Disconnect; //这句不可少,不然会出现RPC服务器找不到的错误;
screen.Cursor:=crdefault;
except
on E:Exception do
begin
showmessage(E.Message+'请查看TemplateIni文件夹下的INI文件配置是否有误!');
wordapplication.Disconnect;
screen.Cursor:=crdefault;
end;
end;

代码有点乱,但100%可以运行.运行的结果类似如下:


图片如下:
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commenttime}{commentauthor}

{CommentUrl}
{commentcontent}