Dbgrid导出为word

unit DbgridToWord; interface uses  SysUtils,ComObj,DBGrids; implementation Function DBGirdToWord(DBGrid: TDBGrid): Boolean;var  Count,I,J:Integer;  Doc:OleVariant;  CurrColumn,RecordNum:Integer;  wTable: Variant;begin  try    Doc:=CreateOleObject('Word.Application');    Doc.Visible:=True;    Doc.Documents.Add;  except    Result:=false;    Exit;  end;  Count:=DBGrid.Columns.Count-1;      CurrColumn:=0;  for I:=0 to Count do  begin    if DBGrid.Columns.Items[I].Visible then     //略过visible=false的column    begin      Inc(CurrColumn);    end;  end;  RecordNum:=DBGrid.DataSource.DataSet.RecordCount+1;  wTable:=Doc.ActiveDocument.Tables.Add(Doc.ActiveDocument.Range(0, 0),RecordNum,CurrColumn);  wTable.Columns.AutoFit;  CurrColumn:=0;  for I:=0 to Count do  begin    if DBGrid.Columns.Items[I].Visible then    begin      wTable.Cell(1,CurrColumn+1).Range.InsertAfter(DBGrid.Columns.Items[I].Title.Caption);      Inc(CurrColumn);    end;  end;  J:=1;  DBGrid.DataSource.DataSet.First;  while not DBGrid.DataSource.DataSet.Eof do  begin    Inc(J);    CurrColumn:=0;    for I:=0 to Count do    begin      if DBGrid.Columns.Items[I].Visible then      begin        wTable.Cell(J,CurrColumn+1).Range.InsertAfter(DBGrid.Columns[I].Field.DisplayText);        Inc(CurrColumn);      end;    end;    DBGrid.DataSource.DataSet.Next;  end;end; end.

你可能感兴趣的:(word)