procedure TDiaryFunc.PrintDiaryList(Grd: TDBGridEh;slTitle: TStringList); var Count,i,j,iRangeEnd:integer; CurrColumn,RecordNum:integer; Doc: OleVariant; wTable,wRange: Variant; Curpos: TBook; begin if not Grd.DataSource.DataSet.Active then begin Exit; end; Curpos := Grd.DataSource.DataSet.GetBook; try Doc:=CreateOleObject('Word.Application'); Doc.Visible := False; Doc.Documents.Add; except Exit end; Count:=Grd.Columns.Count - 1; CurrColumn:=0; for i:=0 to Count do begin if Grd.Columns.Items[i].Visible then Inc(CurrColumn); end; RecordNum:=Grd.DataSource.DataSet.RecordCount+1; try Doc.Caption := '控制Word实例'; Doc.ActiveDocument.PageSetup.Orientation := 1; //1表示横向排列 iRangeEnd := Doc.ActiveDocument.Range.End - 1 ; Doc.ActiveDocument.Range(iRangeEnd,iRangeEnd).Select; Doc.Selection.ParagraphFormat.Reset; Doc.selection.Font.Name := '隶书'; Doc.Selection.Font.Size := 20; Doc.ActiveDocument.Range.InsertAfter(slTitle[0]); Doc.Selection.ParagraphFormat.Alignment := 1; //1表示居中 Doc.ActiveDocument.Range.InsertAfter(#13); //设置换行后的字体 iRangeEnd := Doc.ActiveDocument.Range.End - 1 ; Doc.ActiveDocument.Range(iRangeEnd,iRangeEnd).Select; Doc.Selection.ParagraphFormat.Reset; Doc.selection.Font.Name := '宋体'; Doc.Selection.Font.Size := 10; Doc.ActiveDocument.Range.InsertAfter(slTitle[1] + #13); Doc.Selection.ParagraphFormat.Alignment := 1; iRangeEnd := Doc.ActiveDocument.Range.End - 1 ; Doc.ActiveDocument.Range(iRangeEnd,iRangeEnd).Select; Doc.ActiveDocument.Range.InsertAfter(slTitle[2]); Doc.Selection.ParagraphFormat.Alignment := 1;
Doc.ActiveDocument.Range.InsertAfter(#13); iRangeEnd := Doc.ActiveDocument.Range.End - 1 ; //在文档末尾插入表格 wRange := Doc.ActiveDocument.Range(iRangeEnd,iRangeEnd); wTable:=Doc.ActiveDocument.Tables.Add(wRange, RecordNum, CurrColumn); wTable.Columns.AutoFit; CurrColumn:=0; for i:=0 to Count do begin if Grd.Columns.Items[i].Visible then begin wTable.Cell(1,CurrColumn+1).Range.InsertAfter(Grd.Columns.Items[i].Title.Caption); wTable.Cell(1,CurrColumn+1).Range.Select; wTable.Cell(1,CurrColumn+1).Range.Font.Bold:=true; Inc(CurrColumn); end; end; j := 1; Grd.DataSource.DataSet.First; while not Grd.DataSource.DataSet.Eof do begin Inc(J); CurrColumn := 0; for i:=0 to Count do begin if Grd.Columns.Items[i].Visible then begin if Grd.Columns[i].Field.DisplayText = 'False' then wTable.Cell(j,CurrColumn+1).Range.InsertAfter('否') else if Grd.Columns[i].Field.DisplayText = 'True' then wTable.Cell(j,CurrColumn+1).Range.InsertAfter('是') else wTable.Cell(j,CurrColumn+1).Range.InsertAfter( Grd.Columns[i].Field.DisplayText); Inc(CurrColumn); end; end; Grd.DataSource.DataSet.Next; end; Doc.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader; Doc.Selection.TypeText(Text := '控制Word实例'); Doc.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageFooter; Doc.Selection.TypeText(Text := ‘控制Word实例'); Doc.ActiveWindow.ActivePane.View.SeekView:=wdSeekMainDocument; finally Grd.DataSource.DataSet.GotoBook(Curpos); Doc.Visible := True; end; end;试试我的函数,里面有移动光标的代码。 是移动到文档的末尾,还有设置页眉和页脚的代码。
MsWord:=CreateOleObject('Word.Application');
具体的你可以再查一下相关的资料,网上很多的
输出完毕时,你可以选择重新打开
我有见过人家做得很好,输出数据到WORD过程中不影响用户操作WORD,不会因为把光标位置改变而把数据输出到当前光标之后!
var
Count,i,j,iRangeEnd:integer;
CurrColumn,RecordNum:integer;
Doc: OleVariant;
wTable,wRange: Variant;
Curpos: TBook;
begin
if not Grd.DataSource.DataSet.Active then
begin
Exit;
end;
Curpos := Grd.DataSource.DataSet.GetBook;
try
Doc:=CreateOleObject('Word.Application');
Doc.Visible := False;
Doc.Documents.Add;
except
Exit
end;
Count:=Grd.Columns.Count - 1;
CurrColumn:=0;
for i:=0 to Count do
begin
if Grd.Columns.Items[i].Visible then
Inc(CurrColumn);
end;
RecordNum:=Grd.DataSource.DataSet.RecordCount+1; try
Doc.Caption := '控制Word实例';
Doc.ActiveDocument.PageSetup.Orientation := 1; //1表示横向排列
iRangeEnd := Doc.ActiveDocument.Range.End - 1 ;
Doc.ActiveDocument.Range(iRangeEnd,iRangeEnd).Select;
Doc.Selection.ParagraphFormat.Reset;
Doc.selection.Font.Name := '隶书';
Doc.Selection.Font.Size := 20;
Doc.ActiveDocument.Range.InsertAfter(slTitle[0]);
Doc.Selection.ParagraphFormat.Alignment := 1; //1表示居中 Doc.ActiveDocument.Range.InsertAfter(#13);
//设置换行后的字体
iRangeEnd := Doc.ActiveDocument.Range.End - 1 ;
Doc.ActiveDocument.Range(iRangeEnd,iRangeEnd).Select;
Doc.Selection.ParagraphFormat.Reset;
Doc.selection.Font.Name := '宋体';
Doc.Selection.Font.Size := 10;
Doc.ActiveDocument.Range.InsertAfter(slTitle[1] + #13);
Doc.Selection.ParagraphFormat.Alignment := 1;
iRangeEnd := Doc.ActiveDocument.Range.End - 1 ;
Doc.ActiveDocument.Range(iRangeEnd,iRangeEnd).Select;
Doc.ActiveDocument.Range.InsertAfter(slTitle[2]);
Doc.Selection.ParagraphFormat.Alignment := 1;
Doc.ActiveDocument.Range.InsertAfter(#13);
iRangeEnd := Doc.ActiveDocument.Range.End - 1 ; //在文档末尾插入表格
wRange := Doc.ActiveDocument.Range(iRangeEnd,iRangeEnd);
wTable:=Doc.ActiveDocument.Tables.Add(wRange, RecordNum, CurrColumn);
wTable.Columns.AutoFit;
CurrColumn:=0;
for i:=0 to Count do
begin
if Grd.Columns.Items[i].Visible then
begin
wTable.Cell(1,CurrColumn+1).Range.InsertAfter(Grd.Columns.Items[i].Title.Caption);
wTable.Cell(1,CurrColumn+1).Range.Select;
wTable.Cell(1,CurrColumn+1).Range.Font.Bold:=true;
Inc(CurrColumn);
end;
end;
j := 1;
Grd.DataSource.DataSet.First;
while not Grd.DataSource.DataSet.Eof do
begin
Inc(J);
CurrColumn := 0;
for i:=0 to Count do
begin
if Grd.Columns.Items[i].Visible then
begin
if Grd.Columns[i].Field.DisplayText = 'False' then
wTable.Cell(j,CurrColumn+1).Range.InsertAfter('否')
else if Grd.Columns[i].Field.DisplayText = 'True' then
wTable.Cell(j,CurrColumn+1).Range.InsertAfter('是')
else
wTable.Cell(j,CurrColumn+1).Range.InsertAfter(
Grd.Columns[i].Field.DisplayText);
Inc(CurrColumn);
end;
end;
Grd.DataSource.DataSet.Next;
end;
Doc.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader;
Doc.Selection.TypeText(Text := '控制Word实例');
Doc.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageFooter;
Doc.Selection.TypeText(Text := ‘控制Word实例');
Doc.ActiveWindow.ActivePane.View.SeekView:=wdSeekMainDocument;
finally
Grd.DataSource.DataSet.GotoBook(Curpos);
Doc.Visible := True;
end;
end;试试我的函数,里面有移动光标的代码。
是移动到文档的末尾,还有设置页眉和页脚的代码。
避免使用ActiveDocument,Selection这样的全局对象,而使用Documents(1),Range这样不会因用户操作而改变的对象