当前位置:编程学习 > Delphi >>

用Delphi在word中插入表

在Delphi6中调用word,并在文档中插入一个数据库表的信息
答案:转自: http://www.tianyablog.com/blogger/post_show.asp?blogid=235226&postid=3242812 下面是源代码。其中使用了RZ的控件,用DELPHI自带的DBGIRD也是可以的。 function   TBaseControl.DBGird2Word(DBGrid:   TRzDBGrid):   Boolean; var    Count,I,J:Integer;    Doc:OleVariant;    CurrColumn,RecordNum:Integer;    wTable:   Variant; begin    try    Doc:=CreateOleObject( 'Word.Application ');    Doc.Visible:=True;    Doc.Documents.Add;    except    Result:=false;    Exit;    end;    Count:=DBGrid.Columns.Count-1;    CurrColumn:=0;    for   I:=0   to   Count   do    begin    if   DBGrid.Columns.Items[I].Visible   then    begin    Inc(CurrColumn);    end;    end;    RecordNum:=DBGrid.DataSource.DataSet.RecordCount+1;    wTable:=Doc.ActiveDocument.Tables.Add(Doc.ActiveDocument.Range(0,   0),RecordNum,CurrColumn);    wTable.Columns.AutoFit;    CurrColumn:=0;    for   I:=0   to   Count   do    begin    if   DBGrid.Columns.Items[I].Visible   then    begin    wTable.Cell(1,CurrColumn+1).Range.InsertAfter(DBGrid.Columns.Items[I].Title.Caption);    Inc(CurrColumn);    end;    end;    J:=1;    DBGrid.DataSource.DataSet.First;    while   not   DBGrid.DataSource.DataSet.Eof   do    begin    Inc(J);    CurrColumn:=0;    for   I:=0   to   Count   do    begin    if   DBGrid.Columns.Items[I].Visible   then    begin    wTable.Cell(J,CurrColumn+1).Range.InsertAfter(DBGrid.Columns[I].Field.DisplayText);    Inc(CurrColumn);    end;    end;    DBGrid.DataSource.DataSet.Next;    end;  end;
一个输出到模板表格中的例子 
uses SysUtils,Windows,ADODB,DB,Word97,OleServer, Classes;
var
sSQL: String;
qry: TADOQuery;
fileN:OLEvariant;
oleGrid: array[1..5] of olevariant;
oWordApp: TWordApplication;
oWordDoc: TWordDocument;
sResult: String;
i: Integer;
begin
qry:= nil;
try
sResult:= ' ';
with qry do
begin
qry:= TADOQuery.Create(nil);
qry.LockType:= ltReadOnly;
//转换日期范围
//加载合同项目数据
sSQL:= 'select ProjectName, ProjectCode from FPPro_Project where ProjectID= '+ IntToStr(iProjectID);
db_uPublic_GetRecordset(gConn, sSQL, qry);
if RecordCount= 0 then
begin //如果没有记录,退出
qry.Free;
Result:= '没有找到项目记录! ';
Exit;
end;

//连接Word
oWordApp:= TWordApplication.Create(nil);
owordDoc:= TWordDocument.Create(nil);
try
oWordApp.Connect;
except
qry.Free;
owordDoc.Free;
oWordApp.Free;
Result:= '你可能没有安装Word! ';
Exit;
end;
fileN:= fucGetExePath+ 'Template\基础资料清单.dot ';
if not FileExists(fileN) then
begin
Result:= '没有找到模板文件! ';
Exit;
end;
oWordApp.Documents.Add(fileN,emptyparam);
oWordDoc.ConnectTo(oWordApp.ActiveDocument as _document);
oWordApp.Options.CheckSpellingAsYouType:= False; //关闭拼写检查
oWordApp.Options.CheckGrammarAsYouType:= False; //关闭语法检查
oleGrid[1]:= oWordDoc.Tables.Item(1);
oleGrid[1].Cell(1,2).Range.Text:= FieldByName( 'ProjectName ').AsString;
oleGrid[1].Cell(1,4).Range.Text:= FieldByName( 'ProjectCode ').AsString;

sSQL:= 'select FileName, CreateLogon, CreateDate from (FPPro_AddonFile '
+ 'left join FPPro_RFile on FPPro_AddonFile.FileID=FPPro_RFile.FileID) '
+ 'where FPPro_AddonFile.FileType=2 and FPPro_RFile.RelationKind=1 '
+ 'and FPPro_RFile.RID= '+ IntToStr(iProjectID);
db_uPublic_GetRecordset(gConn, sSQL, qry);

oleGrid[1].Cell(6,1).Range.Text:= '共计份数: '+ IntToStr(RecordCount)+ '份 ';
if RecordCount> 0 then
begin
//添加统计项目
First;
i:=4;
while not Eof do
begin
oleGrid[1].Cell(i,1).Range.Select;
oWordApp.Selection.Rows.Add(emptyparam); //插入空行
oleGrid[1].Cell(i,1).Range.Text:= FieldByName( 'FileName ').AsString;
oleGrid[1].Cell(i,2).Range.Text:= FieldByName( 'CreateLogon ').AsString;
oleGrid[1].Cell(i,3).Range.Text:= FieldByName( 'CreateDate ').AsString;

i:=i+1;
Next;
end;
end;

oWordApp.Visible:= True;
oWordApp.WindowState:= 1;
oWordApp.Activate;
end;
qry.Free;
oWordApp.Free;
owordDoc.Free;

Result:= sResult;
except
on e: Exception do
begin
qry.Free;
if owordDoc <> nil then owordDoc.Free;
if oWordApp <> nil then oWordApp.Free;
Result:= e.Message;
end;
end;
end;
呱呱 ~~!
1.首先你可以使用ole 调用word , 
将word内容在delphi界面显示出来。
2.然后利用word本身的‘宏’命令。
选择‘录制新宏’。
3.在宏录制状态下做你想做的操作(将字段插入word中你想要的任何地方)。操作完毕后选择停止录制。
4.打开刚刚录制的‘宏’。选择编辑,把VBA命令转换为delphi代码。

上一个:跪求delphi 控制话筒输入音量的代码
下一个:英语功底不好学习delphi能行不?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,