一般要两步,首先确定XLS文件(用打开对话框可以实现),再找到对应的页(作一个循环,再写入一个COMBOBOX)uses Excel97,(ExcelXP,)Excel2000 //注意顺序ExcelXP自动加添加控件: ExcelApplication1: TExcelApplication; ExcelWorksheet1: TExcelWorksheet; ExcelWorkbook1: TExcelWorkbook; Edit1: TEdit; OpenDialog1: TOpenDialog; ComboBox1: TComboBox; Button1: TButton; procedure TMainF.Button1Click(Sender: TObject);//打开EXCEL文件var i: Integer;begin if OpenDialog1.Execute then begin Edit1.Text := OpenDialog1.FileName; try ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.open(Edit1.Text, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, Null, 0)); ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Activesheet as _Worksheet); ComboBox1.Clear; for i := 1 to ExcelWorkbook1.Worksheets.Count do begin ComboBox1.Items.Add((ExcelWorkbook1.Worksheets.Item[i] as _Worksheet).Name); end; ExcelApplication1.Quit; ExcelWorksheet1.Disconnect; ExcelWorkbook1.Disconnect; ExcelApplication1.Disconnect; except; MessageBox(GetActiveWindow(), ‘操作失败!‘, ‘警告‘, mb_iconwarning); abort; end; end;end; //选择相应页名并打开procedure TMainF.ComboBox1Select(Sender: TObject);var i: byte;begin with ADOQuery1 do //根据自己的数据连接控件而定 try Close; sql.Clear; Parameters.Clear; ParamCheck := false; sql.Add(‘SELECT * FROM [excel 8.0;database=‘ + Edit1.Text + ‘].[‘ + ComboBox1.Text + ‘$]‘); open; except MessageBox(GetActiveWindow(), ‘打开Excel文件页名失败!‘, ‘警告‘, MB_OK + mb_iconwarning); end;end;
delphi中如何动态链接excel格式数据库
时间: 2024-11-05 12:35:35