在Dephi
中提供了一组Servers组件
实现了与Office的无缝结合
但有关这一方面的资料却很少
最近笔者接触了一个用户案例
要求最终将数据库中的结果总结成一份Word文档
Word文档中对于标题
正文的字体
字号
文档的纸张大小都有一定的要求
而且还要求以表格的形式体现一部分数据库中的数据
笔者通过查找VBA的说明再对照Dephi的VCL终于实现了全部功能现将有关的内容总结如下
在当前程序目录下建立以标题字段命名的Word文件
exepath:=applicationExeName;
for index:= to length(exepath) do
if exepath[index]=\ then
i:=index;
exepath:=copy(exepathi);
doc_file:=exepath+mc+doc;
以标题字段mc命名Word文件
try
Wordapplicationconnect;
except
messagedlg(没有安装Wordmterror[mbok]);
abort;
end;
WordapplicationCaption := XX计划书;
Wordapplicationvisible := true;
Worddocumentactivate;
设置纸张大小
WordapplicationActiveDocumentPageSetupPageWidth:=XXX;
WordapplicationActiveDocumentPageSetupPageHeight:=XXX;
WordapplicationActiveDocumentPageSetupLeftMargin := XX;
//设置左边距
WordapplicationActiveDocumentPageSetuprightMargin := XX;
//设置右边距
插入页码
var fpagepagea:olevariant;
fpage:=true;
pagea:=wdAlignPageNumberCenter;
Wordapplicationactivedocumentsectionsitem()Footersitem()PageNumbersAdd(pageafpage);
设置页面横向打印
s:=Wordapplicationselectionstart;
e:=Wordapplicationselectionstart;
aa:=wdSectionBreakNextPage;
WordapplicationActiveDocumentRange(se)InsertBreak(aa);
WordapplicationSelectionStart:=WordapplicationSelectionStart + ;
s:=WordapplicationSelectionstart;
e:=WordapplicationActiveDocumentContentEnd_;
WordapplicationActiveDocumentRange(Se)PageSetupOrientation:=wdOrientLandscape;
设置字体字号
WordapplicationSelectionFontSize:=;
WordapplicationSelectionFontName := 黑体;
WordapplicationSelectionTypeParagraph;
WordapplicationSelectionParagraphFormatAlignment:= wdAlignParagraphCenter;
WordapplicationSelectionTypeParagraph;
WordapplicationSelectionTypeText(dbedittext);
//标题
WordapplicationSelectionFontSize := ;
WordapplicationSelectionFontName := 宋体;
WordapplicationSelectionTypeParagraph;
WordapplicationSelectionTypeParagraph;
WordapplicationSelectionParagraphFormatAlignment := wdAlignParagraphJustify;
WordapplicationSelectionTypeText( +trim(dbmemotext));
//正文
插入表格
WordapplicationSelectionFontSize :=;
adoqueryActive:=false;
adoqueryactive:=true;
doc:=Wordapplicationactivedocument;
counts:=adoqueryRecordCount;
//记录数决定表格的行数
t:=doctablesAdd(Wordapplicationselectionrangecounts+);//列
tcell()rangetext:= 单位;
tCell()Width:=;
tcell()rangeParagraphsAlignment:= wdAlignParagraphCenter;
tcell()rangetext:= 姓名;
//依次写入其他字段的表头
for i:= to counts+ do
begin
tcell(i)rangetext:=adoqueryfield
byname(dw)asstring;
tCell(i)Width:=;
tcell(i)rangeParagraphsAlignment:=
wdAlignParagraphCenter;
tcell(i)rangetext:=adoqueryfield
byname(xm)asstring;
Adoquerynext;
End;
使用Dephi将Word与数据库结合实现了用户文档的自动生成大大地方便了用户