procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
begin
ComboBox1.Clear;
OpenDialog1.Filter := '*.XLS|*.XLS';
OpenDialog1.DefaultExt := 'XLS';
if OpenDialog1.Execute then
begin
try //创建文件
ExcelApplication1.Connect;
except
Application.Messagebox('Excel 没有安装!', 'Hello', MB_ICONERROR +
mb_Ok);
Abort;
end;
Filename := OpenDialog1.FileName;
ExcelApplication1.Visible[0] := false;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Open
(Filename, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, 0));
for i := 1 to ExcelWorkbook1.Worksheets.Count do
begin
ComboBox1.Items.Add((ExcelWorkbook1.Worksheets.Item
as _WorkSheet).Name);
end;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i,j: Integer;
row, col : Integer;
begin
ExcelWorkSheet1.ConnectTo(ExcelWorkbook1.Worksheets.Item[ComboBox1.ItemIndex + 1] as _WorkSheet);
ExcelWorkSheet1.Activate;
Edit1.Text:=ExcelWorksheet1.Cells.Item[4,2];
//如果要循环读取到最末的话
col := ExcelWorksheet1.UsedRange[0].Cells.Columns.Count;
row := ExcelWorksheet1.UsedRange[0].Cells.rows.Count;
for I := 2 to col do
begin
Application.ProcessMessages;
for J := 4 to row do
begin
edit1.text:= ExcelWorksheet1.Cells.Item[i, j];//如果你要数据库赋值什么的,也是这么做;
end;
end;
end;