unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleServer, Excel2000,
DB, DBTables, Grids, DBGrids, StdCtrls, ADODB,
ExtCtrls, DBCtrls;
type
TForm1 =
class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
ADOConnection1: TADOConnection;
DBGrid1: TDBGrid;
DataSource1: TDataSource;
ListBox1: TListBox;
ADODataSet1: TADODataSet;
DBNavigator1: TDBNavigator;
Button2: TButton;
ADOCommand1: TADOCommand;
Button3: TButton;
Edit1: TEdit;
procedure Button3Click(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
Tabellenname:
string;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var i: integer;
begin
if ADOConnection1.Connected
then ADOConnection1.Close;
if OpenDialog1.Execute
then
begin
ADOConnection1.ConnectionString :=
'
Provider=Microsoft.Jet.OLEDB.4.0;' +
'
Data Source='+ OpenDialog1.FileName +'
;' +
'
Extended Properties="Excel 8.0;HDR=Yes";' +
'
Persist Security Info=False';
ADOConnection1.Open;
if ADOConnection1.Connected
then
begin
ADOConnection1.GetTableNames(ListBox1.Items, False);
Tabellenname:= ListBox1.Items.Strings[0];
ADODataSet1.CommandText:= '
SELECT * FROM [' + Tabellenname + '
]';
ADODataSet1.Open;
for i:= 0
to ADODataSet1.FieldCount - 1
do
begin
DBGrid1.Columns[i].Width:=
DBGrid1.Width
div ADODataSet1.FieldCount;
end;
end;
end;
end;
procedure TForm1.ListBox1Click(Sender: TObject);
var i: integer;
begin
if ADODataSet1.Active
then ADODataSet1.Close;
Tabellenname:= ListBox1.Items.Strings[ListBox1.ItemIndex];
ADODataSet1.CommandText:= '
SELECT * FROM [' + Tabellenname + '
]';
ADODataSet1.Open;
for i:= 0
to ADODataSet1.FieldCount - 1
do
begin
DBGrid1.Columns[i].Width:= DBGrid1.Width
div ADODataSet1.FieldCount;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
with ADODataSet1
do
begin
if Active
and (State
in [dsEdit])
then Close;
Close;
end;
ListBox1.Items.Clear;
end;
procedure TForm1.FormResize(Sender: TObject);
var i: integer;
begin
if ADODataSet1.Active
then
for i:= 0
to ADODataSet1.FieldCount - 1
do
begin
DBGrid1.Columns[i].Width:= DBGrid1.Width
div
ADODataSet1.FieldCount;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
if ADODataSet1.Active
and (Edit1.Text <> '
')
then
begin
ADOCommand1.CommandText:= '
ALTER TABLE [' + Tabellenname +
'
] ADD ' + Edit1.Text +
'
char(20)';
ADOCommand1.Execute;
end;
end;
end.