无效变体操作错误
可能的目标是...我有一个xls excel 2003文件...在第一张表的第一列中,有五个a 2 b字符...
用我的delphi代码,我想输出这样的.. 这个文件有5个a字符,2个b字符...... 当我编译并运行程序时,它给出了无效的变体操作
......烦人...... 完整代码如下:
unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComObj, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private { Private declarations }
public { Public declarations }
end;
var
Form1: TForm1;
var
uygulama: variant;
var
i, w: integer;
// var str:string;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
uygulama := CreateOleObject('Excel.Application');
uygulama.visible := false;
uygulama.Workbooks.open['c:\liste.xls'];
// label1.Caption:=(uygulama.ActiveSheet.cells[1,1]);
i := 1;
w := 1;
repeat
if uygulama.ActiveSheet.cells[i, 1] = 'a' then
inc(w);
until uygulama.ActiveSheet.cells[i, 1] = '';
Label1.Caption := inttostr(w);
end;
end.
May aim is... I have a xls excel 2003 file... In first colums of first sheet, there are five a two b characters...
With my delphi code, I want to output such that..
This file has 5 a characters, two b characters.....
When I compile and run the program, it gives Invalid variant operation
... annoying...
The complete code is given below:
unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComObj, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private { Private declarations }
public { Public declarations }
end;
var
Form1: TForm1;
var
uygulama: variant;
var
i, w: integer;
// var str:string;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
uygulama := CreateOleObject('Excel.Application');
uygulama.visible := false;
uygulama.Workbooks.open['c:\liste.xls'];
// label1.Caption:=(uygulama.ActiveSheet.cells[1,1]);
i := 1;
w := 1;
repeat
if uygulama.ActiveSheet.cells[i, 1] = 'a' then
inc(w);
until uygulama.ActiveSheet.cells[i, 1] = '';
Label1.Caption := inttostr(w);
end;
end.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要从单元格访问值,您必须使用
value
属性,请尝试以下操作
To access a value from a cell you must use the
value
propertytry this
假设您现在已经弄清楚如何获取单元格的值,那么我希望就您的代码提出几点建议。
重复循环会非常慢。最好在进入循环之前将单元格的值存储在局部变量中,然后对局部变量执行计数操作。
循环很有可能是无限的,因为您没有更改任何会发生变化的值(除了 w 之外)。为什么不写一些类似的东西
Assuming that you have now figured out how to get the value of the cell, there are a few points regarding your code which I wish to make.
The repeat loop is going to be very slow. It would be better to store the cell's value in a local variable before entering the loop and then perform your counting operation on your local variable.
The loop stands a very good chance of being infinite because you're not changing any values which change (apart from w). Why not write something like