分无所谓!
这个过程不是我写的,我也不太懂。希望对你有帮助~
procedure TFrmBillRep.ExportWordRep;
var
Template,NewTemplate,DocumentType,visiable,DelFlagCount :OleVariant;
bks :Bookmarks; //书签名
lsFieldName, lsBkName, lsBkPre, lsValue, lsTmp, lsFlag:String;
What,Which,Count,Name,Ext,Unt,Num, bkIndex:OleVariant;
i, liRec,liPos,liTableIndex,liCStart,liCStep, liCircleCount, liDelFlagCount:integer; //liCStart:循环起始记录,liCStep:循环步长
begin
inherited;
Template := GetExePath+'repTemplate/'+ArrRepTemplate[gEntrustClass]+'.Doc';
NewTemplate := false;
dm.Word.Documents.Add(Template,NewTemplate,DocumentType,visiable);
dm.WordDoc.ConnectTo(dm.Word.ActiveDocument);
bks :=BookMarks(dm.WordDoc.Bookmarks);
bks.DefaultSorting := wdSortByName;
liDelFlagCount := 0;
for i:= 1 to bks.Count do
begin
bkIndex := i;
//listBox1.items.add(bks.item(bkIndex).Name);
lsBkName := bks.Item(bkIndex).Name;
lsBkPre := UpperCase(Copy(lsBkName,1,2));
liTableIndex := StrToInt(Copy(lsBkName,3,1));
what :=wdGoToBookmark;
count := 0;
name :=lsBkName;
liPos := Pos('___',lsBkName); //如果有扩展书签,则去除扩展符
if liPos >0 then
lsBkName := Copy(lsBkName,1,liPos-1); //name :=lsBkName放此句之前
if (lsBkPre='A_') or (lsBkPre='B_') then //单值书签
begin
liPos := Pos('__',lsBkName); //'__'位置,存在"__D"表示如果当前值为空则删除该行。
if liPos >0 then
begin
lsFlag := Copy(lsBkName,liPos+2,1);
lsFieldName := Copy(lsBkName,5,liPos-5);
end else
begin
lsFlag := '';
lsFieldName := Copy(lsBkName,5,length(lsBkName)-4);
end;
//lsFieldName := Copy(lsBkName,5,length(lsBkName)-4);
lsValue := trim(ArrDataSetRep[liTableIndex].FieldByName(lsFieldName).asString);
dm.Word.Selection.GoTo_(what,Which,Count,Name);
Num:=length(lsValue);
//Word.Selection.MoveRight(Unt,Num,Ext);
Unt:=wdCharacter;
if lsBkPre = 'A_' then
dm.Word.Selection.Delete(Unt,Num);
dm.Word.Selection.TypeText(lsValue);
if lsFlag ='D' then //"__D"表示如果为空则删除此行并在后面加入一空行
if lsValue='' then
begin
dm.Word.Selection.Rows.Delete;
inc(liDelFlagCount);
end;
end else if lsBkPre='C_' then //表格向下循环书签
begin
liPos := Pos('__',lsBkName); //'__'位置
lsFieldName := Copy(lsBkName,5,liPos-5);
lsTmp := Copy(lsBkName,liPos+2,Length(lsBkName)-liPos-1);
liPos := Pos('_',lsTmp);
liCStart := StrToInt(Copy(lsTmp,1,liPos-1)); //循环开始
liCStep := StrToInt(Copy(lsTmp,liPos+1,Length(lsTmp)-liPos)); //循环次数
dm.Word.Selection.GoTo_(what,Which,Count,Name);
with ArrDataSetRep[liTableIndex] do
begin
disableControls;
if RecNo<>liCStart then //定位记录
Begin
First;
for liRec:=1 to liCStart-1 do next;
end;
liCircleCount := 0;//循环记数
while not eof do
begin
lsValue := trim(FieldByName(lsFieldName).asString);
dm.Word.Selection.TypeText(lsValue);
inc(liCircleCount);
if (liCStep>0) and (liCircleCount>=liCStep) then Break; //如果达到循环步长,则中止循环
Unt:=wdLine;
Count :=1;
Ext:=wdMove;
dm.Word.Selection.MoveDown(Unt,Count,Ext);
next;
end;
EnableControls;
end;
end else if lsBkPre='X_' then //最后插入空行
begin //test
dm.Word.Selection.GoTo_(what,Which,Count,Name);
//dm.Word.Selection.Rows.Delete;
DelFlagCount := liDelFlagCount;
dm.Word.Selection.InsertRows(DelFlagCount);
end;
end;
dm.word.Visible :=true;
end;