這是我做項(xiàng)目過(guò)程中自己做的幾個(gè)函數(shù),見(jiàn)到大家都在問(wèn)Word的問(wèn)題。現(xiàn)在拿出來(lái)和大家共享。(希望有朋友可以進(jìn)一步添加新的功能,或者做成包或者lib等,更方便大家使用。我自己是沒(méi)有時(shí)間啦,呵呵)
使用前,先根據(jù)需要建立一個(gè)空的WORD文件作為
模板,在模板文件中設(shè)置好各種格式和文本。另外,其中的
PRnWordTable的參數(shù)是TDBGridEh類(lèi)型的控件,取自Ehlib2.6
其中用到的shFileCopy函數(shù)(用于復(fù)制文件)和guiInfo函數(shù)(用于顯示消息框)也是自己編寫(xiě)的,代碼也附后。
示范代碼如下:
代碼完成的功能:
1. 替換打印模板中的“#TITLE#”文本為“示范代碼1”
2. 并且將DBGridEh1控件當(dāng)前顯示的內(nèi)容插入到文檔的末尾
3. 在文檔末尾插入一個(gè)空行
4. 在文檔末尾插入新的一行文本
5. 將文檔中的空行去掉
if PrnWordBegin('C:/打印模板.DOC','C:/目標(biāo)文件1.DOC') then
begin
PrnWordReplace('#TITLE#','示范代碼1');
PrnWordTable(DBGridEh1);
PrnWordInsert('');
PrnWordInsert('這是新的一行文本');
PrnWordReplace('^p^p','^p',true);
PrnWordSave;
end;
源代碼如下:
//Word打印(聲明部分)
wDoc,wApp:Variant;
function PrnWordBegin(tempDoc,docName:String):boolean;
function PrnWordReplace(docText,newText:String;bSimpleReplace:boolean=false):boolean;
function PrnWordInsert(lineText:String;bNewLine:boolean=true):boolean;overload;
function PrnWordInsert(var imgInsert:TImage;sBookMark:String=''):boolean;overload;
function PrnWordInsert(var chartInsert:TChart;sBookMark:String=''):boolean;overload;
function PrnWordTable(var dbG:TDBGridEh;sBookMark:String=''):boolean;
procedure PrnWordSave;
procedure PrnWordEnd;
//Word打印(實(shí)現(xiàn)部分)
{
功能:基于模板文件tempDoc新建目標(biāo)文件docName并打開(kāi)文件
}
function PrnWordBegin(tempDoc,docName:String):boolean;
begin
result:=false;
//復(fù)制模版
if tempDoc<>'' then
if not shFileCopy(tempDoc,docName) then exit;
//連接Word
try
except
guiInfo('請(qǐng)先安裝 Microsoft Word 。');
exit;
end;
try
//打開(kāi)
if tempDoc='' then
begin
//創(chuàng)建新文檔
wDoc:=wApp.Document.Add;
wDoc.SaveAs(docName);
end else begin
//打開(kāi)模版
wDoc:=wApp.Documents.Open(docName);
end;
except
guiInfo('打開(kāi)模版失敗,請(qǐng)檢查模版是否正確。');
wApp.Quit;
exit;
end;
wApp.Visible:=true;
result:=true;
end;
{
功能:使用newText替換docText內(nèi)容
bSimpleReplace:true時(shí)僅做簡(jiǎn)單的替換,false時(shí)對(duì)新文本進(jìn)行換行處理
}
function PrnWordReplace(docText,newText:String;bSimpleReplace:boolean=false):boolean;
var i:Integer;
begin
if bSimpleReplace then
begin
//簡(jiǎn)單處理,直接執(zhí)行替換操作
try
wApp.Selection.Find.ClearFormatting;
wApp.Selection.Find.Replacement.ClearFormatting;
wApp.Selection.Find.Text := docText;
wApp.Selection.Find.Replacement.Text :=newText;
wApp.Selection.Find.Forward := True;
wApp.Selection.Find.Wrap := wdFindContinue;
wApp.Selection.Find.Format := False;
wApp.Selection.Find.MatchCase := False;
wApp.Selection.Find.MatchWholeWord := true;
wApp.Selection.Find.MatchByte := True;
wApp.Selection.Find.MatchWildcards := False;
wApp.Selection.Find.MatchSoundsLike := False;
wApp.Selection.Find.MatchAllWordForms := False;
wApp.Selection.Find.Execute(Replace:=wdReplaceAll);
result:=true;
except
result:=false;
end;
exit;
end;
//自動(dòng)分行
reWord.Lines.Clear;
reWord.Lines.Add(newText);
try
//定位到要替換的位置的后面
wApp.Selection.Find.ClearFormatting;
wApp.Selection.Find.Text := docText;
wApp.Selection.Find.Replacement.Text := '';
wApp.Selection.Find.Forward := True;
wApp.Selection.Find.Wrap := wdFindContinue;
wApp.Selection.Find.Format := False;
wApp.Selection.Find.MatchCase := False;
wApp.Selection.Find.MatchWholeWord := False;
wApp.Selection.Find.MatchByte := True;
wApp.Selection.Find.MatchWildcards := False;
wApp.Selection.Find.MatchSoundsLike := False;
wApp.Selection.Find.MatchAllWordForms := False;
wApp.Selection.Find.Execute;
wApp.Selection.MoveRight(wdCharacter,1);
//開(kāi)始逐行插入
for i:=0 to reWord.Lines.Count-1 Do
begin
//插入當(dāng)前行
wApp.Selection.InsertAfter(reWord.Lines[i]);
//除最后一行外,自動(dòng)加入新行
if i<reWord.Lines.Count-1 then
wApp.Selection.InsertAfter(#13);
end;
//刪除替換位標(biāo)
wApp.Selection.Find.ClearFormatting;
wApp.Selection.Find.Replacement.ClearFormatting;
wApp.Selection.Find.Text := docText;
wApp.Selection.Find.Replacement.Text := '';
wApp.Selection.Find.Forward := True;
wApp.Selection.Find.Wrap := wdFindContinue;
wApp.Selection.Find.Format := False;
wApp.Selection.Find.MatchCase := False;
wApp.Selection.Find.MatchWholeWord := true;
wApp.Selection.Find.MatchByte := True;
wApp.Selection.Find.MatchWildcards := False;
wApp.Selection.Find.MatchSoundsLike := False;
wApp.Selection.Find.MatchAllWordForms := False;
wApp.Selection.Find.Execute(Replace:=wdReplaceAll);
result:=true;
except
result:=false;
end;
end;
{
功能:打印TDBGridEh當(dāng)前顯示的內(nèi)容
基于TDBGridEh控件的格式和內(nèi)容,自動(dòng)在文檔中的sBookMark書(shū)簽處生成Word表格
目前能夠支持單元格對(duì)齊、多行標(biāo)題(兩行)、底部合計(jì)等特性
sBookMark:Word中要插入表格的書(shū)簽名稱(chēng)
}
function PrnWordTable(var dbG:TDBGridEh;sBookMark:String=''):boolean;
var iCol,iLine,i,j,k:Integer;
wTable,wRange:Variant;
iRangeEnd:longint;
iGridLine,iTitleLine:Integer;
getTextText:String;getTextDisplay:boolean;
titleList:TStringList;titleSplit,titleCol:Integer;lastTitleSplit,SubTitle:Integer;lastTitle:String;
begin
result:=false;
try
//計(jì)算表格的列數(shù)(不包括隱藏的列)
iTitleLine:=1; //始終默認(rèn)為1
iCol:=0;
for i:=0 to dbG.Columns.Count-1 Do
begin
if dbG.Columns[i].Visible then
begin
iCol:=iCol+1;
end;
end;
//計(jì)算表格的行數(shù)(不包括隱藏的列)
if dbG.DataSource.DataSet.Active then
iLine:=dbG.DataSource.DataSet.RecordCount
else
iLine:=0;
iGridLine:=iLine+iTitleLine+dbG.FooterRowCount;
//定位插入點(diǎn)
if sBookMark='' then
begin
//在文檔末尾
iRangeEnd:=wDoc.Range.End-1;
if iRangeEnd<0 then iRangeEnd:=0;
wRange:=wDoc.Range(iRangeEnd,iRangeEnd);
end else begin
//在書(shū)簽處
wRange:=wDoc.Range.Goto(wdGoToBookmark,,,sBookMark);
end;
wTable:=wDoc.Tables.Add(wRange,iGridLine,iCol);
wTable.Columns.AutoFit;
//標(biāo)題行
k:=1;
for j:=1 to dbG.Columns.Count Do
begin
if dbG.Columns[j-1].Visible then
begin
if dbG.UseMultiTitle then
begin
titleList:=strSplit(dbG.Columns[j-1].Title.Caption,'|');
wTable.Cell(1,k).Range.InsertAfter(titleList.Strings[0]);
end else
wTable.Cell(1,k).Range.InsertAfter(dbG.Columns[j-1].Title.Caption);
//設(shè)置單元格對(duì)齊方式
if dbG.Columns[j-1].Title.Alignment=taCenter then
wTable.Cell(1,k).Range.ParagraphFormat.Alignment:=wdAlignParagraphCenter
else if dbG.Columns[j-1].Title.Alignment=taRightJustify then
wTable.Cell(1,k).Range.ParagraphFormat.Alignment:=wdAlignParagraphRight
else if dbG.Columns[j-1].Title.Alignment=taLeftJustify then
wTable.Cell(1,k).Range.ParagraphFormat.Alignment:=wdAlignParagraphJustify;
k:=k+1;
end;
end;
//填寫(xiě)每一行
if iLine>0 then
begin
dbG.DataSource.dataset.DisableControls;
dbG.DataSource.DataSet.First;
for i:=1 to iLine Do
begin
k:=1;
for j:=1 to dbG.Columns.Count Do
begin
if dbG.Columns[j-1].Visible then
begin
if dbG.Columns[j-1].FieldName<>'' then //避免由于空列而出錯(cuò)
begin
//如果該列有自己的格式化顯示函數(shù),則調(diào)用顯示函數(shù)獲取顯示串
getTextText:='';
if Assigned(dbG.DataSource.DataSet.FieldByName(dbG.Columns[j-1].FieldName).OnGetText) then
begin
dbG.DataSource.DataSet.FieldByName(dbG.Columns[j-1].FieldName).OnGetText(dbG.DataSource.DataSet.FieldByName(dbG.Columns[j-1].FieldName),getTextText,getTextDisplay);
wTable.Cell(i+iTitleLine,k).Range.InsertAfter(getTextText);
end else begin
wTable.Cell(i+iTitleLine,k).Range.InsertAfter(dbG.DataSource.DataSet.FieldByName(dbG.Columns[j-1].FieldName).AsString);
end;
end;