Tolecontainer有一个oleobject:variant属性,据说通过oleobject可以将ole容器中的ole对象作为变量来访问.当我使用如下语句:
olecontainer1.CreateObjectFromFile('E:\test.xls',false);
olecontainer1.oleobject.workbooks.add(...);
时触发EintfCasterror异常,错误信息为:Interface not supported,
请问,oleobject属性该怎样使用?
olecontainer1.CreateObjectFromFile('E:\test.xls',false);
olecontainer1.oleobject.workbooks.add(...);
时触发EintfCasterror异常,错误信息为:Interface not supported,
请问,oleobject属性该怎样使用?
可以用循环的方法把每条记录写入Excel中。
procedure TForm1.Button1Click(Sender: TObject);
var
DBFPath: string;
I, J, K: Integer;
Temp: Excel97.Range;
begin
Button1.Enabled := False;
Button2.Enabled := False;
DBFPath := DirectoryListBox1.Directory;
ADOConnection1.ConnectionString := 'Provider=MSDASQL.1;Persist Security Info=False;Connect Timeout=15;Extended Properties="Driver={Microsoft dBase VFP Driver (*.dbf)};UID=;' +
'SourceDB=' + DBFPath + ';SourceType=DBF;Exclusive=No;' +
'BackgroundFetch=Yes;Collate=Machine;Null=Yes;Deleted=Yes;";' +
'Locale Identifier=2052';
Cursor := crHourGlass;
try
ADOConnection1.Connected := True; for I := 0 to FileListBox1.Items.Count - 1 do
begin
if ADOTable1.Active then ADOTable1.Close;
ADOTable1.TableName := FileListBox1.Items[I];
RxLabel1.Caption := '正在转换文件' + FileListBox1.Items[I] + '的数据...';
Application.ProcessMessages;
try
ADOTable1.Open;
ProgressBar1.Max := ADOTable1.RecordCount;
ProgressBar1.Position := 0; Excelapplication1.Workbooks.Add(EmptyParam, 0);
K := 2;
Temp := Excelapplication1.ActiveCell;
for J := 0 to ADOTable1.FieldCount - 1 do
begin
Temp.Value := ADOTable1.Fields[J].Displaylabel;
Temp := Temp.Next;
end; while not ADOTable1.Eof do
begin
ProgressBar1.Position := ProgressBar1.Position + 1;
Application.ProcessMessages;
Temp := Excelapplication1.Range['A'+IntToStr(K),'A'+IntToStr(K)];
for J := 0 to ADOTable1.FieldCount - 1 do
begin
Temp.Value := ADOTable1.Fields[J].AsString;
Temp := Temp.Next;
end;
ADOTable1.next;
Inc(K);
end;
ADOTable1.Close;
ExcelApplication1.ActiveWorkbook.SaveAs(ExtractFilePath(ParamStr(0)) + 'dbf\' + ADOTable1.TableName + '.xls', EmptyParam,
EmptyParam, EmptyParam, EmptyParam, EmptyParam, xlExclusive , EmptyParam,
EmptyParam, EmptyParam, EmptyParam, 0);
Excelapplication1.Quit;
Excelapplication1.Disconnect;
except
Application.MessageBox(PChar('数据表' + ADOTable1.TableName + '转化出错!'), '华有德康', MB_OK);
end;
end;
Application.MessageBox('所有数据转化成功!', '华有德康', MB_OK);
finally
Cursor := crDefault;
Button1.Enabled := True;
Button2.Enabled := True;
ProgressBar1.Position := 0;
RxLabel1.Caption := '';
end;