码迷,mamicode.com
首页 > 数据库 > 详细

delphi中如何动态链接excel格式数据库

时间:2014-06-20 22:36:44      阅读:335      评论:0      收藏:0      [点我收藏+]

标签:class   ext   com   get   文件   数据   

一般要两步,首先确定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;

delphi中如何动态链接excel格式数据库,布布扣,bubuko.com

delphi中如何动态链接excel格式数据库

标签:class   ext   com   get   文件   数据   

原文地址:http://www.cnblogs.com/azhqiang/p/3795862.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!