下面是我写的一个比较完整的输出到WORD的过程,你可能需要稍微改一下以适合你的需要:
procedure DBGridExportToWord(tempGrid: TObject);
var
WordApplication: TWordApplication;
WordDocument: TWordDocument;
WordFont: TWordFont;
WordParagraphFormat: TWordParagraphFormat;
Template,NewTemplate,ItemIndex:OleVariant;
v1,v2,v3: OleVariant;
coli,colii,rowi,i,j:integer;
ClientDataSet:TClientDataSet;
tReportTitle: string;
tPageWidth,tPageHeight,tTopMargin,tBottomMargin,tLeftMargin,tRightMargin: Double;
tXHLen: Double;
tFX: integer;
procedure setfont;
begin
// WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font);
// if ChkBoxUnderline.checked then WordFont.Underline := 2;
// if ChkBoxBold.checked then WordFont.Bold := 1;
// if ChkBoxItalic.Checked then WordFont.Italic := 1;
// if ChkBoxEmboss.Checked then WordFont.Emboss := 1;
// if ChkBoxEngrave.checked then WordFont.Engrave := 1;
// if ChkBoxShadow.checked then WordFont.shadow := 1;
// if ChkBoxDoublestrike.checked then WordFont.DoubleStrikeThrough := 1;
// if ChkBoxStrike.checked then WordFont.StrikeThrough := 1;
// WordFont.Size := StrToInt(Size.text);
// if Fonttype.Itemindex >= 0 then
// WordFont.Name := FontType.Items[FontType.Itemindex];
end;
begin
//下面的这段是我编写的一段控制WORD页面参数的语句,你可以删除改成自己的
try
WordSetupForm:=TWordSetupForm.Create(nil);
WordSetupForm.ShowModal;
i:=WordSetupForm.ModalResult;
if i=mrOK then
begin
tReportTitle:=WordSetupForm.edtReportTitle.Text;
tPageWidth:=StrToFloat(WordSetupForm.edtPageWidth.Text);
tPageHeight:=StrToFloat(WordSetupForm.edtPageHeight.Text);
tTopMargin:=StrToFloat(WordSetupForm.edtTopMargin.Text);
tBottomMargin:=StrToFloat(WordSetupForm.edtBottomMargin.Text);
tLeftMargin:=StrToFloat(WordSetupForm.edtLeftMargin.Text);
tRightMargin:=StrToFloat(WordSetupForm.edtRightMargin.Text);
tFX:=WordSetupForm.rgFX.ItemIndex;
tXHLen:=StrToFloat(WordSetupForm.edtXHLen.Text);
end;
finally
WordSetupForm.Free;
end;
if i=mrCancel then exit;
//下面正式进入表格打印...这是实际的代码
try
WordApplication:=TWordApplication.Create(nil);
WordDocument:=TWordDocument.Create(nil);
WordParagraphFormat:=TWordParagraphFormat.Create(nil);
WordFont:=TWordFont.Create(nil);
Template := EmptyParam; //这里若使用模板,则应使用模板的绝对路径名
NewTemplate := True;
ItemIndex := 1;
try
WordApplication.Disconnect;
Wordapplication.Connect;
except
MessageDlg('Word 可能还没有安装!', mtError, [mbOk], 0);
Abort;
end;
Wordapplication.Visible := True;
WordApplication.Caption := tReportTitle;
Template := EmptyParam;
NewTemplate := False;
WordApplication.Documents.Add(Template, NewTemplate);
{Assign WordDocument component}
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
{Turn Spell checking of because it takes a long time if enabled and slows down Winword}
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;
WordApplication.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
WordApplication.Selection.Font.Bold := wdToggle;
WordApplication.Selection.Font.Size := 22;
WordApplication.Selection.TypeText(tReportTitle);
WordApplication.Selection.TypeParagraph;
WordApplication.Selection.Font.Bold := wdToggle;
WordApplication.Selection.Font.Size := 11;
WordApplication.Selection.ParagraphFormat.Alignment := wdAlignParagraphLeft;
if tFX=0 then WordDocument.PageSetup.Orientation:=wdOrientPortrait
else WordDocument.PageSetup.Orientation:=wdOrientLandscape;
WordDocument.PageSetup.TopMargin:=WordApplication.MillimetersToPoints(tTopMargin);
WordDocument.PageSetup.BottomMargin:=WordApplication.MillimetersToPoints(tBottomMargin);
WordDocument.PageSetup.LeftMargin:=WordApplication.MillimetersToPoints(tLeftMargin);
WordDocument.PageSetup.RightMargin:=WordApplication.MillimetersToPoints(tRightMargin);
WordDocument.PageSetup.PageWidth:=WordApplication.MillimetersToPoints(tPageWidth);
WordDocument.PageSetup.PageHeight:=WordApplication.MillimetersToPoints(tPageHeight);
//设置页脚
if WordDocument.ActiveWindow.View.SplitSpecial<>wdPaneNone then
WordDocument.ActiveWindow.Panes.Item(2).Close;
if (WordDocument.ActiveWindow.ActivePane.View.type_=wdNormalView)
or (WordDocument.ActiveWindow.ActivePane.View.type_=wdOutlineView) then
WordDocument.ActiveWindow.ActivePane.View.type_:=wdPrintPreview;
WordDocument.ActiveWindow.ActivePane.View.SeekView:=wdSeekCurrentPageHeader;
if WordApplication.Selection.HeaderFooter.IsHeader then
WordDocument.ActiveWindow.ActivePane.View.SeekView:= wdSeekCurrentPageFooter
else
WordDocument.ActiveWindow.ActivePane.View.SeekView:= wdSeekCurrentPageHeader;
WordApplication.Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
WordApplication.Selection.TypeText('第');
v1:=wdFieldPage;v2:=EmptyParam;v3:=EmptyParam;
WordApplication.Selection.Fields.Add(WordApplication.Selection.Range,v1,v2,v3);
WordApplication.Selection.TypeText('/');
v1:=wdFieldNumPages;v2:=EmptyParam;v3:=EmptyParam;
WordApplication.Selection.Fields.Add(WordApplication.Selection.Range,v1,v2,v3);
WordApplication.Selection.TypeText('页');
v1:=wdLine; v2:=EmptyParam;
WordApplication.Selection.HomeKey(v1,v2);
v1:=wdLine; v2:=wdExtend;
WordApplication.Selection.EndKey(v1,v2);
WordApplication.Selection.Borders.Item(wdBorderTop).LineStyle:=WordApplication.Options.DefaultBorderLineStyle;
WordApplication.Selection.Borders.Item(wdBorderTop).LineWidth:=wdLineWidth050pt;
WordApplication.Selection.Borders.Item(wdBorderTop).ColorIndex:=WordApplication.Options.DefaultBorderColorIndex;
WordApplication.ActiveWindow.ActivePane.View.SeekView := wdSeekCurrentPageHeader;
v1:=wdLine; v2:=EmptyParam;
WordApplication.Selection.HomeKey(v1,v2);
v1:=wdLine; v2:=wdExtend;
WordApplication.Selection.EndKey(v1,v2);
WordApplication.Selection.Borders.Item(wdBorderTop).LineStyle := wdLineStyleNone;
WordApplication.Selection.Borders.Item(wdBorderLeft).LineStyle := wdLineStyleNone;
WordApplication.Selection.Borders.Item(wdBorderBottom).LineStyle := wdLineStyleNone;
WordApplication.Selection.Borders.Item(wdBorderRight).LineStyle := wdLineStyleNone;
WordDocument.ActiveWindow.ActivePane.View.SeekView := wdSeekMainDocument;
with WordDocument do
begin
j:=0;
for coli:=0 to (tempGrid as TDBGrid).FieldCount-1 do
if not (tempGrid as TDBGrid).DataSource.DataSet.FieldByName((tempGrid as TDBGrid).Columns[coli].FieldName).Visible then Continue
else inc(j);
//创建表格
tables.Add(WordDocument.Words.Last,(tempGrid as TDBGrid).DataSource.DataSet.RecordCount+1,j+1); //考虑加一个序号
//完成表头
tables.Item(1).Cell(1,1).Width :=WordApplication.MillimetersToPoints(tXHLen); //序号
tables.Item(1).Cell(1,1).Range.Text:='序号';
//下面完成其它的表头部分
j:=2;
for coli:=0 to (tempGrid as TDBGrid).FieldCount-1 do
begin
if not (tempGrid as TDBGrid).DataSource.DataSet.FieldByName((tempGrid as TDBGrid).Columns[coli].FieldName).Visible then Continue;
begin
tables.Item(1).Cell(1, j).Width :=WordApplication.MillimetersToPoints(ScreenPixelToMM((tempGrid as TDBGrid).Columns[coli].Width));
tables.Item(1).Cell(1, j).Range.Text:=(tempGrid as TDBGrid).Columns[coli].FieldName;
end;
inc(j);
end;
//表格居中
WordApplication.Selection.Tables.Item(1).Rows.Alignment := wdAlignRowCenter;
//不允许跨页断行
WordApplication.Selection.Tables.Item(1).Rows.AllowBreakAcrossPages := 0;
//将数据填入表格
for rowi:=0 to (tempGrid as TDBGrid).DataSource.DataSet.RecordCount-1 do
begin
tables.Item(1).Cell(rowi+2,1).Range.Set_Text(IntToStr(rowi+1));
tables.Item(1).Cell(rowi+2,1).Width :=WordApplication.MillimetersToPoints(tXHLen); //序号
i:=1;
for coli:=0 to (tempGrid as TDBGrid).FieldCount-1 do
begin
if not (tempGrid as TDBGrid).DataSource.DataSet.FieldByName((tempGrid as TDBGrid).Columns[coli].FieldName).Visible then Continue;
inc(i);
if i>=j then Break;
tables.Item(1).Cell(rowi+2,i).Range.Set_Text((tempGrid as TDBGrid).DataSource.DataSet.FieldByName((tempGrid as TDBGrid).Columns[coli].FieldName).AsString);
tables.Item(1).Cell(rowi+2,i).Width :=WordApplication.MillimetersToPoints(ScreenPixelToMM((tempGrid as TDBGrid).Columns[coli].Width));
end;
(tempGrid as TDBGrid).DataSource.DataSet.Next;
end;
(tempGrid as TDBGrid).DataSource.DataSet.First;
end;
//表头格式设置
v1:=wdStory; v2:=EmptyParam;
WordApplication.Selection.HomeKey(v1,v2);
v1:=wdLine; v2:=1; v3:=EmptyParam;
WordApplication.Selection.MoveDown(v1,v2,v3);
//在各页顶端以表头形式出现,office 2000 适用
// WordApplication.Selection.Tables.Item(1).Rows.HeadingFormat:= wdToggle;
WordApplication.Selection.SelectRow;
WordApplication.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
//表格线条设置
WordApplication.Selection.Tables.Item(1).Select;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderLeft).LineStyle := wdLineStyleSingle;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderLeft).LineWidth := wdLineWidth100pt;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderLeft).ColorIndex := wdAuto;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderRight).LineStyle := wdLineStyleSingle;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderRight).LineWidth := wdLineWidth100pt;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderRight).ColorIndex := wdAuto;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderTop).LineStyle := wdLineStyleSingle;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderTop).LineWidth := wdLineWidth100pt;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderTop).ColorIndex := wdAuto;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderBottom).LineStyle := wdLineStyleSingle;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderBottom).LineWidth := wdLineWidth100pt;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderBottom).ColorIndex := wdAuto;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderHorizontal).LineStyle := wdLineStyleSingle;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderHorizontal).LineWidth := wdLineWidth050pt;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderHorizontal).ColorIndex := wdAuto;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderVertical).LineStyle := wdLineStyleSingle;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderVertical).LineWidth := wdLineWidth050pt;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderVertical).ColorIndex := wdAuto;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderDiagonalDown).LineStyle := wdLineStyleNone;
WordApplication.Selection.Tables.Item(1).Borders.Item(wdBorderDiagonalUp).LineStyle := wdLineStyleNone;
WordApplication.Selection.Tables.Item(1).Borders.Shadow := False;
v1:=wdStory; v2:=EmptyParam;
WordApplication.Selection.HomeKey(v1,v2);
except
on E: Exception do
begin
ShowMessage(E.Message);
end;
end;
WordApplication.Disconnect;
WordFont.Free;
WordParagraphFormat.Free;
WordDocument.Free;
WordApplication.Free;
end;