如何将TABLE中的数据导到WORD? 如何将TABLE中的数据导到WORD? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你是想用语句直接导出吗??想向存在的word文件继续添加??我的意思是:1.先把table的数据存为文本文件(这个应该不难吧,实在不行,循环从头到尾)2.调用word 打开这个文本文件. 控制Wordvartbl : Table;i,j:integer;un_Var,ex_Var,cnt_Var:OleVariant;row_num,col_num:integer;st:string;begin // 在Word中新建一个文档,并添加文本,然后设置粗体和字体大小 WordApplication1.Connect; WordApplication1.Visible := True; WordApplication1.Documents.Add(EmptyParam,EmptyParam); WordDocument1.Connect; WordApplication1.ActiveWindow.View.Type_:= wdNormalView; WordApplication1.Selection.Font.Name :='黑体'; WordApplication1.Selection.Font.Size := 16; WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter; WordApplication1.Selection.TypeText('昆明市土地信息系统表格输出'); WordApplication1.Selection.TypeParagraph; WordApplication1.Selection.TypeParagraph; WordApplication1.Selection.Font.Name := '宋体'; WordApplication1.Selection.Font.Size :=12; WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphRight; WordApplication1.Selection.TypeText('日期'+formatdatetime('yyyy"年"mm"月"dd"日"',now)); WordApplication1.Selection.TypeParagraph; WordApplication1.Selection.TypeParagraph;//回车 WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphLeft; row_num:=table1.RecordCount; col_num:=table1.Fields.Count; tbl := WordApplication1.ActiveDocument.Tables.Add(WordApplication1.Selection.Range,row_num+1,Col_num); un_Var:=wdCharacter; cnt_Var:=1; ex_Var:=wdMove; table1.First; for j := 0 to Col_num-1 do //标题 begin st:=table1.Fields.Fields[j].FieldName; WordApplication1.Selection.TypeText(st); WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var); end; for i := 0 to row_num-1 do // 行 begin for j := 0 to Col_num-1 do // 列 begin st:=table1.Fields.Fields[j].AsString; WordApplication1.Selection.TypeText(st); WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var); end; WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var); table1.next; end; WordApplication1.Selection.TypeText('制表人:阎磊'); WordApplication1.Selection.TypeParagraph;end; 感谢 yq3woaini(哈哈镜(初级代码工)(★★★)) 提供的代码。。 yq3woaini(哈哈镜(初级代码工)(★★★)) 提供的代码,第一次点击可以顺利地导到Word,但是当关闭WORD再次点击按钮就提示什么"RPC服务器不可用" 导出DBGrid为Word表格。procedure TForm1.Button1Click(Sender: TObject);varWordApp,WordDoc,WordTable:OleVariant;i,j:integer;beginWordApp:=CreateOleObject('Word.Application');WordApp.Visible:=True;WordDoc:=WordApp.Documents.Add;WordTable:=WordDoc.Tables.Add(WordApp.Selection.Range,DBGrid1.DataSource.DataSet.RecordCount+1,DBGrid1.Columns.Count);for i:=1 to DBGrid1.Columns.Count doWordTable.Cell(1,i).Range.InsertAfter(DBGrid1.Columns[i-1].Title.Caption);i:=2;with DBGrid1.DataSource.DataSet dowhile not eof dobeginfor j:=1 to DBGrid1.Columns.Count doWordTable.Cell(i,j).Range.InsertAfter(DBGrid1.Columns[j-1].Field.Value);Next;Inc(i);end;end; undecleared identifer 'CreateoleObject' 为什么出现:undecleared identifer 'CreateoleObject'因为没有:usecomobj unit Main;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, DBCtrls, Grids, DBGrids, StdCtrls, Db, DBTables, OleServer, Word97, clipbrd;type TMainForm = class(TForm) DataSource: TDataSource; DBGrid: TDBGrid; DBNavigator: TDBNavigator; WordDocument: TWordDocument; DBImgFishImg: TDBImage; Table: TTable; BtnInsertRecord: TButton; WordApplication: TWordApplication; WordFont: TWordFont; ChkBoxNewDoc: TCheckBox; GroupBox: TGroupBox; lblFont: TLabel; Fonttype: TListBox; ChkBoxEmboss: TCheckBox; ChkBoxItalic: TCheckBox; ChkBoxBold: TCheckBox; ChkBoxUnderline: TCheckBox; ChkBoxEngrave: TCheckBox; ChkBoxShadow: TCheckBox; ChkBoxDoublestrike: TCheckBox; ChkBoxStrike: TCheckBox; Size: TEdit; lblFontSize: TLabel; btnCloseWord: TButton; BtnPrint: TButton; BtnPreview: TButton; GroupBox2: TGroupBox; lbDocs: TListBox; procedure BtnInsertRecordClick(Sender: TObject); procedure Form1Close(Sender: TObject; var Action: TCloseAction); procedure Form1Create(Sender: TObject); procedure Form1Activate(Sender: TObject); procedure btnCloseWordClick(Sender: TObject); procedure BtnPrintClick(Sender: TObject); procedure BtnPreviewClick(Sender: TObject); procedure WordApplicationDocumentChange(Sender: TObject); private { Private declarations } public { Public declarations } end;var MainForm: TMainForm;implementationuses Variants;{$R *.dfm}procedure TMainForm.BtnInsertRecordClick(Sender: TObject);var Template,NewTemplate,ItemIndex:OleVariant; 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 try Template := EmptyParam; NewTemplate := True; ItemIndex := 1; try Wordapplication.Connect; except MessageDlg('Word may not be installed', mtError, [mbOk], 0); Abort; end; Wordapplication.Visible := True; WordApplication.Caption := 'Delphi automation'; {Create new document} Template := EmptyParam; NewTemplate := False; if ChkBoxNewDoc.Checked then begin WordApplication.Documents.Add(Template, NewTemplate); {Assign WordDocument component} WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex)); end; {Turn Spell checking of because it takes a long time if enabled and slows down Winword} WordApplication.Options.CheckSpellingAsYouType := False; WordApplication.Options.CheckGrammarAsYouType := False; {Insert data} DBImgFishImg.CopyToClipboard; WordDocument.Sentences.Last.Paste; WordDocument.Range.InsertAfter('Common Name: ' + Table.Fields.Fields[2].AsString + #13); SetFont; WordDocument.Range.InsertAfter('Species Name:' + Table.Fields.Fields[3].AsString + #13); WordDocument.Range.InsertAfter('Length: ' + Table.Fields.Fields[4].AsString + #13); WordDocument.Range.InsertAfter(' ' + #13); WordDocument.Range.InsertAfter(' ' + #13); WordDocument.Range.InsertAfter(' ' + #13); BtnCloseWord.Enabled := True; BtnPrint.Enabled := True; BtnPreview.Enabled := True; except on E: Exception do begin ShowMessage(E.Message); WordApplication.Disconnect; end; end;end;procedure TMainForm.Form1Close(Sender: TObject; var Action: TCloseAction);begin Table.Close;end;procedure TMainForm.Form1Create(Sender: TObject);begin Fonttype.Items := Screen.Fonts;end;procedure TMainForm.Form1Activate(Sender: TObject);begin Table.Open;end;procedure TMainForm.btnCloseWordClick(Sender: TObject);var SaveChanges, OriginalFormat, RouteDocument: OleVariant; begin SaveChanges := WdDoNotSaveChanges; OriginalFormat := UnAssigned; RouteDocument := UnAssigned; try WordApplication.Quit(SaveChanges, OriginalFormat, RouteDocument); WordApplication.Disconnect; BtnCloseWord.Enabled := False; BtnPrint.Enabled := False; BtnPreview.Enabled := False; except on E: Exception do begin Showmessage(E.Message); WordApplication.Disconnect; end; end;end;procedure TMainForm.BtnPrintClick(Sender: TObject);begin WordDocument.PrintOut;end;procedure TMainForm.BtnPreviewClick(Sender: TObject);begin WordDocument.PrintPreview;end;procedure TMainForm.WordApplicationDocumentChange(Sender: TObject);begin lbDocs.items.add(WordDocument.Name);end;end. ATGrid报表控件/WEB插件[专业版] c\s,b\s,midas架构的区别和联系???? 为什么delphi写的*.ocx在vb或.net里不能安装? ★★如何通过现成的Excel Sheet复制Sheet★ combox读写 菜鸟问题——怎样打开一个文本文档? 抢分啦!有关文本文件操作,简单,(分数一直要加) 为什么我在装IE6.0时总提示什么数字签名的玩意,然后安装失败? 关于table排序的问题 为什么我用socketconnection连接的三层结构,连接速度那么慢。 我弟今天眼睛受伤,严重的话可能要手术,心情极度低落郁闷,散掉所有分 这怎么做?
1.先把table的数据存为文本文件(这个应该不难吧,实在不行,循环从头到尾)
2.调用word 打开这个文本文件.
tbl : Table;
i,j:integer;
un_Var,ex_Var,cnt_Var:OleVariant;
row_num,col_num:integer;
st:string;
begin
// 在Word中新建一个文档,并添加文本,然后设置粗体和字体大小
WordApplication1.Connect;
WordApplication1.Visible := True;
WordApplication1.Documents.Add(EmptyParam,EmptyParam);
WordDocument1.Connect;
WordApplication1.ActiveWindow.View.Type_:= wdNormalView;
WordApplication1.Selection.Font.Name :='黑体';
WordApplication1.Selection.Font.Size := 16;
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphCenter;
WordApplication1.Selection.TypeText('昆明市土地信息系统表格输出');
WordApplication1.Selection.TypeParagraph;
WordApplication1.Selection.TypeParagraph;
WordApplication1.Selection.Font.Name := '宋体';
WordApplication1.Selection.Font.Size :=12;
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphRight;
WordApplication1.Selection.TypeText('日期'+formatdatetime('yyyy"年"mm"月"dd"日"',now));
WordApplication1.Selection.TypeParagraph;
WordApplication1.Selection.TypeParagraph;//回车
WordApplication1.Selection.ParagraphFormat.Alignment := wdAlignParagraphLeft;
row_num:=table1.RecordCount;
col_num:=table1.Fields.Count;
tbl := WordApplication1.ActiveDocument.Tables.Add(WordApplication1.Selection.Range,row_num+1,Col_num);
un_Var:=wdCharacter;
cnt_Var:=1;
ex_Var:=wdMove;
table1.First;
for j := 0 to Col_num-1 do //标题
begin
st:=table1.Fields.Fields[j].FieldName;
WordApplication1.Selection.TypeText(st);
WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var);
end;
for i := 0 to row_num-1 do // 行
begin
for j := 0 to Col_num-1 do // 列
begin
st:=table1.Fields.Fields[j].AsString;
WordApplication1.Selection.TypeText(st);
WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var);
end;
WordApplication1.Selection.MoveRight(un_Var,cnt_Var,ex_Var);
table1.next;
end;
WordApplication1.Selection.TypeText('制表人:阎磊');
WordApplication1.Selection.TypeParagraph;
end;
因为没有:usecomobj
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, DBCtrls, Grids, DBGrids, StdCtrls, Db, DBTables, OleServer,
Word97, clipbrd;type
TMainForm = class(TForm)
DataSource: TDataSource;
DBGrid: TDBGrid;
DBNavigator: TDBNavigator;
WordDocument: TWordDocument;
DBImgFishImg: TDBImage;
Table: TTable;
BtnInsertRecord: TButton;
WordApplication: TWordApplication;
WordFont: TWordFont;
ChkBoxNewDoc: TCheckBox;
GroupBox: TGroupBox;
lblFont: TLabel;
Fonttype: TListBox;
ChkBoxEmboss: TCheckBox;
ChkBoxItalic: TCheckBox;
ChkBoxBold: TCheckBox;
ChkBoxUnderline: TCheckBox;
ChkBoxEngrave: TCheckBox;
ChkBoxShadow: TCheckBox;
ChkBoxDoublestrike: TCheckBox;
ChkBoxStrike: TCheckBox;
Size: TEdit;
lblFontSize: TLabel;
btnCloseWord: TButton;
BtnPrint: TButton;
BtnPreview: TButton;
GroupBox2: TGroupBox;
lbDocs: TListBox;
procedure BtnInsertRecordClick(Sender: TObject);
procedure Form1Close(Sender: TObject; var Action: TCloseAction);
procedure Form1Create(Sender: TObject);
procedure Form1Activate(Sender: TObject);
procedure btnCloseWordClick(Sender: TObject);
procedure BtnPrintClick(Sender: TObject);
procedure BtnPreviewClick(Sender: TObject);
procedure WordApplicationDocumentChange(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
MainForm: TMainForm;implementationuses Variants;{$R *.dfm}procedure TMainForm.BtnInsertRecordClick(Sender: TObject);
var Template,NewTemplate,ItemIndex:OleVariant; 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
try
Template := EmptyParam;
NewTemplate := True;
ItemIndex := 1;
try
Wordapplication.Connect;
except
MessageDlg('Word may not be installed', mtError, [mbOk], 0);
Abort;
end;
Wordapplication.Visible := True;
WordApplication.Caption := 'Delphi automation';
{Create new document}
Template := EmptyParam;
NewTemplate := False;
if ChkBoxNewDoc.Checked then
begin
WordApplication.Documents.Add(Template, NewTemplate);
{Assign WordDocument component}
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex));
end;
{Turn Spell checking of because it takes a long time if enabled and slows down Winword}
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;
{Insert data}
DBImgFishImg.CopyToClipboard;
WordDocument.Sentences.Last.Paste;
WordDocument.Range.InsertAfter('Common Name: ' + Table.Fields.Fields[2].AsString + #13);
SetFont;
WordDocument.Range.InsertAfter('Species Name:' + Table.Fields.Fields[3].AsString + #13);
WordDocument.Range.InsertAfter('Length: ' + Table.Fields.Fields[4].AsString + #13);
WordDocument.Range.InsertAfter(' ' + #13);
WordDocument.Range.InsertAfter(' ' + #13);
WordDocument.Range.InsertAfter(' ' + #13);
BtnCloseWord.Enabled := True;
BtnPrint.Enabled := True;
BtnPreview.Enabled := True;
except
on E: Exception do
begin
ShowMessage(E.Message);
WordApplication.Disconnect;
end;
end;
end;procedure TMainForm.Form1Close(Sender: TObject; var Action: TCloseAction);
begin
Table.Close;
end;procedure TMainForm.Form1Create(Sender: TObject);
begin
Fonttype.Items := Screen.Fonts;
end;procedure TMainForm.Form1Activate(Sender: TObject);
begin
Table.Open;
end;procedure TMainForm.btnCloseWordClick(Sender: TObject);
var
SaveChanges,
OriginalFormat,
RouteDocument: OleVariant;
begin
SaveChanges := WdDoNotSaveChanges;
OriginalFormat := UnAssigned;
RouteDocument := UnAssigned;
try
WordApplication.Quit(SaveChanges, OriginalFormat, RouteDocument);
WordApplication.Disconnect;
BtnCloseWord.Enabled := False;
BtnPrint.Enabled := False;
BtnPreview.Enabled := False;
except
on E: Exception do
begin
Showmessage(E.Message);
WordApplication.Disconnect;
end;
end;
end;procedure TMainForm.BtnPrintClick(Sender: TObject);
begin
WordDocument.PrintOut;
end;procedure TMainForm.BtnPreviewClick(Sender: TObject);
begin
WordDocument.PrintPreview;
end;procedure TMainForm.WordApplicationDocumentChange(Sender: TObject);
begin
lbDocs.items.add(WordDocument.Name);
end;end.