请高手指点啊!下面的代码问题? ( 积分: 100 )

  • 主题发起人 主题发起人 IT菜鸟
  • 开始时间 开始时间
I

IT菜鸟

Unregistered / Unconfirmed
GUEST, unregistred user!
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
ADODataSet1: TADODataSet;
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
ADOConnection2: TADOConnection;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
BitBtn4: TBitBtn;
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
procedure FormShow(Sender: TObject);
procedure BitBtn4Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
function WriteExcel(ads:tadodataset):string;
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
excelapplication1:texcelapplication;
excelworkbook1:texcelworkbook;
excelworksheet1:texcelworksheet;
filename:string;
i,j:integer;
implementation
function tform1.WriteExcel(ads:tadodataset;sname,title:string):string;
begin
filename:=concat(extracfilepath(application.exename),sname,'.xls');
try
excelapplication1:=texcelapplication.Create(application);
excelworkbook1:=texcelworkbook.Create(application);
excelworksheet1:=texcelworksheet.Create(application);
excelapplication1.Connect;
except
application.MessageBox('excel 没有安装','hello',mb_iconerror+mb_ok);
exit;
end;
try
excelapplication1.Workbooks.Add(emptyparam,0);
excelworkbook1.ConnectTo(excelapplication1.Workbooks[1]);
excelworksheet1.ConnectTo(excelworkbook1.Worksheets[1] as worksheet);
ads.First;
for j:=0 to ads.Fields.Count - 1 do
begin
excelworksheet1.Cells.Item[3,j+1]:=ads.Fields[j].DisplayLabel;
excelworksheet1.Cells.Item[3,j+1].font.size:='10';
end;
for i:=4 to ads.RecordCount+3 do
begin
for j:=0 to ads.Fields.Count - 1 do
begin
excelworksheet1.Cells.Item[i,j+1]:=ads.Fields[j].AsString;
excelworksheet1.Cells.Item[i,j+1].font.size:='10';
end;
ads.Next;
end;
excelworksheet1.Columns.AutoFit;
excelworksheet1.Cells.Item[1,2]:=title;
excelworksheet1.Cells.Item[1,2].font.size:='14';
excelworksheet1.SaveAs(filename);
application.MessageBox(pchar('数据成功导出'+filename),'hello',mb_ok);
finally
excelapplication1.Disconnect;
excelapplication1.Quit;
excelapplication1.Free;
excelworksheet1.Free;
excelworkbook1.Free;
end;
end;
{$R *.dfm}
procedure TForm1.BitBtn3Click(Sender: TObject);
begin
if (messagedlg('将该表中的数据导出到EXCEL中,是否继续?',mtwarning,[mbyes,mbno],0)=mryes)then
begin
bitbtn3.Enabled:=false;
if adoconnection2.Connected then
adoconnection2.Close;
WriteExcel(adodataset1,'采购预算','采购预算');
bitbtn3.Enabled:=true;
end;
end;
我是通过bitbtn这个按纽实现excel的导出上面的函数调用是不是有问题哈
请高手指点
运行出现下面的错误
[Error] Unit1.pas(42): Declaration of 'WriteExcel' differs from previous declaration
[Error] Unit1.pas(44): Undeclared identifier: 'extracfilepath'
[Error] Unit1.pas(44): Undeclared identifier: 'sname'
[Error] Unit1.pas(57): Undeclared identifier: 'worksheet'
[Warning] Unit1.pas(59): For loop control variable must be simple local variable
[Warning] Unit1.pas(64): For loop control variable must be simple local variable
[Warning] Unit1.pas(66): For loop control variable must be simple local variable
[Error] Unit1.pas(74): Undeclared identifier: 'title'
[Error] Unit1.pas(94): Too many actual parameters
[Error] Unit1.pas(94): Too many actual parameters
[Fatal Error] Project1.dpr(5): Could not compile used unit 'Unit1.pas'
拜托各位了
另有没江苏的朋友哈
小弟目前昆山工作
希望能找些朋友指点指点偶
偶刚毕业的
啥都不懂
 
没人指点吗
 
function WriteExcel(ads:tadodataset):string;
//*上面是你的定义,跟你下面的调用的参数不同啊!*//
WriteExcel(adodataset1,'采购预算','采购预算');
把定义修改为:
function WriteExcel(ads:tadodataset; sname,title:string):string;
 
那怎么搞
帮下啊
 
怎么还是没人指教啊[:(]
 
我也新手~慢慢读懂它意思~能翻译出来就什么错误了~~
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ADODB, DB, Grids, DBGrids, ExcelXP, OleServer;

type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
ADODataSet1: TADODataSet;
ADOConnection1: TADOConnection;
ADOTable1: TADOTable;
ADOConnection2: TADOConnection;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
BitBtn4: TBitBtn;
ExcelApplication1: TExcelApplication;
ExcelWorksheet1: TExcelWorksheet;
ExcelWorkbook1: TExcelWorkbook;
function WriteExcel(ads:tadodataset;sname,title:string):string;
procedure BitBtn3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
excelapplication1:texcelapplication;
excelworkbook1:texcelworkbook;
excelworksheet1:texcelworksheet;
filename:string;
i,j:integer;
implementation
{$R *.dfm}
function tform1.WriteExcel(ads:tadodataset;sname,title:string):string;
begin
filename:=concat(ExtractFilePath(application.exename),sname,'.xls');
try
excelapplication1:=texcelapplication.Create(application);
excelworkbook1:=texcelworkbook.Create(application);
excelworksheet1:=texcelworksheet.Create(application);
excelapplication1.Connect;
except
application.MessageBox('excel 没有安装','hello',mb_iconerror+mb_ok);
exit;
end;
try
excelapplication1.Workbooks.Add(emptyparam,0);
excelworkbook1.ConnectTo(excelapplication1.Workbooks[1]);
excelworksheet1.ConnectTo(excelworkbook1.Worksheets[1] as ExcelWorksheet);
ads.First;
for j:=0 to ads.Fields.Count - 1 do
begin
excelworksheet1.Cells.Item[3,j+1]:=ads.Fields[j].DisplayLabel;
excelworksheet1.Cells.Item[3,j+1].font.size:='10';
end;
for i:=4 to ads.RecordCount+3 do
begin
for j:=0 to ads.Fields.Count - 1 do
begin
excelworksheet1.Cells.Item[i,j+1]:=ads.Fields[j].AsString;
excelworksheet1.Cells.Item[i,j+1].font.size:='10';
end;
ads.Next;
end;
excelworksheet1.Columns.AutoFit;
excelworksheet1.Cells.Item[1,2]:=title;
excelworksheet1.Cells.Item[1,2].font.size:='14';
excelworksheet1.SaveAs(filename);
application.MessageBox(pchar('数据成功导出'+filename),'hello',mb_ok);
finally
excelapplication1.Disconnect;
excelapplication1.Quit;
excelapplication1.Free;
excelworksheet1.Free;
excelworkbook1.Free;
end;
end;

procedure TForm1.BitBtn3Click(Sender: TObject);
begin
if (messagedlg('将该表中的数据导出到EXCEL中,是否继续?',mtwarning,[mbyes,mbno],0)=mryes)then
begin
bitbtn3.Enabled:=false;
if adoconnection2.Connected then
adoconnection2.Close;
WriteExcel(adodataset1,'采购预算','采购预算');
bitbtn3.Enabled:=true;
end;
end;

end.
 
后退
顶部