在 Delphi 中,对于“不存在 GetEnumerator”我该怎么办?在 Excel Interop Worksheets 集合上使用 for 循环时出错?
我正在尝试编写一个 Delphi 程序,它将循环遍历 Excel 文件中的每个工作表并格式化一些单元格。不过,我在尝试对 Workbook.Worksheets 集合使用 for-in 循环时收到错误。错误具体是:
[DCC 错误] Office.pas(36): E2431 for-in 语句不能操作 集合类型“Sheets”因为 “工作表”不包含以下成员 'GetEnumerator',或者无法访问
发生这种情况的代码行是:
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。
I'm trying to write a Delphi program that will loop through each worksheet in an Excel file and format some cells. I'm receiving an error while trying to use the for-in loop over the Workbook.Worksheets collection, though. The error is specifically:
[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
The line of code this occurs for is:
for Worksheet in Workbook.Worksheets do
The definition of Worksheet and Workbook is as follows:
var ExcelApp: ExcelApplication;
var Workbook: ExcelWorkbook;
var Worksheet: ExcelWorksheet;
I'm porting this code to Delphi from C#, in which it works. Does anyone know why I'd be getting this GetEnumerator error? I'm using the Office 2007 Excel Interop file and Embarcadero® Delphi® 2010 Version 14.0.3593.25826.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C# 处理枚举器的方式与 Delphi 类似。有一个 IEnumerable 接口,它有两个方法:MoveNext 和 Reset;还有一处名为 Current 的房产。
对于ExcelWorkBook.Sheets属性,需要注意的是,虽然MSDN文档说它有一个GetEnumerator方法,但Delphi提供的ExcelXP单元中该类的定义并没有提供这样的方法。
因此,也许您必须将其类型库的较新版本导入到 Delphi 中。
C# handles enumerators similar to Delphi. There is an IEnumerable interface that has two methods: MoveNext, and Reset; and also one property called Current.
For ExcelWorkBook.Sheets property, you should take note that although MSDN documentation says it has a GetEnumerator method, this class's definition in ExcelXP unit provided by Delphi does not provide such a method.
So maybe you have to import a newer version of its type library into Delphi.
我不确定 C# 如何处理可枚举集合,但对于 Delphi,它在集合上查找名为 GetEnumerator 的方法,该方法返回一个枚举器。枚举数必须是一个至少包含以下两个成员的数据结构:
如果 Delphi 说“Sheets”不包含“GetEnumerator”的成员,或者它不可访问,那么这正是它所说的意思。要么表格上没有 GetEnumerator,要么它不是公共方法。 Sheets 的定义是什么样的?
I'm not sure how C# handles enumerable collections, but for Delphi, it looks for a method on the collection called GetEnumerator, which returns an enumerator. The enumerator must be a data structure that contains at least the following two members:
If Delphi says 'Sheets' does not contain a member for 'GetEnumerator', or it is inaccessible, then that means exactly what it says. Either there's no GetEnumerator on Sheets, or it's not a public method. What does the definition of Sheets look like?
编辑:根据注释,该对象存在 GetEnumerator 方法,因此该解决方案可能不正确。
我已经使用 Delphi 有一段时间了,但根据错误我的猜测是 ExcelWorksheet collection 没有枚举,这意味着执行 for/each 样式循环不起作用,您需要使用类似 C 的 for 循环:
EDIT: Based on comments, the GetEnumerator method exists for the object, so this solution is likely incorrect.
It's been a while since I've used Delphi, but my guess based on the error is that the ExcelWorksheet collection is not enumerated, which would mean doing a for/each style loop would not work and you'd need to use a C-like for loop: