Applies To: |
|
Summary: |
I want to read and write data to an Excel spreadsheet via ODBC. Can you tell me how to configure this and what my options are? |
Solution: |
There are are three options. You can use
either the Cicode SQL functions, the Cicode Device Functions or a
combination of both. Refer to KB Q2063 for more details. Both
methods require a DSN to be configured in the ODBC applet of the
Control Panel. The second and third option, also require an SQL
device to be configured in Citect.
To create a DSN for an existing spreadsheet, select the User DSN tab of the ODBC Administrator form and Add a new DSN after selecting the Microsoft Excel Driver from the listbox. Complete the forms by selecting the appropriate Excel Version and browse to find the Excel Workbook you want to reference. Note: select your new DSN from the list and double click to bring up the Setup Dialog box. Select the Options button and make sure the Read Only box is not checked if you intend writing to this device. In order to access your Excel spreadsheet as an SQL device, it must contain the equivalent of database tables. Any named regions defined in the workbook are "Tables" as far as ODBC is concerned and the first row is taken as the column names. You can have more than one "Table" on the one worksheet. To define regions in Excel go through the menu items INSERT, NAME, DEFINE and type in a "Table" name after selecting the desired region. In Excel 4.0/5.0 and 97, table names already exists for each worksheet within a workbook. In Excel V5.00 they all have a dollar sign ($) as the last character, and you must be sure to include the dollar sign when accessing the worksheet. For example, if you want to access the entire sheet1 of a workbook, you would refer to the table as sheet1$, eg. SQLExec(hSQL, "select * from "sheet1$""). Note that the sheet name is surrounded by double quotation marks. In Excel V4.00, you do not require a $ or double quotation marks. eg SQLEXEC(hSQL, "select * from sheet1"). In Excel97 it appears that the correct syntax is [sheet1$]. Note that you are probably better off defining your own region (ie. table) as the default worksheet tables will contain 256 fields, which is far from optimal for most situations. 1. Using SQL Functions You are now ready to read and write data to your Excel spreadsheet. Example 1.
Example 2.
2. Using Dev Functions You will need to configure a device record that references the DSN and table. For example:
Example
Note that if you are only intending to access your Excel spreadsheet via the Cicode Device functions, then you should consider just defining the Excel device as a dBase device rather than an SQL device. Then you will not need to configure or reference a DSN. |
Keywords: |
Related Links
Attachments