delphi - 在 Delphi 中,在 Excel Interop Worksheets 集合上使用 for 循环时,如何处理 "no GetEnumerator present"错误?

标签 delphi excel interop for-in-loop

我正在尝试编写一个 Delphi 程序,它将循环遍历 Excel 文件中的每个工作表并格式化一些单元格。不过,我在尝试对 Workbook.Worksheets 集合使用 for-in 循环时收到错误。错误具体是:

[DCC Error] Office.pas(36): E2431 for-in statement cannot operate on collection type 'Sheets' because 'Sheets' does not contain a member for 'GetEnumerator', or it is inaccessible

发生这种情况的代码行是:

for Worksheet in Workbook.Worksheets do

Worksheet和Workbook的定义如下:

var ExcelApp: ExcelApplication;
var Workbook: ExcelWorkbook;
var Worksheet: ExcelWorksheet;

我正在将此代码从 C# 移植到 Delphi,它可以在其中运行。有谁知道为什么我会收到此 GetEnumerator 错误?我正在使用 Office 2007 Excel Interop 文件和 Embarcadero® Delphi® 2010 版本 14.0.3593.25826。

最佳答案

我不确定 C# 如何处理可枚举集合,但对于 Delphi,它会在集合上查找名为 GetEnumerator 的方法,该方法返回一个枚举器。枚举数必须是至少包含以下两个成员的数据结构:

public
  function MoveNext: boolean;
  property Current: <some type> read <some read method>;
end;

如果 Delphi 说“Sheets”不包含“GetEnumerator”的成员,或者它不可访问,那么这正是它所说的意思。要么表格上没有 GetEnumerator,要么它不是公共(public)方法。 Sheets 的定义是什么样的?

关于delphi - 在 Delphi 中,在 Excel Interop Worksheets 集合上使用 for 循环时,如何处理 "no GetEnumerator present"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2448897/

相关文章:

delphi - 复合 "with"语句中的名称解析如何工作?

delphi - 重写 Delphi 表单构造函数和使用它的 OnCreate 事件是否存在问题?

excel - VBE 使用分号自动更正

c# - Microsoft Office Interop Excel - 指定 Excel 文件的名称

sql - Delphi:如何将列表作为参数传递给 SQL 查询?

delphi - 是否可以获取Delphi 7中指针指向的类型的大小?

excel - 使用 VBA 从网页复制特定图像并粘贴到 Excel 中

php - 使用 PHPexcel 插件生成的可下载 Excel 文件中没有获取任何值

c++ - Rust 与 C++ std::string 互操作

C#:编码包含数组的结构