我重写了TDBGridEhExportAsXLS.WriteTitle函数,到合并单元格的时候不会写了(技术有点洼),思路也有了,请大家帮我完善一下,相信对大家都有用的。谢谢!
procedure TDBGridEhExportAsXLS.WriteTitle(ColumnsList: TColumnsEhList);
var
 i, k,m: Integer;
  TitleStr:String;
  Function GetTitleRow:Integer;//获取表头最大层数
  var
   i, k,m: Integer;
    MaxRow,Row:Integer;
  begin
    MaxRow:=1;
    for i:=0 to ColumnsList.count-1 do
      begin
        Row:=1;
        m:=0;
        for j:=0 to Length(ColumnsList[i].Title.caption)-2 do
          begin
            if (ColumnsList[i].Title.Caption[j]='|') and ((j-m)>0) and
              (ColumnsList[i].Title.Caption[j+1]<>'|') then
              begin
                Inc(Row);
                m:=j+1;
              end;
          end;
        if MaxRow<Row then
          MaxRow:=Row;
      end;
    Result:=MaxRow;
  end; 
begin
 if (DBGridEh.UseMultiTitle=false) or (GetTitleRow=1) then
   begin
    for i := 0 to ColumnsList.Count - 1 do
      begin
       WriteStringCell(ColumnsList[i].Title.Caption);
      end;
   end
  else
    begin
      for i:=0 to ColumnsList.Count-1 do
        begin
          Frow:=0;
          TitleStr:='';
          m:=0;
          for j:=0 to Length(ColumnsList[i].Title.caption)-2 do//写表头
          begin
            if (ColumnsList[i].Title.Caption[j]='|') and ((j-m)>0) and //写非最后一行表头
              (ColumnsList[i].Title.Caption[j+1]<>'|') then
              begin
                WriteStringCell(TitleStr);
                if i=ColumnsList.count-1 then
                  begin
                    Fcol:=i;
                  end
                else
                  begin
                    Fcol:=Fcol-1;
                    Inc(Frow);
                  end;
                m:=j+1;
                TitleStr:='';
              end
            else
              begin
                TitleStr:=TitleStr+ColumnsList[i].Title.Caption[j];
              end;
          end;
          Frow:=GetTitleRow-1;
          TitleStr:=TitleStr+ColumnsList[i].Title.Caption[j];
          WriteStringCell(TitleStr); //写最后一行表头         
        end;
      
      begin
        //此处为:按行从左到又合并相临的不为空的内容相同的单元格
      end;
      begin
        //此处为:按列从下到上将内容不为空的单元格和它上面内容为空的单元格合并
      end;
      Fcol:=0;
      Frow:=GetTitleRow;
    end;
end;

