使用Delphi和ADO编辑Excel工作表

This step-by-step guide describes how to connect to Microsoft Excel, retrieve sheet data, and enable editing of the data using the DBGrid. You'll also find a list of the most common errors that might appear in the process, plus how to deal with them.

本分步指南介绍了如何连接到Microsoft Excel,检索工作表数据以及使用DBGrid启用数据编辑。 您还将找到过程中可能出现的最常见错误的列表,以及如何处理它们。

What's Covered Below:

以下内容:

  • Methods for transferring data between Excel and Delphi. How to connect to Excel with ADO (ActiveX Data Objects) and Delphi.

    Excel与Delphi之间的数据传输方法。 如何使用ADO (ActiveX数据对象)和Delphi连接到Excel。

  • Creating an Excel spreadsheet editor using Delphi and ADO

    使用Delphi和ADO创建Excel电子表格编辑器
  • Retrieving the data from Excel. How to reference a table (or range) in an Excel workbook.

    从Excel检索数据。 如何在Excel工作簿中引用表(或范围)。
  • A discussion on Excel field (column) types

    关于Excel字段(列)类型的讨论
  • How to modify Excel sheets: edit, add and delete rows.

    如何修改Excel工作表:编辑,添加和删除行。
  • Transferring data from a Delphi application to Excel. How to create a worksheet and fill it with custom data from an MS Access database.

    将数据从Delphi应用程序传输到Excel。 如何创建工作表并用MS Access数据库中的自定义数据填充工作表。

如何连接到Microsoft Excel ( How to Connect to Microsoft 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.

Microsoft Excel是功能强大的电子表格计算器和数据分析工具。 由于Excel工作表的行和列与数据库表的行和列紧密相关,因此许多开发人员认为将其数据传输到Excel工作簿中以进行分析是合适的; 然后将数据检索回应用程序。

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.

在应用程序和Excel之间进行数据交换的最常用方法是自动化 。 自动化提供了一种使用Excel对象模型读取Excel数据的方法,以深入研究工作表,提取其数据,并将其显示在类似于网格的组件(即DBGrid或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:

若要在没有自动化的情况下与Excel进行数据传输,可以使用其他方法,例如:

  • Write data into a comma-delimited text file, and let Excel parse the file into cells

    将数据写入以逗号分隔的文本文件,然后让Excel将文件解析为单元格
  • Transfer data using DDE (Dynamic Data Exchange)

    使用DDE(动态数据交换)传输数据
  • Transfer your data to and from a worksheet using ADO

    使用ADO与工作表之间进行数据传输

使用ADO进行数据传输 ( Data Transfer Using ADO )

Since Excel is JET OLE DB compliant, you can connect to it with Delphi using ADO (dbGO or AdoExpress) and 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).

由于Excel符合JET OLE DB,因此可以使用ADO(dbGO或AdoExpress)与Delphi进行连接,然后通过发出SQL查询将工作表的数据检索到ADO数据集中(就像您针对任何数据库表打开数据集一样) 。

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 let you 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.

这样,ADODataset对象的所有方法和功能都可用于处理Excel数据。 换句话说,使用ADO组件,您可以构建可以将Excel工作簿用作数据库的应用程序。 另一个重要的事实是Excel是进程外ActiveX服务器 。 ADO在进程内运行,并节省了代价高昂的进程外调用的开销。

When you connect to Excel using ADO, you can only exchange raw data to and from a workbook. An 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.

当您使用ADO连接到Excel时,您只能与工作簿交换原始数据。 ADO连接不能用于工作表格式或实现单元格的公式。 但是,如果将数据传输到预先设置格式的工作表中,则将保留该格式。 将数据从应用程序插入Excel之后,您可以使用工作表中的(预先记录的)宏执行任何条件格式。

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. We'll focus on Jet OLE DB Provider, which can be used to access data in Excel workbooks through installable Indexed Sequential Access Method (ISAM) drivers.

您可以使用ADO与作为MDAC一部分的两个OLE DB提供程序连接到Excel:Microsoft Jet OLE DB提供程序或ODBC驱动程序的Microsoft OLE DB提供程序。 我们将重点介绍Jet OLE DB提供程序,该程序可用于通过可安装的索引顺序访问方法(ISAM)驱动程序访问Excel工作簿中的数据。

Tip: See the Beginners Course to Delphi ADO Database Programming if you're new to ADO.

提示:如果您不熟悉 ADO,请参阅Delphi ADO数据库编程的初学者课程

ConnectionString魔术 ( 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.

ConnectionString属性告诉ADO如何连接到数据源。 用于ConnectionString的值由ADO用于建立连接的一个或多个参数组成。

In Delphi, the TADOConnection component encapsulates the ADO connection object; it can be shared by multiple ADO dataset (TADOTable, TADOQuery, etc.) components through their Connection properties.

在Delphi中,TADOConnection组件封装了ADO连接对象。 它可以由多个ADO数据集(TADOTable,TADOQuery等)组件通过其Connection属性共享。

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.

为了连接到Excel,有效的连接字符串仅涉及另外两条信息-工作簿的完整路径和Excel文件版本。

A legitimate connection string could look like this:

合法的连接字符串可能如下所示:


ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyWorkBooks\myDataBook.xls;Extended Properties=Excel 8.0;';

When connecting to an external database format supported by the Jet, the extended properties for the connection needs to be set. In our case, when connecting to an Excel "database," extended properties are used to set the Excel file version. 

当连接到Jet支持的外部数据库格式时,需要设置连接的扩展属性。 在我们的情况下,当连接到Excel“数据库”时,扩展属性用于设置Excel文件版本。

For an Excel95 workbook, this value is "Excel 5.0" (without the quotes); use "Excel 8.0" for Excel 97, Excel 2000, Excel 2002, and ExcelXP.

对于Excel95工作簿,此值为“ Excel 5.0”(不带引号); 对于Excel 97,Excel 2000,Excel 2002和ExcelXP,请使用“ Excel 8.0”。

Important: 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.

重要说明:由于Jet 3.5不支持ISAM驱动程序,因此必须使用Jet 4.0 Provider。 如果将Jet Provider设置为版本3.5,则会收到“找不到可安装的ISAM”错误。

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.

Jet的另一个扩展属性是“ HDR =“。 “ HDR =是”表示该范围中存在标题行,因此Jet不会将所选内容的第一行包括在数据集中。 如果指定“ HDR = No”,则提供程序会将范围(或命名范围)的第一行包括到数据集中。

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".

默认情况下,范围内的第一行被视为标题行(“ HDR = Yes”)。 因此,如果您具有列标题,则无需指定此值。 如果没有列标题,则需要指定“ HDR = No”。

Now that you're all set, this is the part where things become interesting since we're now ready for some code. Let's see how to create a simple Excel Spreadsheet editor using Delphi and ADO.

现在您已经准备就绪,这是使事情变得有趣的部分,因为我们现在已经准备好编写一些代码。 让我们看看如何使用Delphi和ADO创建一个简单的Excel Spreadsheet编辑器。

Note: You should proceed even if you lack knowledge on ADO and Jet programming. As you'll see, editing an Excel workbook is as simple as editing data from any standard database.

注意:即使您缺乏ADO和Jet编程知识,也应该继续进行。 如您所见,编辑Excel工作簿就像编辑任何标准数据库中的数据一样简单。

翻译自: https://www.thoughtco.com/editing-ms-excel-sheets-with-delphi-and-ado-4068789

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值