這是我回答過的一個,希望對你有幫助.
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//把Excel文件當成一個數據庫,
//把文件裏的Sheet1,Sheet2,Sheet3...當成是數據庫裏的表
//這樣你就操作自如了,
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DB, ADODB, Grids, DBGrids, Buttons, ExtCtrls;
type
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
OpenDialog1: TOpenDialog;
ListBox1: TListBox;
Panel1: TPanel;
Button1: TButton;
Edit1: TEdit;
SpeedButton1: TSpeedButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure SpeedButton1Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Edit1.Text:= OpenDialog1.FileName;
end;
//ADOConnection1,ADOQuery1,DataSource1,DBGrid1, ListBox1
procedure TForm1.Button1Click(Sender: TObject);
var
ConnStr, FileName: string;
i: integer;
begin
FileName:= Edit1.Text; //Excel File Name
ConnStr:= ''+
' Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin; ' +
' Data Source=' + FileName + ';Mode=Share Deny None;Extended Properties=Excel 8.0; ' +
' Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDB
atabase Password=""; ' +
' Jet OLEDB:Engine Type=35;Jet OLEDB
atabase Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2; ' +
' Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB: ' +
' Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDB
on''t Copy Locale on Compact=False; ' +
' Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False ';
ADOConnection1.Connected:= false;
ADOConnection1.ConnectionString:= ConnStr;
ADOConnection1.Connected:= true;
ListBox1.Clear;
ADOConnection1.GetTableNames(ListBox1.Items);
if ListBox1.Items.Count>0 then
begin
ListBox1.Selected[0]:= true;
ListBox1.ItemIndex:= 0;
ListBox1.OnClick(ListBox1);
end;
end;
procedure TForm1.ListBox1Click(Sender: TObject);
var
i: integer;
begin
ADOQuery1.Close;
if ListBox1.Items.Count<=0 then exit;
ADOQuery1.SQL.Text:= 'select * from ' + '['+ListBox1.Items.Strings[ListBox1.ItemIndex]+']';
ADOQuery1.Open;
for i:= 0 to DBGrid1.Columns.Count - 1 do
DBGrid1.Columns
.Width:= 80;
Label2.Caption:=
' TableCount: '+ IntToStr(ListBox1.Count)+
' CurrentTableName: '+ ListBox1.Items.Strings[ListBox1.ItemIndex]+
' CurrentTableRecordCount: '+ IntToStr(ADOQuery1.RecordCount);
end;
end.
procedure TForm1.Button1Click(Sender: TObject);
var
ConnStr, FileName, TableName: string;
begin
FileName:= 'F:/Excel1.xls';
TableName:= '[Sheet1$]';
ConnStr:= ''+
' Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=Admin; ' +
' Data Source=' + FileName + ';Mode=Share Deny None;Extended Properties=Excel 8.0; ' +
' Jet OLEDB:System database="";Jet OLEDB:Registry Path="";Jet OLEDBatabase Password=""; ' +
' Jet OLEDB:Engine Type=35;Jet OLEDBatabase Locking Mode=0;Jet OLEDB:Global Partial Bulk Ops=2; ' +
' Jet OLEDB:Global Bulk Transactions=1;Jet OLEDB:New Database Password="";Jet OLEDB: ' +
' Create System Database=False;Jet OLEDB:Encrypt Database=False;Jet OLEDBon''t Copy Locale on Compact=False; ' +
' Jet OLEDB:Compact Without Replica Repair=False;Jet OLEDB:SFP=False ';
ADOConnection1.Connected:= false;
ADOConnection1.ConnectionString:= ConnStr;
ADOConnection1.Connected:= true;
ADOQuery1.Close;
ADOQuery1.SQL.Text:= 'select * from ' + TableName;
ADOQuery1.Open;
end;