在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與數據庫結合實現了用戶文檔的自動生成大大地方便了用戶
From:http://tw.wingwit.com/Article/program/Delphi/201311/24963.html