本人想通过在EDIT栏中导入任意一个EXCEL表的地址,让该表数据在DBGrid 中显示出来,但是操作失败,要两次选择EXCEL文件,之后弹出“对象打开时,不允许操作的对话框”提示错误。具体代码如下,请教哪位热心大侠能指点一二,在下不胜感激。
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
OpenDialog1: TOpenDialog;
ADOConnection1: TADOConnection;
DBGrid1: TDBGrid;
ADOTable1: TADOTable;
DataSource1: TDataSource;
procedure Button1Click(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
pathstr: string;
implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
opendialog1.filename:='*.xls';
if opendialog1.Execute then
begin
pathstr:=opendialog1.FileName;
edit1.Text:=pathstr;
Adoconnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;'
+'Password="";Data Source='''+pathstr+ ''';'
+ 'Extended Properties=Excel 8.0;Persist Security Info=false';
ADOConnection1.connected:=true;
ADOTable1.TableName:=pathstr;
ADOTable1.active:=true;
end;
end;
end.
type
TForm1 = class(TForm)
Label1: TLabel;
Edit1: TEdit;
Button1: TButton;
OpenDialog1: TOpenDialog;
ADOConnection1: TADOConnection;
DBGrid1: TDBGrid;
ADOTable1: TADOTable;
DataSource1: TDataSource;
procedure Button1Click(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
pathstr: string;
implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
opendialog1.filename:='*.xls';
if opendialog1.Execute then
begin
pathstr:=opendialog1.FileName;
edit1.Text:=pathstr;
Adoconnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;'
+'Password="";Data Source='''+pathstr+ ''';'
+ 'Extended Properties=Excel 8.0;Persist Security Info=false';
ADOConnection1.connected:=true;
ADOTable1.TableName:=pathstr;
ADOTable1.active:=true;
end;
end;
end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货