How to dynamically link Excel format database in Delphi

Source: Internet
Author: User

Generally two-step, first determine the XLS file (with open dialog can be implemented), and then find the corresponding page (make a loop, and then write a ComboBox)
Uses EXCEL97, (EXCELXP,) Excel2000//Note Order EXCELXP Auto Plus
To add a control:
Excelapplication1:texcelapplication;
Excelworksheet1:texcelworksheet;
Excelworkbook1:texcelworkbook;
Edit1:tedit;
Opendialog1:topendialog;
Combobox1:tcombobox;
Button1:tbutton;

Procedure Tmainf.button1click (Sender:tobject);//Open Excel file
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 (), ' Operation failed! ', ' warning ', mb_iconwarning);
Abort
End

End
End

Select the appropriate page name and open
Procedure Tmainf.combobox1select (Sender:tobject);
Var
I:byte;
Begin
With ADOQuery1 do//depending on your data connection control
Try

Close;
Sql. Clear;
Parameters.clear;
Paramcheck: = false;
Sql. ADD (' SELECT * from [Excel 8.0;database= ' + edit1.text + '].[' + Combobox1.text + ' $] ');
Open

Except
MessageBox (GetActiveWindow (), ' Open Excel File page name failed! ', ' warning ', MB_OK + mb_iconwarning);
End
End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.