ADODataSet1.next问题 delphi

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ADODataSet1.next问题 delphi相关的知识,希望对你有一定的参考价值。

我想将dbgrid中的数据有选择性的导入word模板中。ADODataSet1连接的是dbgrid,问题出现在只能导入查询到的几行数据的一行(第一行)。(我用adoquery这样做就不存在着问题)
procedure TUser.ToolButton5Click(Sender: TObject);
var
template1, template2, itemindex, vTable: OleVariant;
template11, template22 ,wl: string;
t: dword;
WordApplicationxx: TWordApplication;
i:integer;
begin
//gridtoword(DBGrid1,'报表导出Word文档','组织部');
// ADODataSet1.Active:=false;
// ADODataSet1.Active:=true;
i:=0;
while not ADODataSet1.Eof do //当数据集的内容不为空时 做begin end 内的事情
begin
i:=i+1;
//向已有的模板插入数据的时候,查对应的回车数,代表行,然后查列(行,列)
//动态生成wordapplicationxx控件
WordApplicationxx := TWordApplication.Create(nil);
template1 := ExtractFilePath(Application.ExeName) + '挂职锻炼干部名单.doc';
template11 := template1;
//动态生成文件名
form6.DateTimePicker4.DateTime := Trunc(Now);
wl := formatdatetime('yyyymmddhhmmss',form6.datetimepicker4.datetime);
template2 := ExtractFilePath(Application.ExeName)+wl+'挂职锻炼干部名单.doc';
template22 := template2;

//复制文件
CopyFile(pchar(template11), pchar(template22), false);
t := gettickcount;
//打开文档
while gettickcount - t < 200 do application.processmessages;
//wordapplicationxx带14个参数
WordApplicationxx.Documents.Open(template2, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam);
itemindex := 1;
worddocument1.connectto(WordApplicationxx.documents.item(itemindex));
WordApplicationxx.visible := true;
Worddocument1.activate;
//插入数据
// vTable := WordApplicationxx.ActiveDocument.Tables.Item(1);
vTable.cell(1+i,1).Range.Text:= ADODataSet1.fieldbyname('姓名').asstring;
vTable.cell(1+i,2).Range.Text:= ADODataSet1.fieldbyname('性别').asstring;
vTable.cell(1+i,3).Range.Text:= ADODataSet1.fieldbyname('出生日期').asstring;
vTable.cell(1+i,4).Range.Text:= ADODataSet1.fieldbyname('政治面貌').asstring;
ADODataSet1.Next;
//释放wordapplicationxx
WordApplicationxx.Destroy;
showmessage('名单生成结束!');
exit;
end;
end;
还是没解决

把代码修改了一下,你试试:

var
template1, template2, itemindex, vTable: OleVariant;
template11, template22 ,wl: string;
t: dword;
WordApplicationxx: TWordApplication;
i:integer;
begin
//gridtoword(DBGrid1,'报表导出Word文档','组织部');
// ADODataSet1.Active:=false;
// ADODataSet1.Active:=true;
i:=0;
if ADODataSet1.RecordCount>0 do //当数据集的内容不为空时 做begin end 内的事情
begin
ADODataSet1.First;
whilt not ADODataSet1.Eof do
begin
i:=i+1;
//向已有的模板插入数据的时候,查对应的回车数,代表行,然后查列(行,列)
//动态生成wordapplicationxx控件
WordApplicationxx := TWordApplication.Create(nil);
template1 := ExtractFilePath(Application.ExeName) + '挂职锻炼干部名单.doc';
template11 := template1;
//动态生成文件名
form6.DateTimePicker4.DateTime := Trunc(Now);
wl := formatdatetime('yyyymmddhhmmss',form6.datetimepicker4.datetime);
template2 := ExtractFilePath(Application.ExeName)+wl+'挂职锻炼干部名单.doc';
template22 := template2;

//复制文件
CopyFile(pchar(template11), pchar(template22), false);
t := gettickcount;
//打开文档
while gettickcount - t < 200 do application.processmessages;
//wordapplicationxx带14个参数
WordApplicationxx.Documents.Open(template2, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam, emptyparam);
itemindex := 1;
worddocument1.connectto(WordApplicationxx.documents.item(itemindex));
WordApplicationxx.visible := true;
Worddocument1.activate;
//插入数据
// vTable := WordApplicationxx.ActiveDocument.Tables.Item(1);
vTable.cell(1+i,1).Range.Text:= ADODataSet1.fieldbyname('姓名').asstring;
vTable.cell(1+i,2).Range.Text:= ADODataSet1.fieldbyname('性别').asstring;
vTable.cell(1+i,3).Range.Text:= ADODataSet1.fieldbyname('出生日期').asstring;
vTable.cell(1+i,4).Range.Text:= ADODataSet1.fieldbyname('政治面貌').asstring;
ADODataSet1.Next;
end;
//释放wordapplicationxx
WordApplicationxx.Destroy;
showmessage('名单生成结束!');
end;
参考技术A 既然是连dbgrid 就用
dbgrid.datasource.dateset替换你的ADODataSet1

while 前加上
dbgrid.datasource.dateset.Frist;

delphi DBGridEh 问题

DBGridEh的OnGetCellParams事件中
if qymst.FieldByName('spunit').AsString = 'm' then
begin
dbgrideh1.FieldColumns['pcs'].Font.Color := clred;
end
else
dbgrideh1.FieldColumns['pcs'].Font.Color := clnone;为什么当spunit <> m 的时候怎个网格都在闪

OnGetCellParams事件是会一直运行的(相当于刷新),如果你这里要当qymst.FieldByName(\'spunit\').AsString = \'m\'时才改变字体的颜色为红色,那么你完成可以设置所有字体的颜色为clnone,然后将事件改为:
if qymst.FieldByName(\'spunit\').AsString = \'m\' then
begin
dbgrideh1.FieldColumns[\'pcs\'].Font.Color := clred;
end;
这样就好了。
参考技术A 因为一直在重复赋值。当spunit <> m 的时候,字段pcs的字体颜色为空。但是你代码不该放在getcellparams事件中,建议放在DataSource的 dataChange事件中。

以上是关于ADODataSet1.next问题 delphi的主要内容,如果未能解决你的问题,请参考以下文章

delphi 2010 没有QuickRpt问题

delphi程序问题

delphi 重载问题

Delphi问题

delphi 换版本乱码 问题

delphi开源问题编译