首页 新闻 会员 周边

delphi 操作excel,复制表格的方法错误

0
悬赏园豆:10 [已关闭问题] 关闭于 2015-10-25 18:30
unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleServer, ExcelXP, StdCtrls , ExtCtrls;

type
  TForm1 = class(TForm)
    exclplctn1: TExcelApplication;
    exclwrkbk1: TExcelWorkbook;
    exclwrksht1: TExcelWorksheet;
    exclplctn2: TExcelApplication;
    exclwrkbk2: TExcelWorkbook;
    exclwrksht2: TExcelWorksheet;
    btn1: TButton;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btn1Click(Sender: TObject);
begin
  exclplctn1.Connect;
  exclwrkbk1.ConnectTo(exclplctn1.Workbooks.Add(EmptyParam , 0));
  exclplctn1.Workbooks[1].Activate(0);
  exclplctn1.Visible[0] := True;

  exclplctn2.Connect;
  exclplctn2.Workbooks.Open('d:\Information.xlsx' ,
    EmptyParam , EmptyParam , EmptyParam , EmptyParam ,
    EmptyParam , EmptyParam , EmptyParam , EmptyParam ,
    EmptyParam , EmptyParam , EmptyParam , EmptyParam ,
    EmptyParam , EmptyParam , 0);
  exclplctn2.Workbooks[1].Activate(0);
  exclplctn2.Visible[0] := True;

  exclplctn2.ActiveSheet.Used.Range.Copy;  //这句Used , Range , Copy均报错。
  

end; end.

Used报错为:IDisptch does not contain a member named 'Used';

Range & Copy报错为:Undeclared indentifier。

请问大大们,这个是怎么回事,还有下面动态创建。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs , ComObj;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  wb1 : Variant;
  wb2 : Variant;
begin
  wb1 := CreateOleObject('Excel.Application');
  wb2 := CreateOleObject('Excel.Application');

  wb1.WorkBooks.add;
  wb2.WorkBooks.Open('D:\Information.xlsx');

  wb1.Visible := True;
  wb2.Visible := True;

  wb2.Activate.Used.Range.Copy;       //这句不能执行;

end;

end.

拷贝操作还是不行,不能执行,报出异常:rasied exception class EOleError with message 'Method 'Activate' not supported by automaion object';

还请大神交一下小弟,万分感谢。

ChenYao_Freedom的主页 ChenYao_Freedom | 初学一级 | 园豆:5
提问于:2015-10-25 15:46
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册