Methods for Transferring Data Between Excel and Delphi
This step-by-step article describes how to connect to Excel, retrieve sheet data, and enable editing of data (using the DBGrid). You'll also find a list of most common errors (and how to deal with them) that might pop up in the process.
WHAT YOU WILL LEARN IN THIS ARTICLE
- Methods for transferring data between Excel and Delphi. How to connect to Excel with ADO and Delphi.
- Creating an Excel spreadsheet editor using Delphi and ADO
- Retrieving the data from Excel. How to reference a table (or range) in an Excel workbook.
- A discussion on Excel field (column) types
- How to modify Excel sheets: edit, add and delete rows.
- Transferring data from a Delphi application to Excel. How to create a worksheet, and fill it with "custom" (from an Access database) data.
CONNECT TO: MS EXCEL
Microsoft Excel is a powerful spreadsheet calculator and data analysis tool. Since rows and columns of an Excel worksheet closely relate to the rows and columns of a database table, many developers find it appropriate to transport their data into an Excel workbook for analysis purposes; and retrieve data back to the application afterwards.
The most commonly used approach to data exchange between your application and Excel is Automation. Automation provides a way to read Excel data using the Excel Object Model to dive into the worksheet, extract its data, and display it inside a grid-like component, namely DBGrid or StringGrid.
Automation gives you the greatest flexibility for locating the data in the workbook as well as the ability to format the worksheet and make various settings at run time.
To transfer your data to and from Excel without Automation, you can use other methods such as:
- Write data into a comma-delimited text file, and let Excel parse the file into cells,
- Transfer data using DDE (Dynamic Data Exchange),
- Transfer your data to (and from) a worksheet using ADO.
DATA TRANSFER USING ADO
Since Excel is JET OLE DB compliant, you can connect to it with Delphi using ADO (dbGO or AdoExpress) then retrieve the worksheet's data into an ADO dataset by issuing an SQL query – just like you would open a dataset against any database table. In this way, all the methods and features of the ADODataset object are available to process the Excel data. In other words, using the ADO components, you can build an application that can use an Excel workbook as the database. Another important fact is that Excel is an out-of-process ActiveX server. ADO runs in-process and saves the overhead of costly out-of-process calls.
When you connect to Excel using ADO, you can only exchange raw data to and from a workbook. ADO connection cannot be used for sheet formatting or implementing formulas to cells. However, if you transfer your data to a worksheet that is pre-formatted, the format is maintained. After the data is inserted from your application to Excel, you can carry out any conditional formatting using a (pre-recorded) macro in the worksheet.
You can connect to Excel using ADO with the two OLE DB Providers that are a part of MDAC: Microsoft Jet OLE DB Provider or Microsoft OLE DB Provider for ODBC Drivers.
This article will focus on Jet OLE DB Provider which can be used to access data in Excel workbooks, through installable Indexed Sequential Access Method (ISAM) drivers.
THE CONNECTIONSTRING MAGIC
The ConnectionString property tells ADO how to connect to the datasource. The value used for ConnectionString consists of one or more arguments ADO uses to establish the connection. In Delphi, the TADOConnection component encapsulates the ADO connection object; it can be shared by multiple ADO dataset (TADOTable, TADOQuery, …) components through their Connection properties.
In order to connect to Excel, a valid connection string involves only two (additional) pieces of information: the full path to the workbook, and the Excel file version.
In other words, a legitimate connection string could look like:
When you want to connect to an external database format supported by the Jet, you need to set the extended properties for the connection. In our case, when connecting to Excel "database", extended properties are used to set the Excel file version.
For an Excel95 workbook this value is "Excel 5.0" (without the quotes), for versions Excel 97, Excel 2000, Excel 2002 or ExcelXP the value is "Excel 8.0".
Caution: you must use the Jet 4.0 Provider, since Jet 3.5 does not support the ISAM drivers. If you set the Jet Provider to version 3.5 you'll receive the " Couldn't find installable ISAM." error message.
Another Jet extended property is "HDR=". "HDR=Yes" means that there is a header row in the range, so the Jet will not include the first row of the selection into the dataset. If "HDR=No" is specified, then the provider will include the first row of the range (or named range) into the dataset. The first row in a range is considered to be the header row by default ("HDR=Yes"), therefore if you have column heading you do not need to specify this value. If you do not have column headings, you need to specify "HDR=No";
All set. This is the part where things become interesting; we are now ready for some code. Let's see how to create a simple Excel Spreadsheet editor using Delphi and ADO…
Note: even if you lack knowledge on ADO and Jet programming, you should proceed. As you will see editing an Excel workbook is simple as editing data from any "standard" database!