我用midas开发了一套管理软件,其中把数据集导出excel时,好多机器都没有问题,但有的却不行,导出时提示,interface not support.(接口不支持).
ExcelApplication1、ExcelWorkbook1、ExcelWorksheet1为控件.
请问是不是系统环境是不是缺少文件啊.我的函数是这样写的:
//保存数据到EXCEL
Procedure TSysdb.PubP_DataSaveToExcel(TblName:TdataSet;
Const Tilte:string='';ShowCol:boolean=false);
var i,j,k:integer;
V:array of array of variant;
begin
if not SaveDialog1.Execute then exit;
application.ProcessMessages;
screen.Cursor :=crSQLWait;
try
try
ExcelApplication1.Connect;
ExcelApplication1.Visible[0]:=false;
ExcelApplication1.Workbooks.Add(NULL,0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Sheets[1] as _WorkSheet);
except
application.MessageBox('导出出错,请确认EXCEL97或EXCEL2000是否已按装!',CON_PROMPT,MB_OK+MB_ICONSTOP);
end;
with TblName do
begin
disablecontrols;
if not ShowCol then
V := VarArrayCreate([0,RecordCount+5,0,Fields.Count], varVariant)
else
V := VarArrayCreate([0,2*RecordCount+5,0,Fields.Count], varVariant);
i :=2;
j :=0;
k :=0;
V[0][2] := Tilte;
if not ShowCol then //显示单标题
for j:=0 to Fields.Count-1 do
if Fields[j].visible then //取到标题
begin
V[i][k] := Fields[j].displaylabel;
k := k+1;
end;
first;
while not eof do
begin
k:= 0;
if not ShowCol then //显示单标题
i:= i+1
else
i:= i+2;
for j:=0 to Fields.Count-1 do
begin
if Fields[j].visible then
begin
if not ShowCol then //显示单标题
begin
if Fields[j] is TStringField then
V[i][k] := ''''+Fields[j].AsString//取到数据
else
V[i][k] := Fields[j].value;//取到数据
k :=k+1;
end
else
begin
V[i][k] := Fields[j].displaylabel;
if Fields[j] is TStringField then
V[i+1][k] := ''''+Fields[j].AsString//取到数据
else
V[i+1][k] := Fields[j].value;//取到数据
k :=k+1;
end;
end;
end;
next;
end;
if not ShowCol then //显示单标题
begin
if Fields.Count<26 then
ExcelWorksheet1.Range['A1', Format('%s%d',[Chr(Ord('A')+Fields.Count),RecordCount+5])].Value := V
else
ExcelWorksheet1.Range['A1', Format('%s%d',[Chr(Ord('A')+Trunc(Fields.Count/26)-1)+Chr(Ord('A')+(Fields.Count mod 26)),RecordCount+5])].Value := V;
end
else
begin
if Fields.Count<26 then
ExcelWorksheet1.Range['A1', Format('%s%d',[Chr(Ord('A')+Fields.Count),2*RecordCount+5])].Value := V
else
ExcelWorksheet1.Range['A1', Format('%s%d',[Chr(Ord('A')+Trunc(Fields.Count/26)-1)+Chr(Ord('A')+(Fields.Count mod 26)),2*RecordCount+5])].Value := V;
end;
first;
enablecontrols;
end;
ExcelWorksheet1.SaveAs(SaveDialog1.FileName);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
finally
screen.Cursor := crdefault;
end;
end;
ExcelApplication1、ExcelWorkbook1、ExcelWorksheet1为控件.
请问是不是系统环境是不是缺少文件啊.我的函数是这样写的:
//保存数据到EXCEL
Procedure TSysdb.PubP_DataSaveToExcel(TblName:TdataSet;
Const Tilte:string='';ShowCol:boolean=false);
var i,j,k:integer;
V:array of array of variant;
begin
if not SaveDialog1.Execute then exit;
application.ProcessMessages;
screen.Cursor :=crSQLWait;
try
try
ExcelApplication1.Connect;
ExcelApplication1.Visible[0]:=false;
ExcelApplication1.Workbooks.Add(NULL,0);
ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]);
ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Sheets[1] as _WorkSheet);
except
application.MessageBox('导出出错,请确认EXCEL97或EXCEL2000是否已按装!',CON_PROMPT,MB_OK+MB_ICONSTOP);
end;
with TblName do
begin
disablecontrols;
if not ShowCol then
V := VarArrayCreate([0,RecordCount+5,0,Fields.Count], varVariant)
else
V := VarArrayCreate([0,2*RecordCount+5,0,Fields.Count], varVariant);
i :=2;
j :=0;
k :=0;
V[0][2] := Tilte;
if not ShowCol then //显示单标题
for j:=0 to Fields.Count-1 do
if Fields[j].visible then //取到标题
begin
V[i][k] := Fields[j].displaylabel;
k := k+1;
end;
first;
while not eof do
begin
k:= 0;
if not ShowCol then //显示单标题
i:= i+1
else
i:= i+2;
for j:=0 to Fields.Count-1 do
begin
if Fields[j].visible then
begin
if not ShowCol then //显示单标题
begin
if Fields[j] is TStringField then
V[i][k] := ''''+Fields[j].AsString//取到数据
else
V[i][k] := Fields[j].value;//取到数据
k :=k+1;
end
else
begin
V[i][k] := Fields[j].displaylabel;
if Fields[j] is TStringField then
V[i+1][k] := ''''+Fields[j].AsString//取到数据
else
V[i+1][k] := Fields[j].value;//取到数据
k :=k+1;
end;
end;
end;
next;
end;
if not ShowCol then //显示单标题
begin
if Fields.Count<26 then
ExcelWorksheet1.Range['A1', Format('%s%d',[Chr(Ord('A')+Fields.Count),RecordCount+5])].Value := V
else
ExcelWorksheet1.Range['A1', Format('%s%d',[Chr(Ord('A')+Trunc(Fields.Count/26)-1)+Chr(Ord('A')+(Fields.Count mod 26)),RecordCount+5])].Value := V;
end
else
begin
if Fields.Count<26 then
ExcelWorksheet1.Range['A1', Format('%s%d',[Chr(Ord('A')+Fields.Count),2*RecordCount+5])].Value := V
else
ExcelWorksheet1.Range['A1', Format('%s%d',[Chr(Ord('A')+Trunc(Fields.Count/26)-1)+Chr(Ord('A')+(Fields.Count mod 26)),2*RecordCount+5])].Value := V;
end;
first;
enablecontrols;
end;
ExcelWorksheet1.SaveAs(SaveDialog1.FileName);
ExcelApplication1.Disconnect;
ExcelApplication1.Quit;
finally
screen.Cursor := crdefault;
end;
end;
var
Eclapp,workbook:variant;
row,column,i:integer;
beginif messageDlg('真的要导出到EXCEL吗?',mtconfirmation,[mbyes,mbno],0)<>idyes then
begin
exit;
end
else
begin
try
begin //生成一个Excel OleObject
EclApp:=CreateOleObject('Excel.Application');
workBook:=CreateOleObject('Excel.sheet');
end
except
showmessage('您的机器里面没有安装Excel!!');
exit;
end;
try
//生成一个Excel OleObject
EclApp:=CreateOleObject('Excel.Application');
workBook:=CreateOleObject('Excel.sheet'); if savedialog1.Execute then
begin
workBook:=EclApp.workBooks.add;
row:=2;
EclApp.Workbooks.item[1].activate;
EclApp.cells.font.colorindex:=5;
Eclapp.Activesheet.cells(1,2):='总库存';
datasource1.DataSet.First;
for i:=1 to dbgrid1.Columns.Count do
EclApp.activesheet.cells(2,I):=dbgrid1.Columns[i-1].Title.Caption;
//datasource1.DataSet.Fields[I-1].FieldName;
while not(datasource1.DataSet.Eof) do
begin
column:=1;
for i:=1 to datasource1.DataSet.FieldCount do
begin
eclapp.cells.item[row+1,column]:=datasource1.DataSet.Fields[i-1].AsString;
column:=column+1;
end;
datasource1.DataSet.Next;
row:=row+1;
end; //if savedialog1.Execute then
workbook.saveas(savedialog1.FileName);
workbook.saved:=true;
workBook.close;
Eclapp.quit;
eclapp:=unassigned;
showmessage('数据成功导出到Excel');
end
else
showmessage('Excel文件保存失败');
except
showmessage('Excel文件保存失败');
workbook.close;
eclapp.quit;
eclapp:=Unassigned;
end;
end;这个已经通过测试的
我写的方法不是全都不行,只是出现偶有一两台不行,还出现这种情况.有一台机器原来还行,重装了系统后,重装excel,我做的应用管理系统就导不出execl,提示interface not support.
http://www.microsoft.com/downloads/details.aspx?FamilyID=9ad000f2-cae7-493d-b0f3-ae36c570ade8&DisplayLang=zh-cn