回 帖 发 新 帖 刷新版面

主题:[求助]存储过程的数据导出问题

在delphi中,如何将SQL2000中通过存储过程查询出的数据导出到excel中来。

回复列表 (共3个回复)

沙发

用ADOStoredProc控件name为SP1
uses Comobj;
var
        Eclapp:variant;
        i,n:integer;
begin
    With SP1 do
    begin
        close;
        ProcedureName:=存储过程的名称+';1';
        Parameters.Refresh;
        //有参数就传参
        Parameters.ParamByName('@name').Value:=Edit1.Text;
        //没有参数直接打开
        ExecProc;
        Open;
        //先导入字段名称
        for i:=1 to FieldCount - 1 do
                    begin
                        Eclapp.cells[1,i+1]:=Fields.Fields[i].FullName          
        end;
                    First;
                n:=2;
         //导入数据
                while not Eof do
                begin
                        for i := 0 to Fields.Count-1 do    // Iterate
                        begin
                                eclapp.cells[n,i+1] := Fields[i].AsString;
                        end;    // for
                        n:=n+1;
                        Next;
                end;    // while
    end;
    
end;

板凳

修改一下
用ADOStoredProc控件name为SP1
uses Comobj;
var
        Eclapp:variant;
        i,n:integer;
begin
        if application.MessageBox('确认导出excel表吗?','提示',mb_okcancel+mb_iconinformation)=idcancel then exit;
        Eclapp := createoleobject('Excel.Application');
        Eclapp.workbooks.add;
    With SP1 do
    begin
        close;
        ProcedureName:=存储过程的名称+';1';
        Parameters.Refresh;
        //有参数就传参
        Parameters.ParamByName('@name').Value:=Edit1.Text;
        //没有参数直接打开
        ExecProc;
        Open;
        //先导入字段名称
        for i:=1 to FieldCount - 1 do
                    begin
                        Eclapp.cells[1,i+1]:=Fields.Fields[i].FullName          
        end;
                    First;
                n:=2;
         //导入数据
                while not Eof do
                begin
                        for i := 0 to Fields.Count-1 do    // Iterate
                        begin
                                eclapp.cells[n,i+1] := Fields[i].AsString;
                        end;    // for
                        n:=n+1;
                        Next;
                end;    // while
    end;
    eclapp.visible := true;
end;
//希望对你有帮助

3 楼

明天测试

我来回复

您尚未登录,请登录后再回复。点此登录或注册