您好,请问怎样将 DbGrid / DbGridEh 中的数据导入 word ? (也导为表格形式) 谢谢
解决方案 »
- 请教高手用Delphi如何把WORD格式文件中的数据到如到EXCEL中
- SOCKET ERROR 10054....错误
- 璇锋暀鍚勪綅楂樻墜: 浣跨敤mysql5.0 鏁版嵁搴撴煡璇㈢殑鍙戠幇鏁版嵁绫诲瀷鍑洪敊?
- 流媒体项目外包或者兼职,xdjm有时间的联系我。
- how convert the "String" data to the "Char" data, thanks !
- 请猛禽接分 原帖http://expert.csdn.net/Expert/topic/2368/2368797.xml?temp=.1806757
- 碰到一个怪问题(SQLServer存储过程的)
- 请问 : 一个用 AlphaControl换肤的问题?
- 请问我用什么工具能知道我的设计的程序里用了那些DLL或者其他资源?
- 谁有CHMmaker2.88(耶书制造)的中文帮助。作者的主页下载不了。
- 动态生成的窗体无法加Icon。高手来拿分(从不赊帐!)
- 我用的是WIN2000,DELPHI6.0,有的时候用DELPHI打开一个文件,DELPHI会自动关闭,这是为什么?
WordDocuments.Add(Template,NewTemplate,DocumentType,Visible) ;怎么用 ?
http://expert.csdn.net/Expert/topic/1389/1389323.xml?temp=.4101068
interfaceuses
Windows, Messages, SysUtils, Classes, Forms, DBGrids, Grids, Graphics, ComObj,
ExtCtrls, Controls, StdCtrls, ComCtrls, DB;const
wdAutoFitContent = 1;resourcestring
SPromptExport = '请等待,正在导出数据……';
SConnectWord = '正在启动Word,请稍候……';
SConnectWordError = '连接Word失败,可能没有安装Word。';
SCancel = '取消(&C)';
SError = '错误';
SConfirm = '真的要终止数据的导出吗?';
SCaption = '确认';
SGridError = '没有指定Grid,请指定Grid控件!';type
TWordAlignment = (waLeft, waCenter, waRight, waJustify, waDistribute,
waJustifyMed, waJustifyHi, waJustifyLow);
TTableAlignment = (tlLeft, tlCenter, tlRight); TTableFormatStyle = (tfNone, tfSimple1, tfSimple2, tfSimple3, tfClassic1, tfClassic2,
tfClassic3, tfClassic4, tfColorful1, tfColorful2, tfColorful3, tfColumns1,
tfColumns2, tfColumns3, tfColumns4, tfColumns5, tfGrid1, tfGrid2, tfGrid3, tfGrid4,
tfGrid5, tfGrid6, tfGrid7, tfGrid8, tfList1, tfList2, tfList3, tfList4, tfList5,
tfList6, tfList7, tfList8, tf3DEffects1, tf3DEffects2, tf3DEffects3, tfContemporary,
tfElegant, tfProfessional, tfSubtle1, tfSubtle2, tfWeb1, tfWeb2, tfWeb3, tfDefault); TTableFormatOption = (tfoBorders, tfoShading, tfoFont, tfoColor,
tfoHeadingRows, tfoLastRow, tfoFirstColumn, tfoLastColumn, tfoAutoFit);
TTableFormatOptions = set of TTableFormatOption; TSaveFormat = (sfWord, sfTemplate, sfText, sfTextLineBreak, sfDOSText,
sfDOSTextLineBreak, sfRTF, sfUnicodeText, sfHTML);type
TTableFormat = class(TPersistent)
private
FStyle: TTableFormatStyle;
FOptions: TTableFormatOptions;
FAlign: TTableAlignment;
public
constructor Create;
published
property Align: TTableAlignment read FAlign write FAlign;
property Style: TTableFormatStyle read FStyle write FStyle;
property Options: TTableFormatOptions read FOptions write FOptions;
end;type
{ Word Paragraph Format Object }
TFormats = class(TPersistent)
private
FFont: TFont;
FAlign: TWordAlignment;
FUseFont: boolean;
procedure SetFont(const Value: TFont);
published
property UseFont: boolean read FUseFont write FUseFont;
property Align: TWordAlignment read FAlign write FAlign;
property Font: TFont read FFont write SetFont;
public
constructor Create; virtual;
destructor Destroy; override;
end; { Word Table Title Object }
TTitle = class(TFormats)
private
FCaption: TCaption;
public
constructor Create; override;
published
property Caption: TCaption read FCaption write FCaption;
end; { Word Table Body Format Object }
TBody = class(TFormats);
{ Word Table Header Format Object }
THeader = class(TFormats)
public
constructor Create; override;
end; { Convert Grid To Word Table Object }
TGridToWord = class(TComponent)
private
{ Private declarations }
ProgressForm: TForm;
FShowProgress: boolean;
WordApp: Variant;
WordDoc: Variant;
WordTable: Variant;
FWordFileName: TFileName;
FTitle: TTitle;
FGrid: TCustomGrid;
FOnProgress: TNotifyEvent;
Quit: Boolean; ProgressBar: TProgressBar;
Prompt: TLabel;
FAutoExit: boolean;
FBody: TBody;
FTableFormat: TTableFormat;
FHeader: THeader;
FSaveFormat: TSaveFormat;
FAutoSize: boolean; function GetRowCount: integer;
function GetColCount: integer;
function ConnectToWord: boolean; { Connect Word Application }
procedure CreateProgressForm; { Create the Progress Form }
procedure ButtonClick(Sender: TObject); { Cancel Button Click Event Handle }
procedure SetFont(Selection: OleVariant; Font: TFont); procedure InsertTitle; { Insert Title }
procedure InsertHeader(R, C: integer; Value: string);
procedure InsertBody(R, C: integer; Value: string);
procedure ExportStringGrid; { Export String Grid to Word }
procedure ExportDBGrid; { Export DBGrid to Word }
procedure SetFormat(Selection: Variant; Value: string; Formats: TFormats);
protected
{ Protected declarations }
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure ExportToWord; { Export Grid To Word }
published
{ Published declarations }
property AutoSize: boolean read FAutoSize write FAutoSize; { Auto Size }
property AutoExit: boolean read FAutoExit write FAutoExit; { Auto close Word when done? }
property OnProgress: TNotifyEvent read FOnProgress write FOnProgress;
property Grid: TCustomGrid read FGrid write FGrid; { which Grid will be export? }
property ShowProgress: boolean read FShowProgress write FShowProgress; { Show Progress? }
property SaveFormat: TSaveFormat read FSaveFormat write FSaveFormat; { Save Format }
property WordFileName: TFileName read FWordFileName write FWordFileName; { Word File Name }
property Title: TTitle read FTitle write FTitle; { Title and Format }
property Body: TBody read FBody write FBody; { Body Format }
property Header: THeader read FHeader write FHeader; { Header Format }
property TableFormat: TTableFormat read FTableFormat write FTableFormat;
end;
type
TMyGrid = class(TCustomGrid)
published
property ColCount;
property RowCount;
end;procedure Register;
begin
RegisterComponents('Samples', [TGridToWord]);
end; { Register }{ TGridToWord }procedure TGridToWord.ButtonClick(Sender: TObject);
begin
{ Confirm for Button Cancel Click }
Quit := MessageBox(ProgressForm.Handle, pchar(SConfirm), pchar(SCaption),
MB_OKCANCEL + MB_ICONINFORMATION) = IDOK;
end; { TGridToWord.ButtonClick }function TGridToWord.ConnectToWord: boolean;
begin
Result := False;
try
WordApp := CreateOleObject('Word.Application');
WordDoc := WordApp.Documents.Add;
if Title.Caption <> '' then InsertTitle;
WordTable := WordDoc.Tables.Add(WordApp.Selection.Range, GetRowCount, GetColCount);
Result := True;
except
MessageBox(GetActiveWindow, pchar(SConnectWordError), pchar(SError), MB_OK +
MB_ICONERROR);
end;
end; { TGridToWord.ConnectToWord }constructor TGridToWord.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FShowProgress := True;
FAutoExit := False;
FAutoSize := True;
FSaveFormat := sfWord; FTitle := TTitle.Create;
FTitle.Font.Assign(TForm(Owner).Font); FBody := TBody.Create;
FBody.Font.Assign(FTitle.Font); FHeader := THeader.Create;
FHeader.Font.Assign(FTitle.Font);
FTableFormat := TTableFormat.Create;
end; { TGridToWord.Create }procedure TGridToWord.CreateProgressForm;
var
Panel : TPanel;
Button : TButton;
begin
if Assigned(ProgressForm) then exit; { Aleady Create? } ProgressForm := TForm.Create(Owner);
with ProgressForm do { Create Progress Form }
begin
Font.Name := '宋体';
Font.Size := 9;
BorderStyle := bsNone;
Width := 300;
Height := 125;
BorderWidth := 2;
Color := clBlue;
Position := poOwnerFormCenter;
end; Panel := TPanel.Create(ProgressForm);
with Panel do { Create Panel }
begin
Parent := ProgressForm;
Align := alClient;
BevelInner := bvNone;
BevelOuter := bvRaised;
Caption := '';
end; Prompt := TLabel.Create(Panel);
with Prompt do { Create Label }
begin
Parent := Panel;
Left := 20;
Top := 25;
Caption := SConnectWord;
end; ProgressBar := TProgressBar.Create(panel);
with ProgressBar do { Create ProgressBar }
begin
Step := 1;
Parent := panel;
Left := 20;
Top := 50;
Height := 18;
Width := 260;
end; Button := TButton.Create(Panel);
with Button do { Create Cancel Button }
begin
Parent := Panel;
Left := 115;
Top := 80;
Caption := SCancel;
OnClick := ButtonClick;
end; ProgressForm.Show;
ProgressForm.Update;
end; { TGridToWord.CreateProgressForm }destructor TGridToWord.Destroy;
begin
FTitle.Free;
FBody.Free;
FHeader.Free;
FTableFormat.Free;
inherited;
end; { TGridToWord.Destroy }
var
Data : TDataSet;
DBGrid : TDBGrid;
i, j : integer;
Bm : pointer;
OldBeforeScroll, OldAfterScroll: TDataSetNotifyEvent;
begin
DBGrid := TDBGrid(Grid);
Data := DBGrid.DataSource.DataSet;
with DBGrid do { Insert Table Header }
for i := 1 to Columns.Count do
if Columns[i - 1].Visible then
InsertHeader(1, i, Columns[i - 1].Title.Caption); Bm := Data.GetBook; { Save Current Position }
OldBeforeScroll := Data.BeforeScroll; { Save Old Before Scroll Event handle }
OldAfterScroll := Data.AfterScroll; { Save Old After Scroll Event Handle }
Data.DisableControls; { Disable Control }
Data.BeforeScroll := nil;
Data.AfterScroll := nil;
if ShowProgress then ProgressBar.Max := Data.RecordCount;
try
i := 2;
Data.First;
while not Data.Eof do { Process All record }
begin
with DBGrid do { Process one record }
for j := 1 to Columns.Count do
if Columns[j - 1].Visible then
InsertBody(i, j, Columns[j - 1].Field.DisplayText);
Inc(i);
Data.Next;
if Assigned(FOnProgress) then FOnProgress(Self);
if ShowProgress then { Update Progress UI }
begin
ProgressBar.StepIt;
Application.ProcessMessages;
if Quit then exit;
end;
end;
finally
Data.BeforeScroll := OldBeforeScroll; { Restore Old Event Handle }
Data.AfterScroll := OldAfterScroll;
Data.GotoBook(Bm);
Data.FreeBook(Bm);
Data.EnableControls;
end;
end; { TGridToWord.ExportDBGrid }procedure TGridToWord.ExportStringGrid;
var
i, j : integer;
SGrid : TStringGrid;
begin
SGrid := TStringGrid(Grid);
if ShowProgress then
ProgressBar.Max := SGrid.RowCount * SGrid.ColCount;
for i := 1 to SGrid.RowCount do
for j := 1 to SGrid.ColCount do
begin
if (i <= SGrid.FixedRows) or (j <= SGrid.FixedCols) then { Is Header? }
InsertHeader(i, j, SGrid.Cells[j - 1, i - 1])
else
InsertBody(i, j, SGrid.Cells[j - 1, i - 1]);
if Assigned(FOnProgress) then FOnProgress(Self);
if ShowProgress then { Update Progress UI }
begin
ProgressBar.StepIt;
Application.ProcessMessages;
if Quit then Exit;
end;
end;
end; { TGridToWord.ExportStringGrid }procedure TGridToWord.ExportToWord;
begin
if Grid = nil then raise Exception.Create(SGridError);
if ShowProgress then CreateProgressForm; { Create Progress Form }
if not ConnectToWord then { Exit when error occer }
begin
if ShowProgress then FreeAndNil(ProgressForm);
exit;
end; try
Screen.Cursor := crHourGlass;
TForm(Owner).Enabled := False;
WordApp.DisplayAlerts := False; { Disable Word Dialog }
WordApp.ScreenUpdating := False; { Disable Word Screen Update }
Quit := False;
if ShowProgress then Prompt.Caption := SPromptExport;
if Grid is TDBGrid then
ExportDBGrid
else
ExportStringGrid;
if AutoSize then
WordTable.AutoFitBehavior(wdAutoFitContent); { Auto Fit Table Size for Content }
WordTable.Rows.Alignment := TableFormat.Align;
with TableFormat do { Auto Fit Table Format }
if Style <> tfDefault then
WordTable.AutoFormat(Style, tfoBorders in Options, tfoShading in Options,
tfoFont in Options, tfoColor in Options, tfoHeadingRows in Options,
tfoLastRow in Options, tfoFirstColumn in Options, tfoLastColumn in Options,
tfoAutoFit in Options);
if WordFileName <> '' then WordDoc.SaveAs(WordFileName, SaveFormat);
finally
TForm(Owner).Enabled := True;
Screen.Cursor := crDefault;
if ShowProgress then FreeAndNil(ProgressForm); { Free Progress Form }
WordApp.DisplayAlerts := True;
WordApp.ScreenUpdating := True; if AutoExit then
WordApp.Quit
else
WordApp.Visible := True;
VarClear(WordTable);
VarClear(WordDoc);
VarClear(WordApp);
end;
end; { TGridToWord.ExportToWord }function TGridToWord.GetColCount: integer;
var
i : integer;
begin
Result := 0;
if Grid is TDBGrid then
begin
for i := 0 to TDBGrid(Grid).Columns.Count - 1 do
if TDBGrid(Grid).Columns[i].Visible then
Inc(Result);
end
else if Grid is TStringGrid then
Result := TMyGrid(Grid).ColCount;
end; { TGridToWord.GetColCount }
begin
if Grid is TDBGrid then
Result := TDBGrid(Grid).DataSource.DataSet.RecordCount + 1
else if Grid is TStringGrid then
Result := TMyGrid(Grid).RowCount
else
Result := 0;
end; { TGridToWord.GetRowCount }procedure TGridToWord.InsertBody(R, C: integer; Value: string);
begin
SetFormat(WordTable.Cell(R, C), Value, Body);
end; { TGridToWord.InsertBody }procedure TGridToWord.InsertHeader(R, C: integer; Value: string);
begin
SetFormat(WordTable.Cell(R, C), Value, Header);
end; { TGridToWord.InsertHeader }procedure TGridToWord.InsertTitle;
begin
WordApp.Selection.EndKey;
SetFormat(WordApp.Selection, Title.Caption, Title);
WordApp.Selection.EndKey;
end; { TGridToWord.InsertTitle }procedure TGridToWord.SetFont(Selection: OleVariant; Font: TFont);
begin
Selection.Font.Name := Font.Name;
Selection.Font.Color := ColorToRGB(Font.Color);
Selection.Font.Size := Font.Size;
Selection.Font.Italic := fsItalic in Font.Style;
Selection.Font.Bold := fsBold in Font.Style;
Selection.Font.Underline := fsUnderLine in Font.Style;
Selection.Font.StrikeThrough := fsStrikeOut in Font.Style;
end; { TGridToWord.SetFont }{ TParaFormat }constructor TFormats.Create;
begin
inherited Create;
FAlign := waLeft;
FUseFont := False;
FFont := TFont.Create;
end; { TParaFormat.Create }destructor TFormats.Destroy;
begin
FFont.Free;
inherited;
end; { TParaFormat.Destroy }procedure TFormats.SetFont(const Value: TFont);
begin
FFont.Assign(Value);
end; { TParaFormat.SetFont }procedure TGridToWord.SetFormat(Selection: Variant; Value: string;
Formats: TFormats);
begin
Selection.Range.InsertAfter(Value);
Selection.Range.ParagraphFormat.Alignment := Formats.Align;
if Formats.UseFont then SetFont(Selection.Range, Formats.Font);
end;{ TTitle }constructor TTitle.Create;
begin
inherited;
FAlign := waCenter;
end; { TTitle.Create }{ THeader }constructor THeader.Create;
begin
inherited;
FAlign := waCenter;
end; { THeader.Create }{ TTableFormat }constructor TTableFormat.Create;
begin
inherited Create;
FStyle := tfProfessional;
FAlign := tlCenter;
FOptions := [tfoBorders, tfoShading, tfoFont, tfoColor, tfoHeadingRows,
tfoFirstColumn, tfoAutoFit];
end; { TTableFormat.Create }end.
DBGrids, ComObj, word2000, DB;function DBGrid2Word(DBGrid:TDBGrid;Title:String):Boolean;implementationfunction 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[I].Title.Caption;
//Fields[I].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[I].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[I].DataType in [ftSmallint,ftInteger,ftWord,ftFloat,ftCurrency] then
begin
MSWord2000.Selection.InsertFormula (Formula:='=SUM(ABOVE)', NumberFormat:='');
end;
end;
end;
Result:=True;
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComObj, word2000,Grids, DBGridEh, DB, ADODB, ExtCtrls, StdCtrls,DBGridEhImpExp,
DBGrids;function DBGrid2Word(DBGrid:TDBGrid;Title:String):Boolean;implementationfunction DBGrid2Word(DBGrid:TDBGrid;Title:String):Boolean;
var
MSWord2000: OLEVariant;
ColCount,RowCount,I:Integer;
ColumnTitle,CellStr:String;
ArrOfColumn : Array of 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;
ColCount := DBGrid.Columns.Count ;
SetLength(ArrOfColumn,ColCount) ;
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
ArrOfColumn[i] := DbGrid.Columns.Items[i].FieldName ;
MSWord2000.Selection.Font.Bold:= wdToggle;
MSWord2000.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
ColumnTitle:=DBGrid.Columns[I].Title.Caption;
//Fields[I].FieldName;
MSWord2000.Selection.TypeText(Text:=ColumnTitle);
if I<ColCount-1 then
MSWord2000.Selection.MoveRight(Unit:=wdCell);
end;
First;
ii := 1 ;
//写入表格中的数据; 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:=FieldByName(ArrOfColumn[i]).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 FieldByName(ArrOfColumn[i]).DataType in [ftSmallint,ftInteger,ftWord,ftFloat,ftCurrency] then
begin
MSWord2000.Selection.InsertFormula (Formula:='=SUM(ABOVE)', NumberFormat:='');
end;
end;
end;
SetLength(ArrOfColumn,0) ;
Result:=True;
end;end.
DbGrid ---- > Word :如果表格中一行文字太多跨页,有点问题--------------------------------------------------------unit MyOffice2000;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComObj, word2000,Grids, DBGridEh, DB, ADODB, ExtCtrls, StdCtrls,DBGridEhImpExp,
DBGrids;function DBGrid2Word(DBGrid:TDBGrid;Title:String):Boolean;implementationfunction DBGrid2Word(DBGrid:TDBGrid;Title:String):Boolean;
var
MSWord2000: OLEVariant;
ColCount,RowCount,I:Integer;
ColumnTitle,CellStr:String;
ArrOfColumn : Array of 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;
ColCount := DBGrid.Columns.Count ;
SetLength(ArrOfColumn,ColCount) ;
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
ArrOfColumn[i] := DbGrid.Columns.Items[i].FieldName ;
MSWord2000.Selection.Font.Bold:= wdToggle;
MSWord2000.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
ColumnTitle:=DBGrid.Columns[I].Title.Caption;
//Fields[I].FieldName;
MSWord2000.Selection.TypeText(Text:=ColumnTitle);
if I<ColCount-1 then
MSWord2000.Selection.MoveRight(Unit:=wdCell);
end;
First;
ii := 1 ;
//写入表格中的数据; 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:=FieldByName(ArrOfColumn[i]).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 FieldByName(ArrOfColumn[i]).DataType in [ftSmallint,ftInteger,ftWord,ftFloat,ftCurrency] then
begin
MSWord2000.Selection.InsertFormula (Formula:='=SUM(ABOVE)', NumberFormat:='');
end;
end;
end;
SetLength(ArrOfColumn,0) ;
Result:=True;
end;end.
衷心感谢姐姐:) ,感谢njbudong,我会用心学习您的代码,谢谢:)**********************
网络也会如此美丽......
**********************