15 个解决方案
#1
可以设定ADO的连接方式吧?
如果客户是白痴,我一般在Excel里面用VBA写代码导入数据。
你可以在Execl里面按ALT+F11调出编程窗体,用VB代码就可以搞定了。
如果客户是白痴,我一般在Excel里面用VBA写代码导入数据。
你可以在Execl里面按ALT+F11调出编程窗体,用VB代码就可以搞定了。
#2
Excel可以用ODBC或ADO直接连接的,有关设定的方法网上找找,打字的话,字太多,省点劲吧,很好找的
#3
使用如下的方法可以直接调用Excel文件,FileName为文件名,SHeetName为工作薄名(相当于表)
ADOConnection := TADOConnection.Create(nil);
ADOConnection.LoginPrompt := False;
ADOConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0';
ADOQuery := TADOQuery.Create(nil);
ADOQuery.Connection := ADOConnection;
ADOQuery.SQL.Text := 'Select * from [' + SheetName + '$]';
ADOQuery.Open;
With ADOQuery do
begin
First;
while NOT EOF do
begin
// 导入操作的代码
end;
end;
ADOQuery.Close;
ADOConnection.Close;
ADOQuery.Free;
ADOConnection.Free;
ADOConnection := TADOConnection.Create(nil);
ADOConnection.LoginPrompt := False;
ADOConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0';
ADOQuery := TADOQuery.Create(nil);
ADOQuery.Connection := ADOConnection;
ADOQuery.SQL.Text := 'Select * from [' + SheetName + '$]';
ADOQuery.Open;
With ADOQuery do
begin
First;
while NOT EOF do
begin
// 导入操作的代码
end;
end;
ADOQuery.Close;
ADOConnection.Close;
ADOQuery.Free;
ADOConnection.Free;
#4
excel可以用ado直接连接,从一个数据集往另一个数据集写数据就好了
你可以用AdoDataSet控件﹐設置跟AdoConnection一樣。但要在設置欄里面的'全部'工具條
里的Extended Properties項設成Excel 8.0(設是95版本以上)或Excel 5.0。然后在
CommandType項里面選cmdTableDirect﹐不要選CmdTable和CmdTxt﹐選CmdTxt﹐它會說找不
到你要的那個物件﹐就像你上面出的錯那樣。然后再選擇CommandText﹐只要你選好了Excel
文件﹐里面自然就有你要的東東了。最后再將Active設為True﹐就可以了。保証沒有問題。
你可以用AdoDataSet控件﹐設置跟AdoConnection一樣。但要在設置欄里面的'全部'工具條
里的Extended Properties項設成Excel 8.0(設是95版本以上)或Excel 5.0。然后在
CommandType項里面選cmdTableDirect﹐不要選CmdTable和CmdTxt﹐選CmdTxt﹐它會說找不
到你要的那個物件﹐就像你上面出的錯那樣。然后再選擇CommandText﹐只要你選好了Excel
文件﹐里面自然就有你要的東東了。最后再將Active設為True﹐就可以了。保証沒有問題。
#5
为什么将AdoDataSet设置好各项连接属性并选好table后,提示“from 子句语法错误”
#6
不好意思,前几天老是登录不上来,我已经用: afei78223(阿飞) 的方法将此问题解决了,而且比用delphi的server的控件或ole的速度快多了,基本一闪就出来了。
可是如果要用此方法将数据导出该怎么做?我用insert into 的时候老是显示错,是不是表的名字有什么特殊之处?用delphi的server的控件或ole可以导出,但速度太慢了。
能不能给点提示?我要加分。
可是如果要用此方法将数据导出该怎么做?我用insert into 的时候老是显示错,是不是表的名字有什么特殊之处?用delphi的server的控件或ole可以导出,但速度太慢了。
能不能给点提示?我要加分。
#7
导出的代码差不多的,我给一个将DBGrid的内容导出为Excel文件的函数
先执行以下操作:
1)Project->Import Type Library:
2)Select "Microsoft ADO Ext. for DDL and Security"
3)重新命名类的名称(TTable, TColumn, TIndex, TKey, TGroup, TUser, TCatalog) in
(TXTable, TXColumn, TXIndex, TXKey, TXGroup, TXUser, TXCatalog)
4)创建一个单元,名字为ADOX_TLB
5)在你自己的单元里Uses ADOX_TLB单元
unit DBGridExportToExcel;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, ComCtrls, DB, IniFiles, Buttons, dbgrids, ADOX_TLB, ADODB;
type TScrollEvents = class
BeforeScroll_Event: TDataSetNotifyEvent;
AfterScroll_Event: TDataSetNotifyEvent;
AutoCalcFields_Property: Boolean;
end;
procedure DisableDependencies(DataSet: TDataSet; var ScrollEvents: TScrollEvents);
procedure EnableDependencies(DataSet: TDataSet; ScrollEvents: TScrollEvents);
procedure DBGridToExcelADO(DBGrid: TDBGrid; FileName: string; SheetName: string);
implementation
//Support procedures: I made that in order to increase speed in
//the process of scanning large amounts
//of records in a dataset
//we make a call to the "DisableControls" procedure and then disable the "BeforeScroll" and
//"AfterScroll" events and the "AutoCalcFields" property.
procedure DisableDependencies(DataSet: TDataSet; var ScrollEvents: TScrollEvents);
begin
with DataSet do
begin
DisableControls;
ScrollEvents := TScrollEvents.Create();
with ScrollEvents do
begin
BeforeScroll_Event := BeforeScroll;
AfterScroll_Event := AfterScroll;
AutoCalcFields_Property := AutoCalcFields;
BeforeScroll := nil;
AfterScroll := nil;
AutoCalcFields := False;
end;
end;
end;
//we make a call to the "EnableControls" procedure and then restore
// the "BeforeScroll" and "AfterScroll" events and the "AutoCalcFields" property.
procedure EnableDependencies(DataSet: TDataSet; ScrollEvents: TScrollEvents);
begin
with DataSet do
begin
EnableControls;
with ScrollEvents do
begin
BeforeScroll := BeforeScroll_Event;
AfterScroll := AfterScroll_Event;
AutoCalcFields := AutoCalcFields_Property;
end;
end;
end;
//This is the procedure which make the work:
procedure DBGridToExcelADO(DBGrid: TDBGrid; FileName: string; SheetName: string);
var
cat: _Catalog;
tbl: _Table;
col: _Column;
i: integer;
ADOConnection: TADOConnection;
ADOQuery: TADOQuery;
ScrollEvents: TScrollEvents;
SavePlace: TBookmark;
begin
//
//WorkBook creation (database)
cat := CoCatalog.Create;
cat._Set_ActiveConnection('Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0');
//WorkSheet creation (table)
tbl := CoTable.Create;
tbl.Set_Name(SheetName);
//Columns creation (fields)
DBGrid.DataSource.DataSet.First;
with DBGrid.Columns do
begin
for i := 0 to Count - 1 do
if Items[i].Visible then
begin
col := nil;
col := CoColumn.Create;
with col do
begin
Set_Name(Items[i].Title.Caption);
Set_Type_(adVarWChar);
end;
//add column to table
tbl.Columns.Append(col, adVarWChar, 20);
end;
end;
//add table to database
cat.Tables.Append(tbl);
col := nil;
tbl := nil;
cat := nil;
//exporting
ADOConnection := TADOConnection.Create(nil);
ADOConnection.LoginPrompt := False;
ADOConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0';
ADOQuery := TADOQuery.Create(nil);
ADOQuery.Connection := ADOConnection;
ADOQuery.SQL.Text := 'Select * from [' + SheetName + '$]';
ADOQuery.Open;
DisableDependencies(DBGrid.DataSource.DataSet, ScrollEvents);
SavePlace := DBGrid.DataSource.DataSet.GetBookmark;
try
with DBGrid.DataSource.DataSet do
begin
First;
while not Eof do
begin
ADOQuery.Append;
with DBGrid.Columns do
begin
ADOQuery.Edit;
for i := 0 to Count - 1 do
if Items[i].Visible then
begin
ADOQuery.FieldByName(Items[i].Title.Caption).AsString := FieldByName(Items[i].FieldName).AsString;
end;
ADOQuery.Post;
end;
Next;
end;
end;
finally
DBGrid.DataSource.DataSet.GotoBookmark(SavePlace);
DBGrid.DataSource.DataSet.FreeBookmark(SavePlace);
EnableDependencies(DBGrid.DataSource.DataSet, ScrollEvents);
ADOQuery.Close;
ADOConnection.Close;
ADOQuery.Free;
ADOConnection.Free;
end;
end;
end.
先执行以下操作:
1)Project->Import Type Library:
2)Select "Microsoft ADO Ext. for DDL and Security"
3)重新命名类的名称(TTable, TColumn, TIndex, TKey, TGroup, TUser, TCatalog) in
(TXTable, TXColumn, TXIndex, TXKey, TXGroup, TXUser, TXCatalog)
4)创建一个单元,名字为ADOX_TLB
5)在你自己的单元里Uses ADOX_TLB单元
unit DBGridExportToExcel;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, ComCtrls, DB, IniFiles, Buttons, dbgrids, ADOX_TLB, ADODB;
type TScrollEvents = class
BeforeScroll_Event: TDataSetNotifyEvent;
AfterScroll_Event: TDataSetNotifyEvent;
AutoCalcFields_Property: Boolean;
end;
procedure DisableDependencies(DataSet: TDataSet; var ScrollEvents: TScrollEvents);
procedure EnableDependencies(DataSet: TDataSet; ScrollEvents: TScrollEvents);
procedure DBGridToExcelADO(DBGrid: TDBGrid; FileName: string; SheetName: string);
implementation
//Support procedures: I made that in order to increase speed in
//the process of scanning large amounts
//of records in a dataset
//we make a call to the "DisableControls" procedure and then disable the "BeforeScroll" and
//"AfterScroll" events and the "AutoCalcFields" property.
procedure DisableDependencies(DataSet: TDataSet; var ScrollEvents: TScrollEvents);
begin
with DataSet do
begin
DisableControls;
ScrollEvents := TScrollEvents.Create();
with ScrollEvents do
begin
BeforeScroll_Event := BeforeScroll;
AfterScroll_Event := AfterScroll;
AutoCalcFields_Property := AutoCalcFields;
BeforeScroll := nil;
AfterScroll := nil;
AutoCalcFields := False;
end;
end;
end;
//we make a call to the "EnableControls" procedure and then restore
// the "BeforeScroll" and "AfterScroll" events and the "AutoCalcFields" property.
procedure EnableDependencies(DataSet: TDataSet; ScrollEvents: TScrollEvents);
begin
with DataSet do
begin
EnableControls;
with ScrollEvents do
begin
BeforeScroll := BeforeScroll_Event;
AfterScroll := AfterScroll_Event;
AutoCalcFields := AutoCalcFields_Property;
end;
end;
end;
//This is the procedure which make the work:
procedure DBGridToExcelADO(DBGrid: TDBGrid; FileName: string; SheetName: string);
var
cat: _Catalog;
tbl: _Table;
col: _Column;
i: integer;
ADOConnection: TADOConnection;
ADOQuery: TADOQuery;
ScrollEvents: TScrollEvents;
SavePlace: TBookmark;
begin
//
//WorkBook creation (database)
cat := CoCatalog.Create;
cat._Set_ActiveConnection('Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0');
//WorkSheet creation (table)
tbl := CoTable.Create;
tbl.Set_Name(SheetName);
//Columns creation (fields)
DBGrid.DataSource.DataSet.First;
with DBGrid.Columns do
begin
for i := 0 to Count - 1 do
if Items[i].Visible then
begin
col := nil;
col := CoColumn.Create;
with col do
begin
Set_Name(Items[i].Title.Caption);
Set_Type_(adVarWChar);
end;
//add column to table
tbl.Columns.Append(col, adVarWChar, 20);
end;
end;
//add table to database
cat.Tables.Append(tbl);
col := nil;
tbl := nil;
cat := nil;
//exporting
ADOConnection := TADOConnection.Create(nil);
ADOConnection.LoginPrompt := False;
ADOConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0';
ADOQuery := TADOQuery.Create(nil);
ADOQuery.Connection := ADOConnection;
ADOQuery.SQL.Text := 'Select * from [' + SheetName + '$]';
ADOQuery.Open;
DisableDependencies(DBGrid.DataSource.DataSet, ScrollEvents);
SavePlace := DBGrid.DataSource.DataSet.GetBookmark;
try
with DBGrid.DataSource.DataSet do
begin
First;
while not Eof do
begin
ADOQuery.Append;
with DBGrid.Columns do
begin
ADOQuery.Edit;
for i := 0 to Count - 1 do
if Items[i].Visible then
begin
ADOQuery.FieldByName(Items[i].Title.Caption).AsString := FieldByName(Items[i].FieldName).AsString;
end;
ADOQuery.Post;
end;
Next;
end;
end;
finally
DBGrid.DataSource.DataSet.GotoBookmark(SavePlace);
DBGrid.DataSource.DataSet.FreeBookmark(SavePlace);
EnableDependencies(DBGrid.DataSource.DataSet, ScrollEvents);
ADOQuery.Close;
ADOConnection.Close;
ADOQuery.Free;
ADOConnection.Free;
end;
end;
end.
#8
78:cat._Set_ActiveConnection('Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0');
[Error] DBGridExportToExcel.pas(78): Incompatible types: 'String' and 'IDispatch'
[Error] DBGridExportToExcel.pas(78): Incompatible types: 'String' and 'IDispatch'
#9
我在自己的程序里用过的没有错。你有没有先操作1,2,3,4,5的这几个步骤?
#10
有只是我将
2)Select "Microsoft ADO Ext. for DDL and Security"
选择为2。6的版本。
顺便问一下,这程序执行的速度是不是很快?前面你提供的导入的速度是让人满意的。
2)Select "Microsoft ADO Ext. for DDL and Security"
选择为2。6的版本。
顺便问一下,这程序执行的速度是不是很快?前面你提供的导入的速度是让人满意的。
#11
速度是不成问题的,我好象也是用2。6版本的(有点记不大清楚了),没出现问题。要不你试试2。5版本的看看
#12
我剛在基楚版中回答了這個問題,你找一找,我說了有三種方法.
#13
已经用阿飞提出的第一个方法解决了导入的问题,速度非常令人满意。
导出的问题在另一个帖子找到了解决方法,用剪贴板,一千条记录大概也就是十秒钟不到的时间吧,应该说速度也是令人满意的,阿飞那方法有没有用过?
但我想应该还有更好的办法,例如在sqlserver中导出速度是那么快。
最后谢谢各位对我的帮助!
导出的问题在另一个帖子找到了解决方法,用剪贴板,一千条记录大概也就是十秒钟不到的时间吧,应该说速度也是令人满意的,阿飞那方法有没有用过?
但我想应该还有更好的办法,例如在sqlserver中导出速度是那么快。
最后谢谢各位对我的帮助!
#14
我的导入导出都是用这种方法,很快也很方便!
#15
我也正在做这么个东东,能不能发份源代码给我呀。谢谢啦。如果没有问题的话,我会给分的。
#1
可以设定ADO的连接方式吧?
如果客户是白痴,我一般在Excel里面用VBA写代码导入数据。
你可以在Execl里面按ALT+F11调出编程窗体,用VB代码就可以搞定了。
如果客户是白痴,我一般在Excel里面用VBA写代码导入数据。
你可以在Execl里面按ALT+F11调出编程窗体,用VB代码就可以搞定了。
#2
Excel可以用ODBC或ADO直接连接的,有关设定的方法网上找找,打字的话,字太多,省点劲吧,很好找的
#3
使用如下的方法可以直接调用Excel文件,FileName为文件名,SHeetName为工作薄名(相当于表)
ADOConnection := TADOConnection.Create(nil);
ADOConnection.LoginPrompt := False;
ADOConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0';
ADOQuery := TADOQuery.Create(nil);
ADOQuery.Connection := ADOConnection;
ADOQuery.SQL.Text := 'Select * from [' + SheetName + '$]';
ADOQuery.Open;
With ADOQuery do
begin
First;
while NOT EOF do
begin
// 导入操作的代码
end;
end;
ADOQuery.Close;
ADOConnection.Close;
ADOQuery.Free;
ADOConnection.Free;
ADOConnection := TADOConnection.Create(nil);
ADOConnection.LoginPrompt := False;
ADOConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0';
ADOQuery := TADOQuery.Create(nil);
ADOQuery.Connection := ADOConnection;
ADOQuery.SQL.Text := 'Select * from [' + SheetName + '$]';
ADOQuery.Open;
With ADOQuery do
begin
First;
while NOT EOF do
begin
// 导入操作的代码
end;
end;
ADOQuery.Close;
ADOConnection.Close;
ADOQuery.Free;
ADOConnection.Free;
#4
excel可以用ado直接连接,从一个数据集往另一个数据集写数据就好了
你可以用AdoDataSet控件﹐設置跟AdoConnection一樣。但要在設置欄里面的'全部'工具條
里的Extended Properties項設成Excel 8.0(設是95版本以上)或Excel 5.0。然后在
CommandType項里面選cmdTableDirect﹐不要選CmdTable和CmdTxt﹐選CmdTxt﹐它會說找不
到你要的那個物件﹐就像你上面出的錯那樣。然后再選擇CommandText﹐只要你選好了Excel
文件﹐里面自然就有你要的東東了。最后再將Active設為True﹐就可以了。保証沒有問題。
你可以用AdoDataSet控件﹐設置跟AdoConnection一樣。但要在設置欄里面的'全部'工具條
里的Extended Properties項設成Excel 8.0(設是95版本以上)或Excel 5.0。然后在
CommandType項里面選cmdTableDirect﹐不要選CmdTable和CmdTxt﹐選CmdTxt﹐它會說找不
到你要的那個物件﹐就像你上面出的錯那樣。然后再選擇CommandText﹐只要你選好了Excel
文件﹐里面自然就有你要的東東了。最后再將Active設為True﹐就可以了。保証沒有問題。
#5
为什么将AdoDataSet设置好各项连接属性并选好table后,提示“from 子句语法错误”
#6
不好意思,前几天老是登录不上来,我已经用: afei78223(阿飞) 的方法将此问题解决了,而且比用delphi的server的控件或ole的速度快多了,基本一闪就出来了。
可是如果要用此方法将数据导出该怎么做?我用insert into 的时候老是显示错,是不是表的名字有什么特殊之处?用delphi的server的控件或ole可以导出,但速度太慢了。
能不能给点提示?我要加分。
可是如果要用此方法将数据导出该怎么做?我用insert into 的时候老是显示错,是不是表的名字有什么特殊之处?用delphi的server的控件或ole可以导出,但速度太慢了。
能不能给点提示?我要加分。
#7
导出的代码差不多的,我给一个将DBGrid的内容导出为Excel文件的函数
先执行以下操作:
1)Project->Import Type Library:
2)Select "Microsoft ADO Ext. for DDL and Security"
3)重新命名类的名称(TTable, TColumn, TIndex, TKey, TGroup, TUser, TCatalog) in
(TXTable, TXColumn, TXIndex, TXKey, TXGroup, TXUser, TXCatalog)
4)创建一个单元,名字为ADOX_TLB
5)在你自己的单元里Uses ADOX_TLB单元
unit DBGridExportToExcel;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, ComCtrls, DB, IniFiles, Buttons, dbgrids, ADOX_TLB, ADODB;
type TScrollEvents = class
BeforeScroll_Event: TDataSetNotifyEvent;
AfterScroll_Event: TDataSetNotifyEvent;
AutoCalcFields_Property: Boolean;
end;
procedure DisableDependencies(DataSet: TDataSet; var ScrollEvents: TScrollEvents);
procedure EnableDependencies(DataSet: TDataSet; ScrollEvents: TScrollEvents);
procedure DBGridToExcelADO(DBGrid: TDBGrid; FileName: string; SheetName: string);
implementation
//Support procedures: I made that in order to increase speed in
//the process of scanning large amounts
//of records in a dataset
//we make a call to the "DisableControls" procedure and then disable the "BeforeScroll" and
//"AfterScroll" events and the "AutoCalcFields" property.
procedure DisableDependencies(DataSet: TDataSet; var ScrollEvents: TScrollEvents);
begin
with DataSet do
begin
DisableControls;
ScrollEvents := TScrollEvents.Create();
with ScrollEvents do
begin
BeforeScroll_Event := BeforeScroll;
AfterScroll_Event := AfterScroll;
AutoCalcFields_Property := AutoCalcFields;
BeforeScroll := nil;
AfterScroll := nil;
AutoCalcFields := False;
end;
end;
end;
//we make a call to the "EnableControls" procedure and then restore
// the "BeforeScroll" and "AfterScroll" events and the "AutoCalcFields" property.
procedure EnableDependencies(DataSet: TDataSet; ScrollEvents: TScrollEvents);
begin
with DataSet do
begin
EnableControls;
with ScrollEvents do
begin
BeforeScroll := BeforeScroll_Event;
AfterScroll := AfterScroll_Event;
AutoCalcFields := AutoCalcFields_Property;
end;
end;
end;
//This is the procedure which make the work:
procedure DBGridToExcelADO(DBGrid: TDBGrid; FileName: string; SheetName: string);
var
cat: _Catalog;
tbl: _Table;
col: _Column;
i: integer;
ADOConnection: TADOConnection;
ADOQuery: TADOQuery;
ScrollEvents: TScrollEvents;
SavePlace: TBookmark;
begin
//
//WorkBook creation (database)
cat := CoCatalog.Create;
cat._Set_ActiveConnection('Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0');
//WorkSheet creation (table)
tbl := CoTable.Create;
tbl.Set_Name(SheetName);
//Columns creation (fields)
DBGrid.DataSource.DataSet.First;
with DBGrid.Columns do
begin
for i := 0 to Count - 1 do
if Items[i].Visible then
begin
col := nil;
col := CoColumn.Create;
with col do
begin
Set_Name(Items[i].Title.Caption);
Set_Type_(adVarWChar);
end;
//add column to table
tbl.Columns.Append(col, adVarWChar, 20);
end;
end;
//add table to database
cat.Tables.Append(tbl);
col := nil;
tbl := nil;
cat := nil;
//exporting
ADOConnection := TADOConnection.Create(nil);
ADOConnection.LoginPrompt := False;
ADOConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0';
ADOQuery := TADOQuery.Create(nil);
ADOQuery.Connection := ADOConnection;
ADOQuery.SQL.Text := 'Select * from [' + SheetName + '$]';
ADOQuery.Open;
DisableDependencies(DBGrid.DataSource.DataSet, ScrollEvents);
SavePlace := DBGrid.DataSource.DataSet.GetBookmark;
try
with DBGrid.DataSource.DataSet do
begin
First;
while not Eof do
begin
ADOQuery.Append;
with DBGrid.Columns do
begin
ADOQuery.Edit;
for i := 0 to Count - 1 do
if Items[i].Visible then
begin
ADOQuery.FieldByName(Items[i].Title.Caption).AsString := FieldByName(Items[i].FieldName).AsString;
end;
ADOQuery.Post;
end;
Next;
end;
end;
finally
DBGrid.DataSource.DataSet.GotoBookmark(SavePlace);
DBGrid.DataSource.DataSet.FreeBookmark(SavePlace);
EnableDependencies(DBGrid.DataSource.DataSet, ScrollEvents);
ADOQuery.Close;
ADOConnection.Close;
ADOQuery.Free;
ADOConnection.Free;
end;
end;
end.
先执行以下操作:
1)Project->Import Type Library:
2)Select "Microsoft ADO Ext. for DDL and Security"
3)重新命名类的名称(TTable, TColumn, TIndex, TKey, TGroup, TUser, TCatalog) in
(TXTable, TXColumn, TXIndex, TXKey, TXGroup, TXUser, TXCatalog)
4)创建一个单元,名字为ADOX_TLB
5)在你自己的单元里Uses ADOX_TLB单元
unit DBGridExportToExcel;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls, StdCtrls, ComCtrls, DB, IniFiles, Buttons, dbgrids, ADOX_TLB, ADODB;
type TScrollEvents = class
BeforeScroll_Event: TDataSetNotifyEvent;
AfterScroll_Event: TDataSetNotifyEvent;
AutoCalcFields_Property: Boolean;
end;
procedure DisableDependencies(DataSet: TDataSet; var ScrollEvents: TScrollEvents);
procedure EnableDependencies(DataSet: TDataSet; ScrollEvents: TScrollEvents);
procedure DBGridToExcelADO(DBGrid: TDBGrid; FileName: string; SheetName: string);
implementation
//Support procedures: I made that in order to increase speed in
//the process of scanning large amounts
//of records in a dataset
//we make a call to the "DisableControls" procedure and then disable the "BeforeScroll" and
//"AfterScroll" events and the "AutoCalcFields" property.
procedure DisableDependencies(DataSet: TDataSet; var ScrollEvents: TScrollEvents);
begin
with DataSet do
begin
DisableControls;
ScrollEvents := TScrollEvents.Create();
with ScrollEvents do
begin
BeforeScroll_Event := BeforeScroll;
AfterScroll_Event := AfterScroll;
AutoCalcFields_Property := AutoCalcFields;
BeforeScroll := nil;
AfterScroll := nil;
AutoCalcFields := False;
end;
end;
end;
//we make a call to the "EnableControls" procedure and then restore
// the "BeforeScroll" and "AfterScroll" events and the "AutoCalcFields" property.
procedure EnableDependencies(DataSet: TDataSet; ScrollEvents: TScrollEvents);
begin
with DataSet do
begin
EnableControls;
with ScrollEvents do
begin
BeforeScroll := BeforeScroll_Event;
AfterScroll := AfterScroll_Event;
AutoCalcFields := AutoCalcFields_Property;
end;
end;
end;
//This is the procedure which make the work:
procedure DBGridToExcelADO(DBGrid: TDBGrid; FileName: string; SheetName: string);
var
cat: _Catalog;
tbl: _Table;
col: _Column;
i: integer;
ADOConnection: TADOConnection;
ADOQuery: TADOQuery;
ScrollEvents: TScrollEvents;
SavePlace: TBookmark;
begin
//
//WorkBook creation (database)
cat := CoCatalog.Create;
cat._Set_ActiveConnection('Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0');
//WorkSheet creation (table)
tbl := CoTable.Create;
tbl.Set_Name(SheetName);
//Columns creation (fields)
DBGrid.DataSource.DataSet.First;
with DBGrid.Columns do
begin
for i := 0 to Count - 1 do
if Items[i].Visible then
begin
col := nil;
col := CoColumn.Create;
with col do
begin
Set_Name(Items[i].Title.Caption);
Set_Type_(adVarWChar);
end;
//add column to table
tbl.Columns.Append(col, adVarWChar, 20);
end;
end;
//add table to database
cat.Tables.Append(tbl);
col := nil;
tbl := nil;
cat := nil;
//exporting
ADOConnection := TADOConnection.Create(nil);
ADOConnection.LoginPrompt := False;
ADOConnection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0';
ADOQuery := TADOQuery.Create(nil);
ADOQuery.Connection := ADOConnection;
ADOQuery.SQL.Text := 'Select * from [' + SheetName + '$]';
ADOQuery.Open;
DisableDependencies(DBGrid.DataSource.DataSet, ScrollEvents);
SavePlace := DBGrid.DataSource.DataSet.GetBookmark;
try
with DBGrid.DataSource.DataSet do
begin
First;
while not Eof do
begin
ADOQuery.Append;
with DBGrid.Columns do
begin
ADOQuery.Edit;
for i := 0 to Count - 1 do
if Items[i].Visible then
begin
ADOQuery.FieldByName(Items[i].Title.Caption).AsString := FieldByName(Items[i].FieldName).AsString;
end;
ADOQuery.Post;
end;
Next;
end;
end;
finally
DBGrid.DataSource.DataSet.GotoBookmark(SavePlace);
DBGrid.DataSource.DataSet.FreeBookmark(SavePlace);
EnableDependencies(DBGrid.DataSource.DataSet, ScrollEvents);
ADOQuery.Close;
ADOConnection.Close;
ADOQuery.Free;
ADOConnection.Free;
end;
end;
end.
#8
78:cat._Set_ActiveConnection('Provider=Microsoft.Jet.OLEDB.4.0; Data Source=' + FileName + ';Extended Properties=Excel 8.0');
[Error] DBGridExportToExcel.pas(78): Incompatible types: 'String' and 'IDispatch'
[Error] DBGridExportToExcel.pas(78): Incompatible types: 'String' and 'IDispatch'
#9
我在自己的程序里用过的没有错。你有没有先操作1,2,3,4,5的这几个步骤?
#10
有只是我将
2)Select "Microsoft ADO Ext. for DDL and Security"
选择为2。6的版本。
顺便问一下,这程序执行的速度是不是很快?前面你提供的导入的速度是让人满意的。
2)Select "Microsoft ADO Ext. for DDL and Security"
选择为2。6的版本。
顺便问一下,这程序执行的速度是不是很快?前面你提供的导入的速度是让人满意的。
#11
速度是不成问题的,我好象也是用2。6版本的(有点记不大清楚了),没出现问题。要不你试试2。5版本的看看
#12
我剛在基楚版中回答了這個問題,你找一找,我說了有三種方法.
#13
已经用阿飞提出的第一个方法解决了导入的问题,速度非常令人满意。
导出的问题在另一个帖子找到了解决方法,用剪贴板,一千条记录大概也就是十秒钟不到的时间吧,应该说速度也是令人满意的,阿飞那方法有没有用过?
但我想应该还有更好的办法,例如在sqlserver中导出速度是那么快。
最后谢谢各位对我的帮助!
导出的问题在另一个帖子找到了解决方法,用剪贴板,一千条记录大概也就是十秒钟不到的时间吧,应该说速度也是令人满意的,阿飞那方法有没有用过?
但我想应该还有更好的办法,例如在sqlserver中导出速度是那么快。
最后谢谢各位对我的帮助!
#14
我的导入导出都是用这种方法,很快也很方便!
#15
我也正在做这么个东东,能不能发份源代码给我呀。谢谢啦。如果没有问题的话,我会给分的。