Delphi ADOConnection1 连接excel 读取数据


本文整理自网络,侵删。

 
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Data.DB, Data.Win.ADODB,
  Vcl.Grids, Vcl.DBGrids;

type
  TForm1 = class(TForm)
    OpenDialog1: TOpenDialog;
    Button1: TButton;
    Edit1: TEdit;
    ADOConnection1: TADOConnection;
    ComboBox1: TComboBox;
    ADOQuery1: TADOQuery;
    Memo1: TMemo;
    Button2: TButton;
    DataSource1: TDataSource;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  fn:string;
begin
  if OpenDialog1.Execute then
  begin
    Edit1.Text := OpenDialog1.FileName;
    fn := Edit1.Text;
    Screen.Cursor := crHourGlass;
    try
      ADOConnection1.Close;
      ADOConnection1.ConnectionString := 'Provider=Microsoft.ACE.OLEDB.12.0;Data Source='+fn+';Extended Properties= Excel 12.0;Persist Security Info=True';
      ADOConnection1.Connected := True;
      ADOConnection1.GetTableNames(ComboBox1.Items);




    finally
      Screen.Cursor := crDefault;
    end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
begin
    ADOConnection1.LoginPrompt:=false;
    ADOConnection1.Connected:=true;
    ADOQuery1.Connection:=ADOConnection1;
    ADOQuery1.close;
    ADOQuery1.SQL.Clear;
    ADOQuery1.SQL.add('select * from ['+ComboBox1.Text+']');
    ADOQuery1.Active:=true;


     try
    while not ADOQuery1.Eof do
    begin

      for i := 0 to ADOQuery1.FieldCount -1 do
      begin
        memo1.Lines.Add(ADOQuery1.Fields[i].AsString);
      end;

      ADOQuery1.Next;
    end;
  finally

  end;



end;

end.

相关阅读 >>

推荐一套免费跨平台的Delphi 哈希及加密算法库

Delphi 获取网页代码单元

Delphi android实例-读取设备联系人(xe8+小米2)

Delphi url 中文编解码

Delphi fdquery 判断记录为空

Delphi判断字符串是否是汉字

Delphi 中文字符串截取

Delphi中禁止webbrowser右键的方法

Delphi中关于canvas.textout的用法

Delphi 转换一批.bmp 文件为 .jpg

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



打赏

取消

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

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

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

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

评论

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