当Dephi遇到Word
【打印文章】
在Dephi 5中提供了一组Servers组件,实现了与Office的无缝结合,但有关这一方面的资料却很少,最近笔者接触了一个用户案例,要求最终将数据库中的结果总结成一份Word文档,Word文档中对于标题、正文的字体、字号,文档的纸张大小都有一定的要求,而且还要求以表格的形式体现一部分数据库中的数据。
笔者通过查找VBA的说明,再对照Dephi的VCL,终于实现了全部功能,现将有关的内容总结如下:
1、在当前程序目录下建立以标题字段命名的Word文件
exepath:=application.ExeName;
for index:=1 to length(exepath) do
if exepath[index]='\' then
i:=index;
exepath:=copy(exepath,1,i);
doc_file:=exepath+mc+'.doc';
以标题字段“mc”命名Word文件
try
Wordapplication1.connect;
except
messagedlg('没有安装Word',mterror,[mbok],0);
abort;
end;
Wordapplication1.Caption := 'XX计划书';
Wordapplication1.visible := true;
Worddocument1.activate;
2、设置纸张大小
Wordapplication1.ActiveDocument.PageSetup.PageWidth:=XXX;
Wordapplication1.ActiveDocument.PageSetup.PageHeight:=XXX;
Wordapplication1.ActiveDocument.PageSetup.LeftMargin := XX;
//设置左边距
Wordapplication1.ActiveDocument.PageSetup.rightMargin := XX;
//设置右边距
3、插入页码
var fpage,pagea:olevariant;
fpage:=true;
pagea:=wdAlignPageNumberCenter;
Wordapplication1.activedocument.sections.item(1).Footers.item(1).PageNumbers.Add(pagea,fpage);
4、设置页面横向打印
s:=Wordapplication1.selection.start;
e:=Wordapplication1.selection.start;
aa:=wdSectionBreakNextPage;
Wordapplication1.ActiveDocument.Range(s,e).InsertBreak(aa);
Wordapplication1.Selection.Start:=Wordapplication1.Selection.Start + 1;
s:=Wordapplication1.Selection.start;
e:=Wordapplication1.ActiveDocument.Content.End_;
Wordapplication1.ActiveDocument.Range(S,e).PageSetup.Orientation:=wdOrientLandscape;
5、设置字体、字号
Wordapplication1.Selection.Font.Size:=18;
Wordapplication1.Selection.Font.Name := '黑体';
Wordapplication1.Selection.TypeParagraph;
Wordapplication1.Selection.ParagraphFormat.Alignment:= wdAlignParagraphCenter;
Wordapplication1.Selection.TypeParagraph;
Wordapplication1.Selection.TypeText(dbedit4.text);
//标题
Wordapplication1.Selection.Font.Size := 14;
Wordapplication1.Selection.Font.Name := '宋体';
Wordapplication1.Selection.TypeParagraph;
Wordapplication1.Selection.TypeParagraph;
Wordapplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify;
Wordapplication1.Selection.TypeText(' '+trim(dbmemo1.text));
//正文
... ...
6、插入表格
Wordapplication1.Selection.Font.Size :=10;
adoquery2.Active:=false;
adoquery2.active:=true;
doc:=Wordapplication1.activedocument;
counts:=adoquery2.RecordCount;
//记录数决定表格的行数
t:=doc.tables.Add(Wordapplication1.selection.range,counts+1,5);//5列
t.cell(1,1).range.text:= '单位';
t.Cell(1,1).Width:=120;
t.cell(1,1).range.Paragraphs.Alignment:= wdAlignParagraphCenter;
t.cell(1,2).range.text:= '姓名';
... ...
//依次写入其他字段的表头
for i:=2 to counts+1 do
begin
t.cell(i,1).range.text:=adoquery2.field
byname('dw').asstring;
t.Cell(i,1).Width:=120;
t.cell(i,1).range.Paragraphs.Alignment:=
wdAlignParagraphCenter;
t.cell(i,2).range.text:=adoquery2.field
byname('xm').asstring;
... ...
Adoquery2.next;
End;
使用Dephi将Word与数据库结合,实现了用户文档的自动生成,大大地方便了用户。
笔者通过查找VBA的说明,再对照Dephi的VCL,终于实现了全部功能,现将有关的内容总结如下:
1、在当前程序目录下建立以标题字段命名的Word文件
exepath:=application.ExeName;
for index:=1 to length(exepath) do
if exepath[index]='\' then
i:=index;
exepath:=copy(exepath,1,i);
doc_file:=exepath+mc+'.doc';
以标题字段“mc”命名Word文件
try
Wordapplication1.connect;
except
messagedlg('没有安装Word',mterror,[mbok],0);
abort;
end;
Wordapplication1.Caption := 'XX计划书';
Wordapplication1.visible := true;
Worddocument1.activate;
2、设置纸张大小
Wordapplication1.ActiveDocument.PageSetup.PageWidth:=XXX;
Wordapplication1.ActiveDocument.PageSetup.PageHeight:=XXX;
Wordapplication1.ActiveDocument.PageSetup.LeftMargin := XX;
//设置左边距
Wordapplication1.ActiveDocument.PageSetup.rightMargin := XX;
//设置右边距
3、插入页码
var fpage,pagea:olevariant;
fpage:=true;
pagea:=wdAlignPageNumberCenter;
Wordapplication1.activedocument.sections.item(1).Footers.item(1).PageNumbers.Add(pagea,fpage);
4、设置页面横向打印
s:=Wordapplication1.selection.start;
e:=Wordapplication1.selection.start;
aa:=wdSectionBreakNextPage;
Wordapplication1.ActiveDocument.Range(s,e).InsertBreak(aa);
Wordapplication1.Selection.Start:=Wordapplication1.Selection.Start + 1;
s:=Wordapplication1.Selection.start;
e:=Wordapplication1.ActiveDocument.Content.End_;
Wordapplication1.ActiveDocument.Range(S,e).PageSetup.Orientation:=wdOrientLandscape;
5、设置字体、字号
Wordapplication1.Selection.Font.Size:=18;
Wordapplication1.Selection.Font.Name := '黑体';
Wordapplication1.Selection.TypeParagraph;
Wordapplication1.Selection.ParagraphFormat.Alignment:= wdAlignParagraphCenter;
Wordapplication1.Selection.TypeParagraph;
Wordapplication1.Selection.TypeText(dbedit4.text);
//标题
Wordapplication1.Selection.Font.Size := 14;
Wordapplication1.Selection.Font.Name := '宋体';
Wordapplication1.Selection.TypeParagraph;
Wordapplication1.Selection.TypeParagraph;
Wordapplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphJustify;
Wordapplication1.Selection.TypeText(' '+trim(dbmemo1.text));
//正文
... ...
6、插入表格
Wordapplication1.Selection.Font.Size :=10;
adoquery2.Active:=false;
adoquery2.active:=true;
doc:=Wordapplication1.activedocument;
counts:=adoquery2.RecordCount;
//记录数决定表格的行数
t:=doc.tables.Add(Wordapplication1.selection.range,counts+1,5);//5列
t.cell(1,1).range.text:= '单位';
t.Cell(1,1).Width:=120;
t.cell(1,1).range.Paragraphs.Alignment:= wdAlignParagraphCenter;
t.cell(1,2).range.text:= '姓名';
... ...
//依次写入其他字段的表头
for i:=2 to counts+1 do
begin
t.cell(i,1).range.text:=adoquery2.field
byname('dw').asstring;
t.Cell(i,1).Width:=120;
t.cell(i,1).range.Paragraphs.Alignment:=
wdAlignParagraphCenter;
t.cell(i,2).range.text:=adoquery2.field
byname('xm').asstring;
... ...
Adoquery2.next;
End;
使用Dephi将Word与数据库结合,实现了用户文档的自动生成,大大地方便了用户。
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】