求救啊 delphi 连接Excel 的问题
我在面板上放了3个组件分别是:ExcelApplication1:TExcelApplication;ExcelWorksheet1:TExcelWorksheet;Ex...
我在面板上放了3个 组件分别是:
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
ExcelApplication1.Connect;
ExcelApplication1.Visible[0]:=true;
ExcelApplication1.Caption:='调用Excel';
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(EmptyParam,0));
以上代码都编译通过了 也能运行,可就是下面这一行怎么都写不对:
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1]as_worksheet);
请问怎样才能把ExcelWorksheet1连上ExcelWorkbook1呢 ? 展开
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
ExcelApplication1.Connect;
ExcelApplication1.Visible[0]:=true;
ExcelApplication1.Caption:='调用Excel';
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(EmptyParam,0));
以上代码都编译通过了 也能运行,可就是下面这一行怎么都写不对:
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1]as_worksheet);
请问怎样才能把ExcelWorksheet1连上ExcelWorkbook1呢 ? 展开
1个回答
展开全部
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,comobj, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
excelapp:variant;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i,j,s:integer;
begin
excelapp:=createoleobject('excel.application');
excelapp.visible:=true; //显示当前窗口
excelapp.caption:='试验'; //改一下excel的标题栏
excelapp.workbooks.add; //加一个新的工作簿
excelapp.workbooks.open('D:\1.xls'); //打开一个已经存在的工作簿
excelapp.worksheets[2].activate; //或者excelapp.workssheets['sheet2'].activate; //设置第2个工作表为活动工作表
for i:=1 to 100 do
for j:=1 to 10 do begin
Randomize;
s := Random(10) + 1;
excelapp.cells[i,j].value:=inttostr(s); //给某一个框框赋值
end;
//excelapp.activesheet.columns[1].columnswidth:=20; //设置指定列的宽度 [单位/字符个数]
//excelapp.activesheet.rows[2].rowheight:=1/0.035 //设置指定行的高度 [单位:磅] (1磅=0.035厘米)
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//excelapp.Range['a1:a4'].Select;
excelapp.Range['a1:a4'].Merge;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,comobj, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
excelapp:variant;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
i,j,s:integer;
begin
excelapp:=createoleobject('excel.application');
excelapp.visible:=true; //显示当前窗口
excelapp.caption:='试验'; //改一下excel的标题栏
excelapp.workbooks.add; //加一个新的工作簿
excelapp.workbooks.open('D:\1.xls'); //打开一个已经存在的工作簿
excelapp.worksheets[2].activate; //或者excelapp.workssheets['sheet2'].activate; //设置第2个工作表为活动工作表
for i:=1 to 100 do
for j:=1 to 10 do begin
Randomize;
s := Random(10) + 1;
excelapp.cells[i,j].value:=inttostr(s); //给某一个框框赋值
end;
//excelapp.activesheet.columns[1].columnswidth:=20; //设置指定列的宽度 [单位/字符个数]
//excelapp.activesheet.rows[2].rowheight:=1/0.035 //设置指定行的高度 [单位:磅] (1磅=0.035厘米)
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
//excelapp.Range['a1:a4'].Select;
excelapp.Range['a1:a4'].Merge;
end;
end.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询