delphi dbgrid 导出excel出错 出现 Undeclared identifier: 'f' 错误,代码如下 20
方法procedureDBGridInFoToExcel(FileName,TitleCaption:string;MakeDataSource:TDataSource;...
方法
procedure DBGridInFoToExcel(FileName, TitleCaption: string;MakeDataSource: TDataSource; makeDBGrid: TDBGrid);
var
xlApp, xlSheet, szValue: Variant;
ARow, iLoop: word;
begin
xlApp := CreateOleObject('Excel.Application');
try
xlSheet := CreateOleObject('Excel.Sheet');
xlSheet := xlApp.WorkBooks.Add;
// 表格标题
for iLoop := 0 to makeDBGrid.Columns.Count - 1 do
xlSheet.WorkSheets[1].Cells[1, iLoop + 1] := makeDBGrid.Columns[iLoop].Title.Caption;
// 数据
ARow := 2;
with MakeDataSource.DataSet do
begin
DisableControls;
First;
while not Eof do
begin
for iLoop := 0 to Fields.Count - 1 do
begin
szValue := Fields[iLoop].Value;
xlSheet.WorkSheets[1].Cells[ARow, iLoop + 1] := szValue;
end;
inc(ARow);
Next;
end;
First;
EnableControls;
end;
try
xlSheet.SaveAs(FileName);
Application.MessageBox('导出完毕!', '提示', MB_IconExclamation);
finally
xlSheet.Close;
xlApp.Quit;
xlApp := UnAssigned;
end;
except
Application.MessageBox('本机没有安装Excel.', '错误', MB_OK);
end;
end;
调用
procedure TForm1.Button2Click(Sender: TObject);
begin
DBGridInFoToExcel(f, '', DataSource1, DBGrid1);
end;
在button下调用时出现Undeclared identifier: 'f' 错误,不知道怎么回事 展开
procedure DBGridInFoToExcel(FileName, TitleCaption: string;MakeDataSource: TDataSource; makeDBGrid: TDBGrid);
var
xlApp, xlSheet, szValue: Variant;
ARow, iLoop: word;
begin
xlApp := CreateOleObject('Excel.Application');
try
xlSheet := CreateOleObject('Excel.Sheet');
xlSheet := xlApp.WorkBooks.Add;
// 表格标题
for iLoop := 0 to makeDBGrid.Columns.Count - 1 do
xlSheet.WorkSheets[1].Cells[1, iLoop + 1] := makeDBGrid.Columns[iLoop].Title.Caption;
// 数据
ARow := 2;
with MakeDataSource.DataSet do
begin
DisableControls;
First;
while not Eof do
begin
for iLoop := 0 to Fields.Count - 1 do
begin
szValue := Fields[iLoop].Value;
xlSheet.WorkSheets[1].Cells[ARow, iLoop + 1] := szValue;
end;
inc(ARow);
Next;
end;
First;
EnableControls;
end;
try
xlSheet.SaveAs(FileName);
Application.MessageBox('导出完毕!', '提示', MB_IconExclamation);
finally
xlSheet.Close;
xlApp.Quit;
xlApp := UnAssigned;
end;
except
Application.MessageBox('本机没有安装Excel.', '错误', MB_OK);
end;
end;
调用
procedure TForm1.Button2Click(Sender: TObject);
begin
DBGridInFoToExcel(f, '', DataSource1, DBGrid1);
end;
在button下调用时出现Undeclared identifier: 'f' 错误,不知道怎么回事 展开
1个回答
2017-02-26 · 知道合伙人软件行家
关注
展开全部
Undeclared identifier: 'f' 未定义的标志符 f。
从代码看,DBGridInFoToExcel(f, '', DataSource1, DBGrid1) 调用的第1个函数,其定义是 FileName, string 类型,所以调用时,传递的 f 应该是个 string 类型的变量。
但在实际代码 Button2Click 中,并没有定义 f ,如果不使用全局变量,则建议代码修改示例如下:
procedure TForm1.Button2Click(Sender: TObject);
var f: string;
begin
f := 'e:\test\test.xls';
DBGridInFoToExcel(f, '', DataSource1, DBGrid1);
end;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询