如何将dbgrid中的数据插入已经存在的word文档的一个已经建立好的表中? word文档已经打开了,表是事先见好的,字段与dbgrid的字段一一对应 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你说的这个需要对WORD文档定位,恐怕有点难度吧看看这个有没有帮助如何使用 Delphi 给 word 增加表格现有一个模板文件 aaa.dot.里面有一个表格,形式如+------+-------------------------------------------|姓名:|网重|---------------------------------------------------|简历:+-------------------------------------------------|-------------------------------------------现在需要简历下面的单元格中动态插入一个表格(两列,行数由数据库中的数据决定)现在代码写了如下:..try WordApp := CreateOleObject('Word.Application'); MyDoc := CreateOleObject('Word.Document'); except Raise Exception.Create('无法打开Word,请确认已安装Word。'); end; WordApp.Visible := true; MyDoc := WordApp.Documents.Add(FileName,false); MyDoc.BookMarks.Item('简历表').Select; WordApp.Selection.Text := Edit1.Text+'的个人简历'; wTable := MyDoc.Tables.Item(1); wtable.cell(3,1).Range.Text :='个人简历'; wtablenew:=wtable.cell(3,1).tables.Add(wtable.cell (3,1).range,10,10,wdWord9TableBehavior,wdAutoFitFixed); 我没有做过,给个文章你看看吧,好像有帮助向word文档中输出表格及图形 free_card(原作) 关键字 word 工作中遇到一个软件开发项目,该项目要求将相关数据报表存为word格式文档(涉及表格、图形的输出),便于用户修改。本人在开发过程中,发现此类资料较少,且介绍相对简单,大多只是文本数据的的简单输出。现将个人摸索出的一些方法及心得体会告诉大家,以求共同探讨,共同提高! 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; 对Word模板定义标签 然后在程序中给标签写数据OKprocedure TForm1.Button4Click(Sender: TObject);var WordApp,DocApp:OleVariant; BookMark_Name: string;begin try WordApp := CreateOleObject('Word.Application'); except Exit; end; WordApp.Visible := True; DocApp:=WordApp.Documents.Open('C:\公安交通管理撤销决定书.dot'); //赋值 BookMark_Name := 'test'; //标签 DocApp.Application.Selection.goto(What := wdGoToBook, Name := BookMark_Name); DocApp.Application.Selection.Text := '这个是替换test的结果';end; 新手求助FTP限制端口问题. 请高手关注:关于delphi KEY 键值问题 listview的滚动条问题 memo中的复制和粘贴功能 如何制作.exe文件?? Treeview图标问题 Delphi中有个可以链接到网页和机器默认邮件服务器的WIN API函数是什么来着?各位帮忙! 会c/s的高手请帮忙! 做一个由server端向client端发送消息的程序 怎样让一个form紧贴着另外一个form? 50分求Lotus ScreenCam软件 怎么样能按一个按钮,使decisiongraph1的图形变化样式,如由柱形变成饼形?
如何使用 Delphi 给 word 增加表格现有一个模板文件 aaa.dot.
里面有一个表格,形式如
+------+-------------------------------------------
|姓名:|网重
|---------------------------------------------------
|简历:
+-------------------------------------------------
|-------------------------------------------
现在需要简历下面的单元格中动态插入一个表格(两列,行数由数据库中的数据决定)
现在代码写了如下:..try
WordApp := CreateOleObject('Word.Application');
MyDoc := CreateOleObject('Word.Document');
except
Raise Exception.Create('无法打开Word,请确认已安装Word。');
end; WordApp.Visible := true;
MyDoc := WordApp.Documents.Add(FileName,false); MyDoc.BookMarks.Item('简历表').Select;
WordApp.Selection.Text := Edit1.Text+'的个人简历';
wTable := MyDoc.Tables.Item(1); wtable.cell(3,1).Range.Text :='个人简历';
wtablenew:=wtable.cell(3,1).tables.Add(wtable.cell (3,1).range,10,10,wdWord9TableBehavior,wdAutoFitFixed);
free_card(原作)
关键字 word
工作中遇到一个软件开发项目,该项目要求将相关数据报表存为word格式文档(涉及表格、图形的输出),便于用户修改。本人在开发过程中,发现此类资料较少,且介绍相对简单,大多只是文本数据的的简单输出。现将个人摸索出的一些方法及心得体会告诉大家,以求共同探讨,共同提高! 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;
然后在程序中给标签写数据OK
procedure TForm1.Button4Click(Sender: TObject);
var
WordApp,DocApp:OleVariant;
BookMark_Name: string;
begin
try
WordApp := CreateOleObject('Word.Application');
except
Exit;
end;
WordApp.Visible := True;
DocApp:=WordApp.Documents.Open('C:\公安交通管理撤销决定书.dot'); //赋值
BookMark_Name := 'test'; //标签
DocApp.Application.Selection.goto(What := wdGoToBook, Name := BookMark_Name);
DocApp.Application.Selection.Text := '这个是替换test的结果';end;