delphi中如何动态链接excel
我用adoconnection和adoquery和DBgrid成功的连接和显示了excel文件。现在我想用动态链接的方法,就是用一个按钮,通过点击按钮查找需要的excel...
我用adoconnection和adoquery和DBgrid成功的连接和显示了excel文件。
现在我想用动态链接的方法,就是用一个按钮,通过点击按钮查找需要的excel文件然后直接导入到DBgrid显示出来。 展开
现在我想用动态链接的方法,就是用一个按钮,通过点击按钮查找需要的excel文件然后直接导入到DBgrid显示出来。 展开
1个回答
展开全部
一般要两步,首先确定XLS文件(用打开对话框可以实现),再找到对应的页(作一个循环,再写入一个COMBOBOX)
uses Excel97,(ExcelXP,)Excel2000 //注意顺序ExcelXP自动加
添加控件:
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
Edit1: TEdit;
OpenDialog1: TOpenDialog;
ComboBox1: TComboBox;
Button1: TButton;
procedure TMainF.Button1Click(Sender: TObject);//打开EXCEL文件
var
i: Integer;
begin
if OpenDialog1.Execute then
begin
Edit1.Text := OpenDialog1.FileName;
try
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.open(Edit1.Text, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, 0));
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Activesheet as _Worksheet);
ComboBox1.Clear;
for i := 1 to ExcelWorkbook1.Worksheets.Count do
begin
ComboBox1.Items.Add((ExcelWorkbook1.Worksheets.Item[i] as _Worksheet).Name);
end;
ExcelApplication1.Quit;
ExcelWorksheet1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;
except;
MessageBox(GetActiveWindow(), '操作失败!', '警告', mb_iconwarning);
abort;
end;
end;
end;
//选择相应页名并打开
procedure TMainF.ComboBox1Select(Sender: TObject);
var
i: byte;
begin
with ADOQuery1 do //根据自己的数据连接控件而定
try
Close;
sql.Clear;
Parameters.Clear;
ParamCheck := false;
sql.Add('SELECT * FROM [excel 8.0;database=' + Edit1.Text + '].[' + ComboBox1.Text + '$]');
open;
except
MessageBox(GetActiveWindow(), '打开Excel文件页名失败!', '警告', MB_OK + mb_iconwarning);
end;
end;
uses Excel97,(ExcelXP,)Excel2000 //注意顺序ExcelXP自动加
添加控件:
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
Edit1: TEdit;
OpenDialog1: TOpenDialog;
ComboBox1: TComboBox;
Button1: TButton;
procedure TMainF.Button1Click(Sender: TObject);//打开EXCEL文件
var
i: Integer;
begin
if OpenDialog1.Execute then
begin
Edit1.Text := OpenDialog1.FileName;
try
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.open(Edit1.Text, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, 0));
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Activesheet as _Worksheet);
ComboBox1.Clear;
for i := 1 to ExcelWorkbook1.Worksheets.Count do
begin
ComboBox1.Items.Add((ExcelWorkbook1.Worksheets.Item[i] as _Worksheet).Name);
end;
ExcelApplication1.Quit;
ExcelWorksheet1.Disconnect;
ExcelWorkbook1.Disconnect;
ExcelApplication1.Disconnect;
except;
MessageBox(GetActiveWindow(), '操作失败!', '警告', mb_iconwarning);
abort;
end;
end;
end;
//选择相应页名并打开
procedure TMainF.ComboBox1Select(Sender: TObject);
var
i: byte;
begin
with ADOQuery1 do //根据自己的数据连接控件而定
try
Close;
sql.Clear;
Parameters.Clear;
ParamCheck := false;
sql.Add('SELECT * FROM [excel 8.0;database=' + Edit1.Text + '].[' + ComboBox1.Text + '$]');
open;
except
MessageBox(GetActiveWindow(), '打开Excel文件页名失败!', '警告', MB_OK + mb_iconwarning);
end;
end;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询