請問如何將database 的資料表名稱轉至Word或Excel |
|
ok2002
一般會員 發表:3 回覆:8 積分:2 註冊:2002-04-14 發送簡訊給我 |
|
領航天使
站長 發表:12216 回覆:4186 積分:4084 註冊:2001-07-25 發送簡訊給我 |
引言: 請問如何將現有Oracle 或MSSQL 的DATABASE 的資料表的欄位名稱.型別.長度.PK.是否為Null 轉出Word 或Excel 謝謝找出一斷以前寫的程式,希望對您有幫助: Table1.open; if Table1.active then begin NewFile; MainForm.ComboBox1.text:=ComboBox1.Text; MainForm.Edit1.text:=ComboBox2.Text; DBHFileName:=ComboBox2.Text '.DBH'; Table1.fieldDefs.update; for i:=0 to Table1.fieldDefs.count-1 do begin with Table1.fieldDefs.Items[i] do begin if i<>0 then StringGrid1.rowcount:=StringGrid1.rowcount 1; StringGrid1.Cells[1,i 1]:=Name; StringGrid1.Cells[2,i 1]:=FieldTypeAlis[DataType]; StringGrid1.Cells[3,i 1]:=inttostr(size); if Required then StringGrid1.cells[4,i 1]:='R'; StringGrid1.Cells[5,i 1]:=Name; end; end; Table1.IndexDefs.Update; for i:=0 to Table1.IndexDefs.count-1 do begin with Table1.IndexDefs.Items[i] do begin if i<>0 then StringGrid2.rowcount:=StringGrid2.rowcount 1; tmp:=''; if (ixPrimary in Options) then tmp:=tmp 'P'; if (ixUnique in Options) then tmp:=tmp 'U'; if (ixDescending in Options) then tmp:=tmp 'U'; if (ixExpression in Options) then tmp:=tmp 'E'; { if (ixCaseInSensitive in Options) then tmp:=tmp 'C';} StringGrid2.Cells[1,i 1]:=Name; StringGrid2.Cells[2,i 1]:=fields; StringGrid2.Cells[3,i 1]:=tmp; StringGrid2.Cells[4,i 1]:=Name; end; end; AdjustCaption; end; Table1.close; end;~~~Delphi K.Top討論區站長~~~
------
~~~Delphi K.Top討論區站長~~~ |
rowxuan
一般會員 發表:1 回覆:7 積分:1 註冊:2002-06-13 發送簡訊給我 |
|
領航天使
站長 發表:12216 回覆:4186 積分:4084 註冊:2001-07-25 發送簡訊給我 |
Table1.open;打開Table if Table1.active then打開成功 begin Table1.fieldDefs.update;更新Table的欄位資訊 for i:=0 to Table1.fieldDefs.count-1 doTable的每一個欄位個跑一遍 begin with Table1.fieldDefs.Items[i] do使用欄位資訊 begin StringGrid1.Cells[1,i 1]:=Name; 將欄位名稱存入stringgrid StringGrid1.Cells[2,i 1]:=FieldTypeAlis[DataType];將資料型態轉成文字名稱 StringGrid1.Cells[3,i 1]:=inttostr(size);將欄位大小存入stringgrid if Required then StringGrid1.cells[4,i 1]:='R';就是Not Null end; end; Table1.IndexDefs.Update;更新Table的索引資訊 for i:=0 to Table1.IndexDefs.count-1 do每個index各跑一遍 begin with Table1.IndexDefs.Items[i] do取出index資訊 begin tmp:=''; if (ixPrimary in Options) then tmp:=tmp 'P'; Primary Key if (ixUnique in Options) then tmp:=tmp 'U';Uni-Key if (ixDescending in Options) then tmp:=tmp 'U';降羃 Key if (ixExpression in Options) then tmp:=tmp 'E';運算 Key { if (ixCaseInSensitive in Options) then tmp:=tmp 'C';} StringGrid2.Cells[1,i 1]:=Name;index名稱 StringGrid2.Cells[2,i 1]:=fields;index的欄位 end; end; end; Table1.close; end;對了還有一個Array FieldTypeTot =23; FieldType : array[1..FieldTypeTot] of record key:string; cmd:string; memo:string; end =( { (key:'S' ;memo:'Smallint ';ft:ftSmallint ), (key:'I' ;memo:'Integer ';ft:ftInteger ), (key:'W' ;memo:'Word ';ft:ftWord ), (key:'L' ;memo:'Boolean ';ft:ftBoolean ), (key:'N' ;memo:'Float ';ft:ftFloat ), (key:'$' ;memo:'Currency ';ft:ftCurrency ), (key:'#' ;memo:'BCD ';ft:ftBCD ), (key:'D' ;memo:'Date ';ft:ftDate ), (key:'T' ;memo:'Time ';ft:ftTime ), (key:'@' ;memo:'DateTime ';ft:ftDateTime ), (key:'Y' ;memo:'Bytes ';ft:ftBytes ), (key:'V' ;memo:'VarBytes ';ft:ftVarBytes ), (key:' ' ;memo:'AutoInc ';ft:ftAutoInc ), (key:'BLOB';memo:'Blob ';ft:ftBlob ), (key:'M' ;memo:'Memo ';ft:ftMemo ), (key:'G' ;memo:'Graphic ';ft:ftGraphic ), (key:'F' ;memo:'FmtMemo ';ft:ftFmtMemo ), (key:'POLE';memo:'ParadoxOle ';ft:ftParadoxOle ), (key:'DOLE';memo:'DBaseOle ';ft:ftDBaseOle ), (key:'B' ;memo:'TypedBinary';ft:ftTypedBinary) } (key:'C' ;cmd:'CHAR(n) ';memo:'¤å¦r'), (key:'N' ;cmd:'NUMERIC(n) ';memo:'¼Æ¦r'), (key:'S' ;cmd:'SMALLINT ';memo:''), (key:'I' ;cmd:'INTEGER ';memo:'¾¼Æ'), (key:'F' ;cmd:'FLOAT ';memo:''), (key:'DE' ;cmd:'DECIMAL(n) ';memo:''), (key:'B' ;cmd:'BOOLEAN ';memo:'¯u°²'), (key:'D' ;cmd:'DATE ';memo:'¤é´Á'), (key:'T' ;cmd:'TIME ';memo:'®É¶¡'), (key:'DT' ;cmd:'DATETIME ';memo:'¤é´Á®É¶¡'), (key:'$' ;cmd:'MONEY ';memo:'ª÷¿ú'), (key:' ' ;cmd:'AUTOINC ';memo:'¦Û°Ê²Ö¥['), (key:'BT' ;cmd:'BYTES(n) ';memo:''), (key:'M' ;cmd:'BLOB(n,1) ';memo:'MEMO'), (key:'BI' ;cmd:'BLOB(n,2) ';memo:'BINARY'), (key:'FM' ;cmd:'BLOB(n,3) ';memo:'FMT-MEMO'), (key:'O' ;cmd:'BLOB(n,4) ';memo:'OLEOBJ'), (key:'G' ;cmd:'BLOB(n,5) ';memo:'GRAPHIC'), (key:'U' ;cmd:'DOUBLE PRECISION ';memo:''), (key:'CH' ;cmd:'CHARACTER(n) ';memo:''), (key:'CV' ;cmd:'CHARACTER VARYING(n)';memo:''), (key:'VC' ;cmd:'VARCHAR(n) ';memo:''), (key:'LR' ;cmd:'LONG RAW ';memo:'ORACLE¹Ï¤å') ); // ¦Û©wÄæ¦ì¥N¸¹ type TFieldTypeAlisMap = array [TFieldType] of string[2]; const FieldTypeAlis : TFieldTypeAlisMap = ( '_','VC','SI','I','S','B','F','$','DE','D','T', 'DT','BI','BI',' ','BI','M','G','FM','O','O','BI','_','_','_','_','_','_','_','_','_','_','_','_','_','_');~~~Delphi K.Top討論區站長~~~
------
~~~Delphi K.Top討論區站長~~~ |
rowxuan
一般會員 發表:1 回覆:7 積分:1 註冊:2002-06-13 發送簡訊給我 |
站長大哥..麻煩您看一下程式....我試了後還是不行耶... unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, Grids, DBGrids, DBTables; type
TForm1 = class(TForm)
Table1: TTable;
Button1: TButton;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
Database1: TDatabase;
procedure Button1Click(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
tmp: string; begin
Table1.open; //打開Table
if Table1.active then //打開成功
StringGrid1.RowCount := Table1.FieldCount 1;
StringGrid1.ColCount := Table1.FieldCount 1;
begin
Table1.fieldDefs.update; //更新Table的欄位資訊
for i := 0 to Table1.fieldDefs.count - 1 do //Table的每一個欄位個跑一遍
begin
with Table1.fieldDefs.Items[i] do //使用欄位資訊
begin
StringGrid1.Cells[1, i 1] := Name; //將欄位名稱存入stringgrid
//StringGrid1.Cells[2,i 1]:=FieldTypeAlis[DataType];//將資料型態轉成文字名稱
{
FieldTypeTot=23;
FieldType : array[1..FieldTypeTot] of record
TFieldTypeAlisMap = array [TFieldType] of string[2];
const FieldTypeAlis : TFieldTypeAlisMap=( '_','VC','SI','I','S','B','F','$','DE','D','T','DT','BI','BI',' ','BI','M','G','FM','O','O','BI','_','_','_','_','_','_','_','_','_','_','_','_','_','_');
}
StringGrid1.Cells[3, i 1] := inttostr(size); //將欄位大小存入stringgrid
if Required then StringGrid1.cells[4, i 1] := 'Y'; //就是Not Null
//StringGrid1.cells[4,i 1]:=BoolToStr(Required); //就是Not Null
end;
end;
Table1.IndexDefs.Update; //更新Table的索引資訊
for i := 0 to Table1.IndexDefs.count - 1 do //每個index各跑一遍
begin
with Table1.IndexDefs.Items[i] do //取出index資訊
begin
tmp := '';
if (ixPrimary in Options) then tmp := tmp 'P'; //Primary Key
if (ixUnique in Options) then tmp := tmp 'U'; //Uni-Key
if (ixDescending in Options) then tmp := tmp 'U'; //降羃 Key
if (ixExpression in Options) then tmp := tmp 'E'; //運算 Key
if (ixCaseInSensitive in Options) then tmp := tmp 'C';
StringGrid2.Cells[1, i 1] := Name; //index名稱
StringGrid2.Cells[2, i 1] := fields; //index的欄位
end;
end;
end;
Table1.close;
end; FieldTypeTot=23;
FieldType : array[1..FieldTypeTot] of record
key: string;
cmd: string;
memo: string;
end
TFieldTypeAlisMap = array [TFieldType] of string[2];
const FieldTypeAlis : TFieldTypeAlisMap =( '_','VC','SI','I','S','B','F','$','DE','D','T','DT','BI','BI',' ','BI','M','G','FM','O','O','BI','_','_','_','_','_','_','_','_','_','_','_','_','_','_'); end.
|
領航天使
站長 發表:12216 回覆:4186 積分:4084 註冊:2001-07-25 發送簡訊給我 |
這一次我都測過了,沒問題:
拉一個Form1,button1,StringGrid1,StringGrid2,table1
unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Db, DBTables, Grids; type TForm1 = class(TForm) Button1: TButton; Table1: TTable; StringGrid1: TStringGrid; StringGrid2: TStringGrid; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; const FieldTypeTot = 23; var Form1: TForm1; FieldType: array[1..FieldTypeTot] of record key: string; cmd: string; memo: string; end; type TFieldTypeAlisMap = array[TFieldType] of string[2]; const FieldTypeAlis: TFieldTypeAlisMap = ('_', 'VC', 'SI', 'I', 'S', 'B', 'F', '$', 'DE', 'D', 'T', 'DT', 'BI', 'BI', ' ', 'BI', 'M', 'G', 'FM', 'O', 'O', 'BI', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_', '_'); implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject); var i: integer; tmp: string; begin Table1.open; //¥´¶}Table if Table1.active then //¥´¶}¦¨¥\ StringGrid1.RowCount := Table1.FieldCount 1; StringGrid1.ColCount := Table1.FieldCount 1; begin Table1.fieldDefs.update; //§ó·sTableªºÄæ¦ì¸ê°T for i := 0 to Table1.fieldDefs.count - 1 do //Tableªº¨C¤@ÓÄæ¦ìÓ¶]¤@¹M begin with Table1.fieldDefs.Items[i] do //¨Ï¥ÎÄæ¦ì¸ê°T begin StringGrid1.Cells[1, i 1] := Name; //±NÄæ¦ì¦WºÙ¦s¤Jstringgrid StringGrid1.Cells[2, i 1] := FieldTypeAlis[DataType]; //±N¸ê®Æ«¬ºAÂন¤å¦r¦WºÙ StringGrid1.Cells[3, i 1] := inttostr(size); //±NÄæ¦ì¤j¤p¦s¤Jstringgrid if Required then StringGrid1.cells[4, i 1] := 'Y'; //´N¬ONot Null end; end; Table1.IndexDefs.Update; //§ó·sTableªº¯Á¤Þ¸ê°T for i := 0 to Table1.IndexDefs.count - 1 do //¨CÓindex¦U¶]¤@¹M begin with Table1.IndexDefs.Items[i] do //¨ú¥Xindex¸ê°T begin tmp := ''; if (ixPrimary in Options) then tmp := tmp 'P'; //Primary Key if (ixUnique in Options) then tmp := tmp 'U'; //Uni-Key if (ixDescending in Options) then tmp := tmp 'U'; //°ò» Key if (ixExpression in Options) then tmp := tmp 'E'; //¹Bºâ Key if (ixCaseInSensitive in Options) then tmp := tmp 'C'; StringGrid2.Cells[1, i 1] := Name; //index¦WºÙ StringGrid2.Cells[2, i 1] := fields; //indexªºÄæ¦ì end; end; end; Table1.close; end; end. end.~~~Delphi K.Top討論區站長~~~
------
~~~Delphi K.Top討論區站長~~~ |
領航天使
站長 發表:12216 回覆:4186 積分:4084 註冊:2001-07-25 發送簡訊給我 |
|
領航天使
站長 發表:12216 回覆:4186 積分:4084 註冊:2001-07-25 發送簡訊給我 |
|
ok2002
一般會員 發表:3 回覆:8 積分:2 註冊:2002-04-14 發送簡訊給我 |
站長你好:這是我看過最好的討論區,真是辛苦大哥你了!
FieldtypeNames: TFieldTypeAlisMap = ('_', 'VC', 'SI', 'I', 'S', 'B', 'F', '$','DE', 'D', 'T', 'DT', 'BI', 'BI', ' ', 'BI', 'M', 'G', 'FM', 'O', 'O', 'BI','_', '_', '_', '_', '_', '_', '_', '_', '_', '_', ' _', '_', '_', '_'); 出現:
[Error] Unit1.pas(36): Number of elements differs from declaration 請問作用何在?mark掉即可執行!
|
rowxuan
一般會員 發表:1 回覆:7 積分:1 註冊:2002-06-13 發送簡訊給我 |
|
領航天使
站長 發表:12216 回覆:4186 積分:4084 註冊:2001-07-25 發送簡訊給我 |
好啦!我再仔細重寫一次了!
1.拉一個Form1
2.拉兩個StringGrid1/2
3.拉一個Button1 (按兩下產生OnClick事件)
4.拉一個Table (指向一個Database的Table)
5.將程式碼改為 unit Unit1; interface uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Db, DBTables, Grids; type
TForm1 = class(TForm)
Button1: TButton;
Table1: TTable;
StringGrid1: TStringGrid;
StringGrid2: TStringGrid;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.DFM} procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
tmp: string;
begin
Table1.open; //打開Table
Table1.fieldDefs.update; //更新欄位資料
StringGrid1.RowCount := Table1.FieldCount 1;
StringGrid1.ColCount := 5;
StringGrid1.Cells[1,0]:='欄位名稱';
StringGrid1.Cells[2,0]:='欄位型態';
StringGrid1.Cells[3,0]:='欄位大小';
StringGrid1.Cells[4,0]:='NOT NULL'; Table1.IndexDefs.Update; //更新索引資料
StringGrid2.RowCount := Table1.IndexDefs.count 1;
StringGrid2.ColCount := 4;
StringGrid2.Cells[1,0]:='索引名稱';
StringGrid2.Cells[2,0]:='索引欄位';
StringGrid2.Cells[3,0]:='索引型態';
begin
Table1.fieldDefs.update; //更新欄位資料
for i := 0 to Table1.fieldDefs.count - 1 do //Table的每一個欄位各跑一次
begin
with Table1.fieldDefs.Items[i] do //取出欄位資訊
begin
StringGrid1.Cells[1, i 1] := Name; //取出欄位名稱
StringGrid1.Cells[2, i 1] := FieldtypeNames[datatype]; // 取出欄位型態
StringGrid1.Cells[3, i 1] := inttostr(size); //取出欄位大小
if Required then
StringGrid1.cells[4, i 1] := 'Y'; //是否Not Null
end;
end;
Table1.IndexDefs.Update; //更索所引資料
for i := 0 to Table1.IndexDefs.count - 1 do //每一個索引各跑一次
begin
with Table1.IndexDefs.Items[i] do //取出索引資訊
begin
tmp := '';
if (ixPrimary in Options) then tmp := tmp 'P'; //Primary Key
if (ixUnique in Options) then tmp := tmp 'U'; //Uni-Key
if (ixDescending in Options) then tmp := tmp 'D'; //降冪 Key
if (ixExpression in Options) then tmp := tmp 'E'; //運算 Key
if (ixCaseInSensitive in Options) then tmp := tmp 'C'; // 分大小寫的KEY
StringGrid2.Cells[1, i 1] := Name; //取出索引名稱
StringGrid2.Cells[2, i 1] := fields; //取出索引的欄位資料
StringGrid2.Cells[3, i 1] := tmp; //取出索引的型態
end;
end;
end;
Table1.close; // 關掉Table
end; end. 再不行,請下載原始程式碼:http://delphi.ktop.com.tw/forum.asp?FORUM_ID=115 ~~~Delphi K.Top討論區站長~~~
------
~~~Delphi K.Top討論區站長~~~ |
rowxuan
一般會員 發表:1 回覆:7 積分:1 註冊:2002-06-13 發送簡訊給我 |
|
rowxuan
一般會員 發表:1 回覆:7 積分:1 註冊:2002-06-13 發送簡訊給我 |
|
hxf_wolf
一般會員 發表:7 回覆:38 積分:9 註冊:2002-03-14 發送簡訊給我 |
var
i:integer;
str:string;
strlist:Tstringlist;
begin
try
with Table1 do
begin
first;
DisableControls;
while not eof do
begin
str:='';
for i:=0 to fieldcount-1 do
begin
str:=str fields[i].asstring #9;
strlist.add(str);
next;
end;
strlist.savetofile(c:\a.xls);
enableControls;
end;
end;
finally
strlist.free;
end;
end;
------
Happy Coding |
ok2002
一般會員 發表:3 回覆:8 積分:2 註冊:2002-04-14 發送簡訊給我 |
procedure TForm1.Button1Click(Sender: TObject);
var
MsExcel, MsExcelWorkBook, MsExcelWorkSheet: Variant;
i, j,k,l: Integer;
s : String;
begin
Table1.Open; if Table1.Active then
begin
try
MsExcel := CreateOleObject('Excel.Application');
MsExcelWorkBook := MsExcel.WorkBooks.Add;
MsExcelWorkSheet := MsExcel.WorkSheets.Add;
MsExcel.Visible := True;
i := 2;
for k := 0 to Pred(StringGrid1.RowCount) do
begin
for l := 1 to Pred(StringGrid1.ColCount) do
begin
s := StringGrid1.Cells[l,k];
MsExcelWorkSheet.Range[Chr(64 l) IntToStr(K 1)].Value := s; end; end; finally
MsExcel.Quit;
end;
end;
end;
|
jthwang
一般會員 發表:1 回覆:21 積分:4 註冊:2002-07-31 發送簡訊給我 |
下列程式邏輯似乎有些問題,我稍微改一下就可了,提供大家參考!!
//----------------------------------------------------
procedure TForm3.Button1Click(Sender: TObject);
var
i:integer;
str:string;
strlist:Tstringlist;
begin
strlist := TStringList.Create;
try
with clientdataset1 do
begin
first;
clientdataset1.DisableControls;
while not eof do begin
str:='';
for i:=0 to clientdataset1.FieldCount-1 do
begin
str:=str+fields[i].asstring+#9;
end;
strlist.add(str);
strlist.savetofile('c:\a.txt');
enableControls;
next;
end;
end;
finally
strlist.free;
end;
end; //----------------------------------------------------
引言: var i:integer; str:string; strlist:Tstringlist; begin try with Table1 do begin first; DisableControls; while not eof do begin str:=''; for i:=0 to fieldcount-1 do begin str:=str fields[i].asstring #9; strlist.add(str); next; end; strlist.savetofile(c:\a.xls); enableControls; end; end; finally strlist.free; end; end; |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |