关于"数据库内容导出到EXCEL 中时出现错误"的问题.(20分)

  • 主题发起人 主题发起人 ysp_1984
  • 开始时间 开始时间
Y

ysp_1984

Unregistered / Unconfirmed
GUEST, unregistred user!
用下面的代码,将数据库内容导出到EXCEL 中时出现错误,错误为"没有注册类别".是什么意思啊?
procedure DataToExcel(TbOrQuery:TCustomADODataset;GridCtrl:TDBGrid );
var
SaveFn:String;
SaveDialog1: TSaveDialog;
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
ACount,i:Integer;
low,row:Integer;
begin
SaveDialog1:=TSaveDialog.Create(Application.Owner);
SaveDialog1.Filter:='*.Xls';
if Not SaveDialog1.Execute then SaveDialog1.Execute ;
SaveFn:=SaveDialog1.FileName ;
if SaveFn='' then begin messagedlg('請輸入文件名!',mtinformation,[mbok],0);
abort end;
low:=2;
ACount:=GridCtrl.FieldCount ;
ExcelApplication1:=TExcelApplication.Create(Application.Owner);
ExcelWorksheet1:=TExcelWorksheet.Create(Application.Owner );
ExcelWorkbook1:=TExcelWorkbook.Create(Application.Owner );
try
ExcelApplication1.Connect ;
except
MessageDlg('請確定是否正確安裝Excel2000?',mterror,[mbok],0);
abort;
end;
ExcelApplication1.Visible[0]:=false;
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks.Add(null,0) );
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet);
for i:=1 to ACount do
ExcelWorksheet1.Cells.Item[1,i]:=GridCtrl.Columns[i-1].Title.Caption ;
TbOrQuery.First ;
while not TbOrQuery.Eof do
begin
for row:=1 to ACount do begin
ExcelWorksheet1.Cells.Item[low,row].NumberFormatLocal:='@';
ExcelWorksheet1.Cells.Item[low,row]:=TbOrQuery.Fields[row-1].Value ;
end;
low:=low+1;
TbOrQuery.Next;
end;

ExcelWorkBook1.SaveCopyAs(SaveFn+'.xls');
ExcelWOrkBook1.Close(False);
ExcelWorksheet1.Disconnect ;
ExcelWorkbook1.Disconnect ;
ExcelApplication1.Disconnect;
ExcelWOrkBook1.Close;
ExcelApplication1.Quit;
ExcelWorksheet1.Free;
ExcelWorkbook1.Free;
ExcelApplication1.Free ;
end;
 
怎么没有人会嘛?
代码:
 
這個是可以導出的.你可以試下.
procedure TForm1.Button3Click(Sender: TObject);
var
fieldNum: integer;
i,j : integer;
exls,sheet: variant;
begin
try
exls:=createoleobject('Excel.application');
sheet:=createoleobject('excel.sheet');
except
application.MessageBox('please install MICROSOFT EXCEL first !','information',mb_ok+mb_iconinformation);
exit;
end;
sheet:=exls.workBooks.Add;
with datamodule2.ADOQuery1 do
begin
DisableControls;
fieldNum := dbgrid1.fieldCount;
for i:=1 to fieldNum do
begin
exls.Cells[1,i].value:=Fields[i-1].FieldName;
end;
first;
i:=2;
while not eof do
begin
for j:=1 to fieldNum do
begin
exls.Cells[i,j]:=fields[j-1].AsString;
end;
next;
i:=i+1;
end;
EnableControls;
exls.visible:=true;
end;
end;
 
谢了!问题解决了。再次感谢myveremy
你真是个可爱的人。
 
我还想问个问题,就是导出到Excel的时候,由于字段值太长,使得Excel列的宽度不够大。
如何在导出的时候顺便调整其Excel列的宽度。
 
后退
顶部