具体是这样的,我要在一个数据库中根据父子两类进行导出到EXCEL,现在我已经准备好记录集,并将记录集传入一个递归函数中,我的意思是按照次序导出到EXCEL,比如 
   TYPENO    PARENTNO    NAME
   1                     111NAME
   2            1        222NAME
   3            1        333NAME
   4                     444NAME
   5            2        555NAME根据这个记录集应该是这样排列
   1--111NAME
   |     |
   |     2--222NAME
   |     |    |
   |     |    5--55NAME
   |     |
   |     |           
   |     3--333NAME
   |
   |
   |
   4--444NAME导出EXCEL时候应该是这样的
  111NAME
     222NAME
        555NAME
     333NAME
  444NAME一定要导出成这样,显示的要错落有次序
我现在数据集不用大家来考虑
我现在把我定义的递归给大家看看,procedure TWLXGL.exportdata(AdoTemp:TClientDataSet;ExcelApp:olevariant;ShowId:string);
const ListNum:string='CDEFGHIJKLMN';
var Id:string;
   procedure AddData(AdoTmp:TClientDataSet);
   begin
    with AdoTmp do
        begin
          while not eof do
            begin
             .....
             省略过程
             .....
            end;
   end;begin
AdoTemp.Filtered:=false;
AdoTemp.Filter:='parentno='+ShowId;
Adotemp.Filtered:=true;
if Adotemp.RecordCount<>0 then
begin
while not adotemp.Eof do
  begin
    id:=adotemp.fieldbyname('businesstypeno').AsString;
    adotemp.Filtered:=false;
    AdoTemp.Filter:='parentno='+Id;
    Adotemp.Filtered:=true;
    if adotemp.RecordCount=0 then
    begin
    adotemp.Filtered:=false;
    AddData(Adotemp);
    exit;
    end
    else exportdata(AdoTemp,ExcelApp,id);
   next;
  end;
end;
end;请各位大虾看看这个递归为什么不能实现我要的格式,它数据是全部导出了,但不是要要的格式
它导出的是  
  111NAME
  444NAME
     222NAME
     333NAME
        555NAME
这个格式不是我要的,拜托各位了,现在很急,在线等待,如果觉得100分还少,我在加

解决方案 »

  1.   

    怎么从楼主的函数中没见到导入Excel的语句?
      

  2.   

    楼主的递规用的While是不是不合理啊
    你一个编号不会有好几个父编号把?
      

  3.   

    WHILE中那里不合理请指教
      

  4.   

    procedure AddData(AdoTmp:TClientDataSet);
        var
            num,i,j:integer;
            SheetNum,ShowNum:string;
            MsgText:string;
            MsgNum,MsgNumCount,MsgNumSpace:integer;
        begin
          num:=0;
          with AdoTmp do
            begin
              while not eof do
                begin
                  GuageNum:=GuageNum+1;
                  WProgress.LBShowName.Width:=WProgress.Canvas.TextWidth('正在导出业务:'+Fields[1].AsString)+20;
                  WProgress.LBShowName.Caption:='正在导出业务:'+Fields[1].AsString;               for i:=1 to length(ListNum) do
                   begin
                     if (num=Fields[0].AsInteger) and (i in [1,2,3,4,5,6,7,9,12]) then continue;
                     SheetNum:=ListNum[i]+inttostr(GuageNum);
                     case i of
                       8: MsgText:='正在导出预算:';
                       10: MsgText:='正在导出凭证模板:';
                       11: MsgText:='正在导出明细界面:';
                       else MsgText:='';
                   end;               if (MsgText<>'') and (AdoTemp.Fields[i].AsString<>'') then
                    begin
                      MsgNumSpace:=length(MsgText);
                      MsgText:=MsgText+Fields[i].AsString;
                      if WProgress.canvas.TextWidth(MsgText)>500 then
                        begin
                          MsgNumCount:=length(MsgText);
                          for MsgNum:=1 to MsgNumCount do
                             if WProgress.canvas.TextWidth(copy(MsgText,1,MsgNum))>500 then break;
                          WProgress.LBBudGet.Caption:=copy(MsgText,0,MsgNum+1)+#13+stringofchar(' ',MsgNumSpace-1)+copy(MsgText,MsgNum+2,length(MsgText));
                        end
                        else
                          WProgress.LBBudGet.Caption:=MsgText;
                        end;                 if i=1 then ExcelApp.activesheet.range[SheetNum+':'+SheetNum].Value:=stringofchar(' ',2*FieldByName('pilenum').AsInteger)+Fields[i].AsString
                     else ExcelApp.activesheet.range[SheetNum+':'+SheetNum].Value:=Fields[i].AsString+' ';
                   end;                if num<>Fields[0].AsInteger then
                      begin
                        num:=Fields[0].AsInteger;
                        inc(ShowListNum);
                        ExcelApp.activesheet.range['b'+inttostr(GuageNum)+':b'+inttostr(GuageNum)].Value:=inttostr(ShowListNum);
                      end;
                     WProgress.Gauge.Progress:=round(GuageNum*100/GuageCount);
                     application.ProcessMessages;
                     next;
                end;
            end;
        end;
      

  5.   

    能不能把调用这个函数exportdata的代码贴一下?
      

  6.   

    既然楼主树都容易形成了那还不如直接遍历树了
    function TForm1.AllOverTreeView(node:TTreenode):TTreenode;
    begin
    while node&lt;&gt;nil do
    begin
    if node.HasChildren then
    begin
    node:=node.getFirstChild;
    allovertreeview(node);
    node:=node.Parent;
    end;
    if node.getNextSibling<>nil then
    node:=node.getNextSibling
    else
    exit;
    end;
    end;
      

  7.   

    如果那么简单,有树就好了,我是在内存中取到了数据集,然后在不断的Filter,这样来形成新的结果集,因为是连接10个表以上,不可能修改数据,所以才需要使用递归,而且不可能从SQL中排序,并且这个SQL已经就是一个递归表了
      

  8.   

    应该是先序遍历树
    procedure SearchTree(Nodetag:integer);
    var nc:integer;
    begin
      //导出这条纪录
      //查PARENTNO为NodeTag的纪录
      while not xx.Eof do
      begin
        nc:=子节点
        SearchTree(nc);
        xx.Next;
      end;
    end;
      

  9.   

    jinjazz(人雅的标记--落寞刺客)
    那我应该在那里写ADDDATA,这个是最关键的,其他的我到不担心,你能不能结合我的程序帮我想个办法
      

  10.   

    你的Adddata是干什么用的,太长了,懒得看
      

  11.   

    似乎你应该在AddData里面调用AddData
      

  12.   

    感觉要new一个TClientDataSet来传递参数!