這是我做項目過程中自己做的幾個函數,見到大家都在問Word的問題。現在拿出來和大家共享。(希望有朋友可以進一步添加新的功能,或者做成包或者lib等,更方便大家使用。我自己是沒有時間啦,呵呵)
使用前,先根據需要建立一個空的WORD文件作為
模板,在模板文件中設置好各種格式和文本。另外,其中的
PRnWordTable的參數是TDBGridEh類型的控件,取自Ehlib2.6
其中用到的shFileCopy函數(用于復制文件)和guiInfo函數(用于顯示消息框)也是自己編寫的,代碼也附后。
示范代碼如下:
代碼完成的功能:
1. 替換打印模板中的“#TITLE#”文本為“示范代碼1”
2. 并且將DBGridEh1控件當前顯示的內容插入到文檔的末尾
3. 在文檔末尾插入一個空行
4. 在文檔末尾插入新的一行文本
5. 將文檔中的空行去掉
if PrnWordBegin('C:/打印模板.DOC','C:/目標文件1.DOC') then
begin
PrnWordReplace('#TITLE#','示范代碼1');
PrnWordTable(DBGridEh1);
PrnWordInsert('');
PrnWordInsert('這是新的一行文本');
PrnWordReplace('^p^p','^p',true);
PrnWordSave;
end;
源代碼如下:
//Word打?。暶鞑糠郑?/B>
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打?。▽崿F部分)
{
功能:基于模板文件tempDoc新建目標文件docName并打開文件
}
function PrnWordBegin(tempDoc,docName:String):boolean;
begin
result:=false;
//復制模版
if tempDoc<>'' then
if not shFileCopy(tempDoc,docName) then exit;
//連接Word
try
except
guiInfo('請先安裝 Microsoft Word 。');
exit;
end;
try
//打開
if tempDoc='' then
begin
//創建新文檔
wDoc:=wApp.Document.Add;
wDoc.SaveAs(docName);
end else begin
//打開模版
wDoc:=wApp.Documents.Open(docName);
end;
except
guiInfo('打開模版失敗,請檢查模版是否正確。');
wApp.Quit;
exit;
end;
wApp.Visible:=true;
result:=true;
end;
{
功能:使用newText替換docText內容
bSimpleReplace:true時僅做簡單的替換,false時對新文本進行換行處理
}
function PrnWordReplace(docText,newText:String;bSimpleReplace:boolean=false):boolean;
var i:Integer;
begin
if bSimpleReplace then
begin
//簡單處理,直接執行替換操作
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;
//自動分行
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);
//開始逐行插入
for i:=0 to reWord.Lines.Count-1 Do
begin
//插入當前行
wApp.Selection.InsertAfter(reWord.Lines[i]);
//除最后一行外,自動加入新行
if i<reWord.Lines.Count-1 then
wApp.Selection.InsertAfter(#13);
end;
//刪除替換位標
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當前顯示的內容
基于TDBGridEh控件的格式和內容,自動在文檔中的sBookMark書簽處生成Word表格
目前能夠支持單元格對齊、多行標題(兩行)、底部合計等特性
sBookMark:Word中要插入表格的書簽名稱
}
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
//計算表格的列數(不包括隱藏的列)
iTitleLine:=1; //始終默認為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;
//計算表格的行數(不包括隱藏的列)
if dbG.DataSource.DataSet.Active then
iLine:=dbG.DataSource.DataSet.RecordCount
else
iLine:=0;
iGridLine:=iLine+iTitleLine+dbG.FooterRowCount;
//定位插入點
if sBookMark='' then
begin
//在文檔末尾
iRangeEnd:=wDoc.Range.End-1;
if iRangeEnd<0 then iRangeEnd:=0;
wRange:=wDoc.Range(iRangeEnd,iRangeEnd);
end else begin
//在書簽處
wRange:=wDoc.Range.Goto(wdGoToBookmark,,,sBookMark);
end;
wTable:=wDoc.Tables.Add(wRange,iGridLine,iCol);
wTable.Columns.AutoFit;
//標題行
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);
//設置單元格對齊方式
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;
//填寫每一行
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 //避免由于空列而出錯
begin
//如果該列有自己的格式化顯示函數,則調用顯示函數獲取顯示串
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;