Use Aadoconnection to connect excel in Delphi7 today

Source: Internet
Author: User

today in Delphi using ADO to connect Excel (previously read in a book, review the-_-!!), some gains, dare not exclusive. The first part: 1. Set the ADOConnection ConnectionString property of the OLE DB provider to select Microsoft Jet 4.0 OLE Dbprovider (which was originally used to connect to the Access database driver, But you can also open the Excel file), press "Next" button 2. Select the database name. Note: Excel's extension is *.xls, and the default file type is Microsoft Access database (*.mdb), we choose "All files (* *)" bar, then select the Excel file we want to connect!! If it is in the current path, fill in the 2007.xls directly. Just don't panic. Press the test Connection button, or the error dialog box appears. You can try it and see ^_^ 3. In the All tab, locate Extended properties, double-click it, or press the Edit Value button to set the Extended properties to Excel 8.0. 4. We return to the "Connection" tab and press the "Test Connection" button. Test connection Success!!! ^_^ Finally, the parameters in the ConnectionString after Setup are:

--2003 and version
' Provider=Microsoft.Jet.OLEDB.4.0;Data source=%s; Extended Properties=excel 8.0 '
--2007
' Provider=microsoft.ace.oledb.12.0;data source=%s; Extended properties= Excel 12.0; Persist Security info=true ';

%s represents the path to the file

Persist Security info=false Part II: Then use Adotable,adodataset and adoquery to connect to just the adoconnection, of course you can set adotable directly, The ConnectionString property of the Adodataset or adoquery. 1. Adotable settings: A. Adotable TableName Property Value: The system automatically generates Excel sheet1$,sheet2$ and sheet3$. Direct active is set to true, and the system reports information such as malformed SQL statements. B. Locate the TableDirect property, set to True. Because access to an Excel file is direct data file access, it is not accessed through SQL statements that manipulate cursors. Then set the active to true. It worked!! ^_^ C. You can change the sheet1$,sheet2$ and sheet3$ in the TableName property to [sheet1$],[sheet2$] and [sheet3$] without using the TableDirect property. Yes, just add "[]" on the line. 2. Adodataset settings: A. Just set the CommandType property to Cmdtabledirect, select the worksheet, and the active setting to true succeeds. B. You can also set the CommandType property to Cmdtable and change the CommandText property to [sheet1$],[sheet2$] and [sheet3$]. 3. Adoquery settings: SQL properties are as follows:

SELECT * FROM [sheet1$] Summary: Review previous studies, try a new connection method, beneficial to deepen memory. I find that the name of the worksheet is appended with a $ sign, and after opening Adotable,adodataset or adoquery, their cursortype automatically becomes ctstatic.


Get the Excel version

[Delphi]
  1. {
  2. Const
  3. Wordversion97 = 8;
  4. Wordversion2000 = 9;
  5. WORDVERSIONXP = 10;
  6. Wordversion2003 = 11;
  7. Wordversion2007 = 12;
  8. }
  9. function Getinstalledwordversion:integer;
  10. Var
  11. Excel:olevariant;
  12. Begin
  13. Try
  14. Excel:=createoleobject (' Excel.Application ');
  15. Except
  16. ShowMessage (' Error ... ');
  17. Exit;
  18. End
  19. Result: = Excel.version;
  20. Excel.quit;
  21. Excel: = UnAssigned;
  22. End

Transferred from: http://blog.csdn.net/love3s/article/details/6973761

Use Aadoconnection to connect excel in Delphi7 today

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.