无效变体操作错误

发布于 2024-11-27 08:01:22 字数 1045 浏览 0 评论 0原文

可能的目标是...我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以酷 2024-12-04 08:01:22

要从单元格访问值,您必须使用 value 属性,

请尝试以下操作

  repeat
    if uygulama.ActiveSheet.cells[i, 1].value = 'a' then
      inc(w);
  until uygulama.ActiveSheet.cells[i, 1].value = '';

To access a value from a cell you must use the value property

try this

  repeat
    if uygulama.ActiveSheet.cells[i, 1].value = 'a' then
      inc(w);
  until uygulama.ActiveSheet.cells[i, 1].value = '';
三岁铭 2024-12-04 08:01:22

假设您现在已经弄清楚如何获取单元格的值,那么我希望就您的代码提出几点建议。

  1. 重复循环会非常慢。最好在进入循环之前将单元格的值存储在局部变量中,然后对局部变量执行计数操作。

  2. 循环很有可能是无限的,因为您没有更改任何会发生变化的值(除了 w 之外)。为什么不写一些类似的东西

    s:= uygulama.activesheet.cells[i,1].value;
    w:= 0;
    对于 i:= 1 到 length (s) 做
      如果 s[i] = 'a' 则 inc (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.

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

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

    s:= uygulama.activesheet.cells[i,1].value;
    w:= 0;
    for i:= 1 to length (s) do
      if s[i] = 'a' then inc (w);
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文