- 人气:
- 放大
- 缩小
- 二维码
- 赞赏
delphi ADO 连接 Excel (附Excel各个版本的版本号)
引用ComObj这个单元
Type
Conn: TADOConnection;
qry: TADOQuery;
var
Excel: OLEVariant;
ExcelVersion: string;
begin
try
Excel := CreateOLEObject('EXCEL.Application');
ExcelVersion := Excel.version;
finally
Excel.Quit;
Excel := UnAssigned;
end;
Conn.Close;
if ExcelVersion = '11.0' then
//Excel2003及早期的版本
Conn.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+
filename+';Extended Properties=excel 8.0;'+
'Persist Security Info=false;'
else //Excel2007及以后的版本
Conn.ConnectionString:='Provider=Microsoft.ACE.OLEDB.12.0;Data Source='+
filename+';Extended Properties=excel 12.0;'+
'Persist Security Info=True';//注意不能为false
Conn.LoginPrompt:=false;
Conn.Connected:=true;
qry.Connection:=Conn;
qry.close;
qry.SQL.Clear;
qry.SQL.add('select * from ['+sheet1+'$]');
qry.Active:=true;
end;
附上各个EXCEL版本的版本号:
版本号 Excel名称
2.0 Excel 2.0
3.0 Excel 3.0
4.0 Excel 4.0
5.0 Excel 5.0
7.0 Excel 95
8.0 Excel 97
9.0 Excel 2000
10.0 Excel XP
11.0 Excel 2003
12.0 Excel 2007
13.0 Excel 2010
15.0 Excel 2013
————————————————
原文链接:https://blog.csdn.net/songling418/article/details/14054359