delphi ADOConnection1 连接excel表格读取数据


本文整理自网络,侵删。

 
uses ComObj;

procedure TForm1.Button6Click(Sender: TObject);
var
  Excel: OLEVariant;
  ExcelVersion: string;
  filename:string;
begin

filename:=ExtractFilePath(ParamStr(0))+'工作表.xlsx';
   try
      Excel := CreateOLEObject('EXCEL.Application');
      ExcelVersion := Excel.version;
   finally
      Excel.Quit;
      Excel := UnAssigned;
   end;
  ADOConnection1.Close;

  if ExcelVersion = '11.0' then
    //Excel2003及早期的版本
     ADOConnection1.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+
                            filename+';Extended Properties=excel 8.0;'+
                            'Persist Security Info=false;'
  else //Excel2007及以后的版本
     ADOConnection1.ConnectionString:='Provider=Microsoft.ACE.OLEDB.12.0;Data Source='+
                            filename+';Extended Properties=excel 12.0;'+
                             'Persist Security Info=True';//注意不能为false


    ADOConnection1.LoginPrompt:=false;
    ADOConnection1.Connected:=true;
    ADOQuery1.Connection:=ADOConnection1;
    ADOQuery1.close;
    ADOQuery1.SQL.Clear;
    ADOQuery1.SQL.add('select * from [sheet1$]');
    ADOQuery1.Active:=true;


end;



Delphi Excel写入 ADOConnection
var
  sFileName: string;
begin
  sFileName := ExtractFilePath(Application.ExeName) + 'Book1.xls';
  con1.Connected := False;
  con1.ConnectionString:= 'Provider=Microsoft.Jet.OLEDB.4.0;Password="";Data Source=' + sFileName + ';Extended Properties=Excel 8.0;Persist Security Info=True';;
  con1.Connected := true;
  con1.Execute('create table Test(name varchar(20))');
  con1.Execute('insert into Test values (''AName'')');
end;

相关阅读 >>

Delphi android gps服务

Delphi中用idhttp获取json数据乱码问题

Delphi 获取系统mydocuments文件夹路径

Delphi 输出用空格对齐字符串的函数

Delphi 搜索指定目录下的文件

Delphi fmx手机app,如何下载网站图片而不卡界面

Delphi 不重启不注销不关explorer刷新注册表

Delphi中inputbox 和inputquery 函数的使用

Delphi提取exe,dll文件的icon图标

Delphi 简单的旋转图像角度代码

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



打赏

取消

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

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

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

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

评论

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