delphi ADO 连接 Excel (附Excel各个版本的版本号)  
官方Delphi 学习QQ群: 682628230(三千人)
频道

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

推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.087743997573853 seconds