往Excel一个Cell一个Cell写数据太慢,VB中可以往Excel中一次写入整个数组,Delphi中怎么实现???(100分)

  • 主题发起人 主题发起人 dirk
  • 开始时间 开始时间
D

dirk

Unregistered / Unconfirmed
GUEST, unregistred user!
VB中代码:
-----------------------------------------------------------------------
Option Explicit
Dim Ex As Excel.Application
Dim Wb As Workbook
Dim Ws As Worksheet
Dim Ra As Range

Private Sub Command1_Click()
Dim a(2, 2) As String
Set Ex = New Excel.Application
Set Wb = Ex.Workbooks.Add
Set Ws = Wb.Worksheets.Add
Ex.Visible = True

a(0, 0) = "aaaaa"
a(0, 1) = "bbbbb"
a(1, 0) = "ccccc"
a(1, 1) = "ddddd"

[red]Ws.Range("A1:B2") = a[/red]

End Sub

Private Sub Command2_Click()
Ex.Quit
End Sub

-----------------------------------------------------------------------
但Delphi中无法写入整个数组,而这个方法可使写入Excel的速度大大提高,有没有办法实现???
 
哎呀,你怎么改口啦?哈哈,我刚想说不行。
 
Range是只读属性,不能像VB中那样赋值,VB中有默认属性,给Range赋值一数组可能只是对其一默认属性赋值,但不知道是哪个。

用Variant变量或ExcelWorksheetA.Range['A1','A3'].Value :=VarArrayOf(['1','10', '100']);虽然编译不出错,但运行返回错误:
Interface not supported.
 
var qzw:Variant;ff:array [1..3] of string;
qzw:=excelworksheet3.Range['A1','C1'];
ff[1]:='1';
ff[2]:='2';
ff[3]:='3';
qzw.VALUE:=VARARRAYOF([FF[1],FF[3],FF[2]]);
注意:A1。。C1对应FF[1]、[2]、[3]都是一维数组,我原来用A1。。A3,结果都是FF[1]的值。
在D6+WIN2000+OFFICE2000下调试通过
 
procedure TForm1.Button1Click(Sender: TObject);
var
ExcelApplicationA: TExcelApplication;
ExcelWorkbookA: TExcelWorkbook;
ExcelWorksheetA: TExcelWorksheet;
A: Variant;
begin
ExcelApplicationA:= TExcelApplication.Create(nil);
ExcelWorkbookA:=TExcelWorkbook.Create(nil);
ExcelWorksheetA:=TExcelWorksheet.Create(nil);

ExcelApplicationA.Connect ;
ExcelApplicationA.DisplayAlerts[0]:=false;
ExcelApplicationA.Visible[0]:=false;

ExcelWorkbookA.ConnectTo(ExcelApplicationA.Workbooks.Add(EmptyParam,0));

ExcelApplicationA.Visible[0]:=true;

a:=ExcelWorksheetA.Range['A1','C3'] ;//这里就出错???

a.Value :=VARARRAYOF(['aa','bb','cc']);
end;

不行啊,我还以为是d5的问题,结果D6也是一样,a:=ExcelWorksheetA.Range['A1','C3'] ;出错,下面是返回的错误:

---------------------------
Project1
---------------------------
Interface not supported.
---------------------------
确定
---------------------------
 
begin
excelapplication1.Connect;
excelapplication1.Caption:='123';
excelapplication1.Visible[0]:=true;
excelapplication1.DisplayAlerts[1]:=false;
excelapplication1.Workbooks.Add(null,0);
excelworkbook1.ConnectTo(excelapplication1.Workbooks[1]);
excelworksheet1.ConnectTo(excelworkbook1.Worksheets[1] as _worksheet);
excelworksheet2.ConnectTo(excelworkbook1.Worksheets[2] as _worksheet);
excelworksheet3.ConnectTo(excelworkbook1.Worksheets[3] as _worksheet);
excelworksheet3.Activate;
qzw:=excelworksheet3.Range['A1','C1'];
ff[1]:='1';
ff[2]:='2';
ff[3]:='3';
qzw.VALUE:=VARARRAYOF([FF[1],FF[3],FF[2]]);

 
明白了,错在我的ExcelWorksheetA:=TExcelWorksheet.Create(nil);,之后没有ConnectTo,虽然不ConnectTo也能使用,可看来还是有差别的,谢谢。
 
后退
顶部