1:如何将多个DBGrid中的内容导到一个Word文档的多个表格中?
2:如何能得到一个word文档中有几个表格(表格不嵌套)?
补充:(1)word文档中事先没有表格,表格由程序自动生产(这个我会)。
(2)现在的问题是多个表格位置的控制,如何才不至于表格都嵌套在一起。
2:如何能得到一个word文档中有几个表格(表格不嵌套)?
补充:(1)word文档中事先没有表格,表格由程序自动生产(这个我会)。
(2)现在的问题是多个表格位置的控制,如何才不至于表格都嵌套在一起。
解决方案 »
- win7下使用UrlDownloadToFile无法保存文件??
- 大家好:请问一下Shockwave Flash Object 格式的文件用什么工具打开——————在线等待!!!!!
- 各位大侠,小妹请教一个问题,来者有分!!!!!!!
- 拜托,哪位高手指点一下
- 怎么让一个Edit只能输入数字和小数点?
- 不好意思我把问题说具体些:
- 二个很简单的问题?
- 在做数据库开发的时候经常会遇到数据库连不上的情况,这个时候程序就会卡死,要等好长时间才能有反应,有什么办法解决没
- 集合类型有什么函数可以操作?
- 怎么用 OleVariant 传一个类的内容
- 拜托各位帮我解决一个简单问题,急!急!急!
- 在TTreeview用什么函数判断它的根节点?
http://blog.csdn.net/neowang/archive/2004/10/08/127522.aspx
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, DBCtrls, Grids, DBGrids, StdCtrls, Db, DBTables, OleServer,
Word97, clipbrd, Word2000, WordXP, ADODB;
type
TMyFirstThread = class(TThread)
private protected
procedure Execute;override;
end;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Button1: TButton;
Button2: TButton;
WordDocument: TWordDocument;
WordApplication: TWordApplication;
WordFont: TWordFont;
ChkBoxNewDoc: TCheckBox;
ADOQuery2: TADOQuery;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
procedure SetFontSize();
public
Thread1:TMyFirstThread;
end;var
Form1: TForm1;implementationuses Variants;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
ADOQuery1.Close;
ADOQuery1.SQL.Text:='SELECT * FROM PT_WSMS WHERE (WSLX=''0'' OR WSLX=''1'' OR WSLX=''5'' OR WSLX=''6'') AND (Ltrim(Rtrim(HelpTitle))='''' OR HelpTitle IS NULL)';
ADOQuery1.Open;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Thread1:=TMyFirstThread.Create(False);
Thread1.Priority:=tpNormal;
Thread1.FreeOnTerminate:=True;
end;procedure TForm1.SetFontSize;
begin
WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font);
WordFont.Name:='宋体';
WordFont.Size:=9;
end;{ TMyFirstThread }procedure TMyFirstThread.Execute;
var
Docs, Template, NewTemplate, ItemIndex: OleVariant;
i,j:integer;
RecCount:integer;
begin
with Form1 do
begin
try
Template := EmptyParam;
NewTemplate := True;
ItemIndex := 1;
try
Wordapplication.Connect;
except
MessageDlg('Word没有安装!', mtError, [mbOk], 0);
Abort;
end;
Wordapplication.Visible := True;
WordApplication.Caption := '文书详细';
Template := EmptyParam; {创建新的文档}
NewTemplate := False;
Docs := WordApplication.Documents;
Docs.Add(NewTemplate := True);
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex)); {WordDocument组件赋值}
WordApplication.Options.CheckSpellingAsYouType := False; {关闭拼写检查,如果进行拼写检查,将减缓Winword速度}
WordApplication.Options.CheckGrammarAsYouType := False;
WordDocument.Sentences.Last.Paste;
WordDocument.Range.Text := ''; {插入数据}
i:=1;
while not ADOQuery1.Eof do
begin
j:=6;
ADOQuery2.Close;
ADOQuery2.SQL.Text:='select sysobjects.name as tablename,syscolumns.name as columnname,sysproperties.value as discription from ';
ADOQuery2.SQL.Add('sysobjects,syscolumns,sysproperties where ');
ADOQuery2.SQL.Add('sysobjects.name='''+Trim(ADOQuery1.FieldByName('TableName').AsString)+''' and syscolumns.id = sysobjects.id and sysproperties.smallid= syscolumns.colid and sysproperties.id = sysobjects.id');
ADOQuery2.Open;
RecCount:=ADOQuery2.RecordCount+5;
WordDocument.Tables.Add(WordDocument.Words.Last,RecCount,4,EmptyParam,EmptyParam);
WordDocument.Range.InsertAfter('' + #13);
WordDocument.Tables.Item(i).Cell(1,1).Merge(WordDocument.Tables.Item(i).Cell(1,2));
WordDocument.Tables.Item(i).Cell(1,2).Merge(WordDocument.Tables.Item(i).Cell(1,3));
WordDocument.Tables.Item(i).Cell(2,1).Merge(WordDocument.Tables.Item(i).Cell(2,2));
WordDocument.Tables.Item(i).Cell(2,2).Merge(WordDocument.Tables.Item(i).Cell(2,3));
WordDocument.Tables.Item(i).Cell(3,1).Merge(WordDocument.Tables.Item(i).Cell(3,2));
WordDocument.Tables.Item(i).Cell(3,2).Merge(WordDocument.Tables.Item(i).Cell(3,3));
WordDocument.Tables.Item(i).Cell(4,1).Merge(WordDocument.Tables.Item(i).Cell(4,4));
WordDocument.Tables.Item(i).Cell(1,1).Range.Text := '文书名称';
SetFontSize;
WordDocument.Tables.Item(i).Cell(1,2).Range.Text := ADOQuery1.FieldByName('WritName').AsString;
SetFontSize;
WordDocument.Tables.Item(i).Cell(2,1).Range.Text := '对应表名';
SetFontSize;
WordDocument.Tables.Item(i).Cell(2,2).Range.Text := ADOQuery2.FieldByName('TableName').AsString;
SetFontSize;
WordDocument.Tables.Item(i).Cell(3,1).Range.Text := 'ID号';
SetFontSize;
WordDocument.Tables.Item(i).Cell(3,2).Range.Text := ADOQuery1.FieldByName('ID').AsString;
SetFontSize;
WordDocument.Tables.Item(i).Cell(5,1).Range.Text := '字段名';
SetFontSize;
WordDocument.Tables.Item(i).Cell(5,2).Range.Text := '中文名称';
SetFontSize;
WordDocument.Tables.Item(i).Cell(5,3).Range.Text := '是否系统生成';
SetFontSize;
WordDocument.Tables.Item(i).Cell(5,4).Range.Text := '是否必填字段';
SetFontSize;
while not ADOQuery2.Eof do
begin
WordDocument.Tables.Item(i).Cell(j,1).Range.Text:=ADOQuery2.FieldByName('columnname').AsString;
SetFontSize;
WordDocument.Tables.Item(i).Cell(j,2).Range.Text:=ADOQuery2.FieldByName('discription').AsString;
SetFontSize;
j:=j+1;
ADOQuery2.Next;
end;
ADOQuery1.Next;
i:=i+1;
end;
except
on E: Exception do
begin
ShowMessage(E.Message);
WordApplication.Disconnect;
end;
end;
end;
end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, DBCtrls, Grids, DBGrids, StdCtrls, Db, DBTables, OleServer,
Word97, clipbrd, Word2000, WordXP, ADODB;
type
TMyFirstThread = class(TThread)
private protected
procedure Execute;override;
end;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
Button1: TButton;
Button2: TButton;
WordDocument: TWordDocument;
WordApplication: TWordApplication;
WordFont: TWordFont;
ChkBoxNewDoc: TCheckBox;
ADOQuery2: TADOQuery;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
procedure SetFontSize();
public
Thread1:TMyFirstThread;
end;var
Form1: TForm1;implementationuses Variants;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
ADOQuery1.Close;
ADOQuery1.SQL.Text:='SELECT * FROM PT_WSMS WHERE (WSLX=''0'' OR WSLX=''1'' OR WSLX=''5'' OR WSLX=''6'') AND (Ltrim(Rtrim(HelpTitle))='''' OR HelpTitle IS NULL)';
ADOQuery1.Open;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
Thread1:=TMyFirstThread.Create(False);
Thread1.Priority:=tpNormal;
Thread1.FreeOnTerminate:=True;
end;procedure TForm1.SetFontSize;
begin
WordFont.ConnectTo(WordDocument.Sentences.Get_Last.Font);
WordFont.Name:='宋体';
WordFont.Size:=9;
end;{ TMyFirstThread }procedure TMyFirstThread.Execute;
var
Docs, Template, NewTemplate, ItemIndex: OleVariant;
i,j:integer;
RecCount:integer;
begin
with Form1 do
begin
try
Template := EmptyParam;
NewTemplate := True;
ItemIndex := 1;
try
Wordapplication.Connect;
except
MessageDlg('Word没有安装!', mtError, [mbOk], 0);
Abort;
end;
Wordapplication.Visible := True;
WordApplication.Caption := '文书详细';
Template := EmptyParam; {创建新的文档}
NewTemplate := False;
Docs := WordApplication.Documents;
Docs.Add(NewTemplate := True);
WordDocument.ConnectTo(WordApplication.Documents.Item(ItemIndex)); {WordDocument组件赋值}
WordApplication.Options.CheckSpellingAsYouType := False; {关闭拼写检查,如果进行拼写检查,将减缓Winword速度}
WordApplication.Options.CheckGrammarAsYouType := False;
WordDocument.Sentences.Last.Paste;
WordDocument.Range.Text := ''; {插入数据}
i:=1;
while not ADOQuery1.Eof do
begin
j:=6;
ADOQuery2.Close;
ADOQuery2.SQL.Text:='select sysobjects.name as tablename,syscolumns.name as columnname,sysproperties.value as discription from ';
ADOQuery2.SQL.Add('sysobjects,syscolumns,sysproperties where ');
ADOQuery2.SQL.Add('sysobjects.name='''+Trim(ADOQuery1.FieldByName('TableName').AsString)+''' and syscolumns.id = sysobjects.id and sysproperties.smallid= syscolumns.colid and sysproperties.id = sysobjects.id');
ADOQuery2.Open;
RecCount:=ADOQuery2.RecordCount+5;
WordDocument.Tables.Add(WordDocument.Words.Last,RecCount,4,EmptyParam,EmptyParam);
WordDocument.Range.InsertAfter('' + #13);
WordDocument.Tables.Item(i).Cell(1,1).Merge(WordDocument.Tables.Item(i).Cell(1,2));
WordDocument.Tables.Item(i).Cell(1,2).Merge(WordDocument.Tables.Item(i).Cell(1,3));
WordDocument.Tables.Item(i).Cell(2,1).Merge(WordDocument.Tables.Item(i).Cell(2,2));
WordDocument.Tables.Item(i).Cell(2,2).Merge(WordDocument.Tables.Item(i).Cell(2,3));
WordDocument.Tables.Item(i).Cell(3,1).Merge(WordDocument.Tables.Item(i).Cell(3,2));
WordDocument.Tables.Item(i).Cell(3,2).Merge(WordDocument.Tables.Item(i).Cell(3,3));
WordDocument.Tables.Item(i).Cell(4,1).Merge(WordDocument.Tables.Item(i).Cell(4,4));
WordDocument.Tables.Item(i).Cell(1,1).Range.Text := '文书名称';
SetFontSize;
WordDocument.Tables.Item(i).Cell(1,2).Range.Text := ADOQuery1.FieldByName('WritName').AsString;
SetFontSize;
WordDocument.Tables.Item(i).Cell(2,1).Range.Text := '对应表名';
SetFontSize;
WordDocument.Tables.Item(i).Cell(2,2).Range.Text := ADOQuery2.FieldByName('TableName').AsString;
SetFontSize;
WordDocument.Tables.Item(i).Cell(3,1).Range.Text := 'ID号';
SetFontSize;
WordDocument.Tables.Item(i).Cell(3,2).Range.Text := ADOQuery1.FieldByName('ID').AsString;
SetFontSize;
WordDocument.Tables.Item(i).Cell(5,1).Range.Text := '字段名';
SetFontSize;
WordDocument.Tables.Item(i).Cell(5,2).Range.Text := '中文名称';
SetFontSize;
WordDocument.Tables.Item(i).Cell(5,3).Range.Text := '是否系统生成';
SetFontSize;
WordDocument.Tables.Item(i).Cell(5,4).Range.Text := '是否必填字段';
SetFontSize;
while not ADOQuery2.Eof do
begin
WordDocument.Tables.Item(i).Cell(j,1).Range.Text:=ADOQuery2.FieldByName('columnname').AsString;
SetFontSize;
WordDocument.Tables.Item(i).Cell(j,2).Range.Text:=ADOQuery2.FieldByName('discription').AsString;
SetFontSize;
j:=j+1;
ADOQuery2.Next;
end;
ADOQuery1.Next;
i:=i+1;
end;
except
on E: Exception do
begin
ShowMessage(E.Message);
WordApplication.Disconnect;
end;
end;
end;
end;end.
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed
With Selection.Tables(1)
If .Style <> "网格型" Then
.Style = "网格型"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = True
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = True
End With
ShowVisualBasicEditor = True
关于定位问题 有两种方法:
1。用书签
2。在理面定义特殊字符,查找这个字符的位置
你应该用第一种方法!
Word文档是程序自动创建的,里面没有书签。
现在的关键问题是我创建完了一个表格之后,如何“跳”出来,再去创建下一个表格。关键是如何跳。