我写的一个函数,看看吧
function DBGrid2Word(DBGrid:TDBGrid;Title:String;WithTotal:Boolean=False):Boolean;
var
MSWord2000: OLEVariant;
ColCount,RowCount,DBGridWidth,I:Integer;
ColumnTitle,CellStr:String;//临时变量,用于取得列的标题和单元格中的字串;
CalculateTotal:Boolean;//是否进行了“合计”计算信号量;
begin
Result:=False;
ColCount:=DBGrid.FieldCount;
//计算表格宽度;
DBGridWidth:=0;
for I:=0 to ColCount-1 do
begin
DBGridWidth:=DBGridWidth+DBGrid.Columns.Width;
end;
//打开Word2000;
try
MSWord2000 := GetActiveOleObject('Word.Application');
except
try
MSWord2000 := CreateOleObject('Word.Application');
except
Exit;
end;
end;
MSWord2000.Visible := True;
MSWord2000.Caption := Title;
//创建空文档;
MSWord2000.Documents.Add();
if DBGridWidth>400 then
begin
//将页面改为“横向”;
MSWord2000.ActiveDocument.PageSetup.Orientation := wdOrientLandscape;
end;
//写入标题;
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;
if WithTotal then
begin
CalculateTotal:=False;
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,ftBCD] then
begin
CalculateTotal:=True;
MSWord2000.Selection.InsertFormula (Formula:='=SUM(ABOVE)', NumberFormat:='');
end;
end;
if not CalculateTotal then
begin
MSWord2000.Selection.MoveLeft (Unit:=wdCharacter, Count:=ColCount-1, Extend:=wdExtend);
MSWord2000.Selection.Cells.Merge;
MSWord2000.Selection.TypeText (Text:='以上共'+inttostr(RowCount)+'笔数据')
end;
end;
end;
Result:=True;
end;