Merging Cells rowspan from a Web html table.

Topics: Developer Forum, User Forum
Mar 7, 2013 at 6:09 AM
OS:Window 7
PHP Version 5.4.7
PHPExcel_1.7.8

I have a project to make like of google spreadsheet using PHP and jquery, right now I am able to create background color, text color, text-align, content, etc., with the help of this library PHPExcel. Right now I am focusing on how to merge a cells. I already did merge a colspan but I see that it's difficult on merging rowspan.

This is my code on merging colspan:
 if($content->colspan){
              
           $matches = array();

            preg_match('/([a-zA-Z]+)(\d+)/', $content->tdid, $matches );

            for($i=$matches[1],$x=0; $x<$content->colspan; $x++,$i++){ 
              $str .= $i;
            }
            
             $first = substr($str,0, 1);
             $last = substr($str, -1);
             
             $fi = $first.$matches[2];
             $la = $last.$matches[2];
             
             $worksheet->mergeCells($fi.":".$la);
             
             unset($str);   
          
      }
and this work out fine. But now I got a problem merging rowspan. . Every cell has a diffrent ID. . "$content->tdid" . .

Anyone that has the Idea how to do it! ?? Thanks!!
Mar 7, 2013 at 7:51 AM
Answer to my question:

Just a a little common sense and I got it. .
 if($content->colspan && $content->rowspan){         
        // echo $content->tdid.'=Both<br>';
         
           $match = array();
           
           preg_match('/([a-zA-Z]+)(\d+)/', $content->tdid, $match );
           
           $rowcount = ($match[2] - 1) + $content->rowspan;
           
           
           
           for($i=$match[1],$x=0; $x<$content->colspan; $x++,$i++){ 
               
              $str .= $i;
              
            }
            
            $last = substr($str, -1);
            
            $both = $last.$rowcount;
            
            $worksheet->mergeCells($content->tdid.':'.$both);
            
            unset($str); 
        
     }elseif($content->colspan){
        
          //SETTING up COlspan Only 
         
            $matches = array();

            preg_match('/([a-zA-Z]+)(\d+)/', $content->tdid, $matches );

            for($i=$matches[1],$x=0; $x<$content->colspan; $x++,$i++){ 
              $str .= $i;
            }
            
             $first = substr($str,0, 1);
             $last = substr($str, -1);
             
             $fi = $first.$matches[2];
             $la = $last.$matches[2];
             
             $worksheet->mergeCells($fi.":".$la);
             
             unset($str); 
         
        
     }elseif($content->rowspan){
         
          //SETTING up COlspan Rowspan Only
         
          $matches = array();

           preg_match('/([a-zA-Z]+)(\d+)/', $content->tdid, $matches );
           
           $span = ($matches[2] - 1) + $content->rowspan;
           
           $thespan =  $content->tdid.':'.$matches[1].$span;
           $worksheet->mergeCells($thespan);
           
     }   
Apr 2, 2013 at 8:27 AM
hello lilmer,

Thank you for making the code very great,

I need help to create a rowspan,

I found this code in http://phpexcel.codeplex.com/discussions/275807

to generate html table to excel, but does not work rowspan, could you help me?

This code is perfect, and the more perfect if it makes the code to generate rowspan.

Can you help me? please

thanks

Sorry for my bad english.