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

相关阅读 >>

Delphi按下esc键就退出程序

Delphi tnethttpclient演示

Delphi 调用dll运行正常,退出时弹出错误解决办法

Delphi获取其它进程窗口句柄的三种方法

Delphi时间相减函数

Delphi xe 在andriod程序中获取外置sd卡根目录

Delphi tstrings 随机打乱

Delphi xe 如何实现("再按一次退出") 然后退出程序

Delphi 分离网址链接最后一层文件目录

Delphi编程用流来读取txt文件中的数据

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...