还是给你个现成的吧,我刚写的(D6+Word2000)
unit MyOffice2000;
interface
uses
DBGrids, ComObj, word2000, DB;
function DBGrid2Word(DBGrid:TDBGrid;Title:String):Boolean;
implementation
function DBGrid2Word(DBGrid:TDBGrid;Title:String):Boolean;
var
MSWord2000: OLEVariant;
ColCount,RowCount,I:Integer;
ColumnTitle,CellStr:String;
begin
Result:=False;
//打开Word2000;
try
MSWord2000 := GetActiveOleObject('Word.Application');
except
try
MSWord2000 := CreateOleObject('Word.Application');
except
Exit;
end;
end;
MSWord2000.Visible := True;
//创建空文档;
MSWord2000.Documents.Add();
//写入标题;
MSWord2000.Selection.ParagraphFormat.Alignment:=wdAlignParagraphCenter;
MSWord2000.Selection.Font.Size:=22;
MSWord2000.Selection.Font.Bold:=wdToggle;
MSWord2000.Selection.TypeText(Text:=Title);
//MSWord2000.Selection.MoveRight(Unit:=1, Count:=1);
MSWord2000.Selection.TypeParagraph;
MSWord2000.Selection.ParagraphFormat.Alignment:= wdAlignParagraphJustify;
MSWord2000.Selection.Font.Size:=11;
MSWord2000.Selection.Font.Bold:=wdToggle;
MSWord2000.Selection.TypeParagraph;
with DBGrid.DataSource.DataSet do
begin
ColCount:=FieldCount;
RowCount:=RecordCount;
//画出空表格;
MSWord2000.ActiveDocument.Tables.Add(Range:=MSWord2000.Selection.Range,
NumRows:=RowCount+1,
NumColumns:=ColCount,
DefaultTableBehavior:=0,
AutoFitBehavior:=0);
//写入表格的表头
for I:=0 to ColCount-1 do
begin
MSWord2000.Selection.Font.Bold:= wdToggle;
MSWord2000.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
ColumnTitle:=DBGrid.Columns.Title.Caption;
//Fields.FieldName;
MSWord2000.Selection.TypeText(Text:=ColumnTitle);
if I<ColCount-1 then
MSWord2000.Selection.MoveRight(Unit:=wdCell);
end;
First;
//写入表格中的数据;
while not EOF do
begin
MSWord2000.Selection.MoveDown(Unit:=wdLine,Count:=1);
MSWord2000.Selection.MoveLeft(Unit:=wdCell,Count:=ColCount-1);
for I:=0 to ColCount-1 do
begin
CellStr:=Fields.AsString;
MSWord2000.Selection.TypeText(Text:=CellStr);
if I<ColCount-1 then
MSWord2000.Selection.MoveRight(Unit:=wdCell);
end;
Next;
end;
MSWord2000.Selection.MoveRight(Unit:=wdCharacter, Count:=1);
MSWord2000.Selection.InsertRows( 1);
MSWord2000.Selection.Collapse (Direction:=wdCollapseStart);
MSWord2000.Selection.TypeText (Text:='合计');
for I:=1 to ColCount-1 do
begin
MSWord2000.Selection.MoveRight(Unit:=wdCell, Count:=1);
if Fields.DataType in [ftSmallint,ftInteger,ftWord,ftFloat,ftCurrency] then
begin
MSWord2000.Selection.InsertFormula (Formula:='=SUM(ABOVE)', NumberFormat:='');
end;
end;
end;
Result:=True;
end;
end.
记得把分数给我