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 datasnap使用ipv6

Delphi 关于字符串, 之前没这样用过

Delphi工具之tdump

Delphi 获取网页源代码的最简单办法

Delphi unigui unistringgrid1 清空

使用串口模拟工具进行串口程序开发调试

Delphi tstringlist的delimitedtext的空格问题

Delphi 窗体的位置和高宽度-tform:letf、top、width、height、clientwidth、clientheight

webbrowser点击没有id和name的按钮或链接

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



打赏

取消

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

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

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

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

评论

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

    正在狠努力加载,请稍候...