解决方案 »

  1.   


    这个你参考下 for inti:=0 to length(columnNo)-1  do
          begin            s:=DBGridEh1.Columns[columnNo[inti]].Title.Caption ;
                i:=pos('|',s);
                if i<>0 then
                begin
                  StrT1:=copy(s,1,i-1);
                  strT2:=copy(s,i+1,length(s)-i);
                  //±&Egrave;&frac12;&Iuml;&ordm;ó&Ograve;&raquo;&cedil;&ouml;&Ecirc;&yacute;&frac34;&Yacute;              if inti<length(columnNo)-1    then
                  begin
                     s:=DBGridEh1.Columns[columnNo[inti+1]].Title.Caption ;
                     i:=pos('|',s);
                     StrT3:=copy(s,1,i-1);
                     strT4:=copy(s,i+1,length(s)-i);
                  end;              if StrT1=strT3 then
                  begin
                     setlength(arrayA,length(arrayA)+1);
                     arrayA[high(arrayA)]:=StrT2;
                     continue;
                  end
                  else
                  begin
                     setlength(arrayA,length(arrayA)+1);
                     arrayA[high(arrayA)]:=StrT2;
                     if length(arrayA)>=1 then
                     begin
                     //±ê&Igrave;&acirc;1
                      if (inti-length(arrayA)+1)>=26 then
                      begin
                         intc:=(inti+intx-length(arrayA)) div 26;
                         intk:=(inti-length(arrayA)+1) mod 26;
                         crstr:=chr(65+intc-1)+chr(65+inti-length(arrayA)+1-26*intc)+'3'+':'+chr(65+intc-1)+chr(65+inti-26*intc)+'3';
                      end
                      else
                         crstr:=chr(65+inti-length(arrayA)+1)+'3'+':'+chr(65+inti)+'3';                  // crstr:='A1:K1';
                      FillRectText(strT1 ,crstr);
                      for intx:=1 to length(arrayA)  do
                      begin
                        //±ê&Igrave;&acirc;2
                        if (inti+intx-length(arrayA))>=26 then
                        begin
                         intc:=(inti+intx-length(arrayA)) div 26;
                         intk:=(inti+intx-length(arrayA)) mod 26;
                     //  crstr:=chr(65+intk)+chr(65+inti+intx-length(arrayA))+'4'+':'+chr(65+inti)+'4';
                         crstr:=chr(65+intc-1)+chr(65+inti+intx-length(arrayA)-26*intc)+'4';
                      end
                      else
                        crstr:=chr(65+inti+intx-length(arrayA))+'4';                    FillRectText(arrayA[intx-1] ,crstr);
                      end;
                     end ;
                     setlength(arrayA,0);
                  end;
                end
                else
                begin
                   stra:=DBGridEh1.Columns[columnNo[inti]].Title.Caption ;
                   crstr:=chr(65+inti)+'3';
                   FillRectText(stra,crstr);
                end;
          end ;
             if length(arrayA)>=1 then
             begin
                //±ê&Igrave;&acirc;1
                if (inti-length(arrayA)+1)>=26 then
                begin
                   intc:=(inti-length(arrayA)) div 26;
                   intk:=(inti-length(arrayA)+1) mod 26;
                   crstr:=chr(65+intc-1)+chr(65+inti-length(arrayA)-26*intc)+'3'+':'+chr(65++intc-1)+chr(65+inti-1-26*intc)+'3';
                end
                else
                    crstr:=chr(65+inti-length(arrayA))+'3'+':'+chr(65+inti-1)+'3';
                // crstr:='A1:K1';
                FillRectText(strT1 ,crstr);
                for intx:=1 to length(arrayA)  do
                begin               if (inti+intx-length(arrayA))>=26 then
                   begin
                         intc:=(inti+intx-length(arrayA)) div 26;
                         intk:=(inti+intx-length(arrayA)) mod 26;
                     //  crstr:=chr(65+intk)+chr(65+inti+intx-length(arrayA))+'4'+':'+chr(65+inti)+'4';
                         crstr:=chr(65+intc-1)+chr(65+inti+intx-length(arrayA)-1-26*intc)+'4';
                   end
                   else
                      crstr:=chr(65+inti+intx-length(arrayA)-1)+'4';
                   FillRectText(arrayA[intx-1] ,crstr);
                end;
             end ;
             setlength(arrayA,0);          dbgrideh1.DataSource.DataSet.First;
             for inti:=0 to dbgrideh1.DataSource.DataSet.RecordCount-1 do
             begin            for intx:=low(columnNo) to  high(columnNo) do
                begin
                  stra:=DBGridEh1.Columns[columnNo[intx]].FieldName;
                  s:=dbgrideh1.DataSource.DataSet.fieldbyname(stra).asstring;
                  if intx>=26 then
                  begin
                      intc:=(inti+intx-length(arrayA)) div 26;
                      crstr:=chr(65+intc-1)+chr(65+intx-26*intc)+inttostr(5+inti);
                  end
                  else
                     crstr:=chr(65+intx)+inttostr(5+inti);
                  if  intx=0 then
                     s:=''''+s;
                  FillRectText(s ,crstr);           end;
               dbgrideh1.DataSource.DataSet.next;           //-------------------------------------
              { for inttmp:=0 to length(columnNo)-1  do
               begin              s:=DBGridEh1.f
                  FillRectText(s ,crstr);
               end;  }           //-------------------------------------
            end;
      

  2.   

    提供思路For Row:=1 to RowDepth-1 Do
      Begin
        CapStr:=trim(exApp.Cells[Row,1].value);
        Col1:=2;
        Col2:=FldCount;
        For Col1:=2 to Col2 Do
        begin
          NewCapStr:=trim(exApp.Cells[Row,Col1].value);
          if (NewCapStr=CapStr) then
          Begin
            Cell1:=exApp.Cells.Item[Row,Col1-1];
            Cell2:=exApp.Cells.Item[Row,Col1];
            exApp.Cells[Row,Col1].value:='';
            exApp.Range[Cell1,Cell2].Merge(True);
          end
          else
          begin
            CapStr:=NewCapStr;
          end;
        end;
      

  3.   

    N天了,都没人用过吗?????
    哪们老大给发份完整的啊
    [email protected]感激不尽!