colspan not merging columns in MS Word

Feb 25, 2014 at 4:15 AM
Hi,
I am facing an issue in htmltodocx library as colspan of table not merging columns when it is opened in MS Word. The cell is being displayed broken without merge.

I have implemented the below example :-
https://htmltodocx.codeplex.com/documentation

Its working properly in LibreOffice but not in MS Word.

Please let me know if anyone has solution for this.

Regards,
Vivek
May 20, 2014 at 6:58 PM
Edited May 20, 2014 at 7:56 PM
I have the same issue! Works well on OpenOffice too, but it doesn't on MS Word. Well I found a solution, it's not pretty but it's something:
  1. Do all the steps from the following link: http://stackoverflow.com/questions/11925337/how-to-merge-cells-in-phpword-or-to-have-different-number-of-cells-in-each-tabl
In case the link expires here's the code: (It's not my code though, I got it from the link)
1 PHPWord/Section/Table/Cell.php
    /**
     * How many columns this cell spans
     * @var int
     */
    private $_gridSpan;

    public function__construct($insideOf,$pCount,$width=null,$style=null,$gridSpan=1){
            $this->_insideOf = $insideOf;
            ...
            $this->_gridSpan = $gridSpan;
    }

    /**
    * Get the number of columns this cell spans
    *
    * @return int
    */
    public function getGridSpan(){
            return $this->_gridSpan;
    }

2 PHPWord/Section/Table.php
     public function addCell($width,$style=null,$gridSpan=1){
            $cell = new PHPWord_Section_Table_Cell($this->_insideOf,
                    $this->_pCount,$width,$style,$gridSpan);
      }

    3 PHPWord/Writer/Word2007/Base.php

      protected function _writeTable(PHPWord_Shared_XMLWriter $objWriter = null,            PHPWord_Section_Table $table) {
                $_rows = $table->getRows();
        $_cRows = count($_rows);
                ...

                    $_heights = $table->getRowHeights();
            for($i=0; $i<$_cRows; $i++) {
               $row = $_rows[$i];
               $height = $_heights[$i];
                           //add
                            $objWriter->startElement('w:trPr');
                     //FIXME:Make this an option on a row or table
                     $objWriter->startElement('w:cantSplit');
                     $objWriter->endElement();
                $objWriter->endElement();
                           //end add
                           foreach($rows as $cell){

                                  ...
                                  $width = $cell->getWidth();
                                  //add 
                                  $gridSpan = $cell->getGridSpan();
                                  //end add
                                   ...
                                  //add
                                  if($gridSpan > 1){
                                      $objWriter->startElement('w:gridSpan');
                                          $objWriter->writeAttribute('w:val',$gridSpan);
                                      $objWriter->endElement();
                                   }
                                  //end add
                             ...
                                  }
                           }          
}
  1. After you're done with that, you need to modify htmltodocx_converter/h2d_htmlconverter.php
function htmltodocx_insert_html_recursive(&$phpword_element, $html_dom_array, &$state = array()) {
 ....
  case 'table':
  ...
  //replace this line:
   if (is_object($state['phpword_object']) && method_exists($state['phpword_object'], 'addTableStyle')) {
              $state['phpword_object']->addTableStyle('temp_table_style', $state['current_style']);
              $table_style = 'temp_table_style';
            }
  // with this:
 if (is_object($state['phpword_object']) && method_exists($state['phpword_object'], 'addTableStyle')) {
              $state['phpword_object']->addTableStyle('temp_table_style', $state['current_style'], null);
              $table_style = 'temp_table_style';
            }
  //notice the 'NULL' added

  ...
  case 'td':
  case 'th':
  ...
   //replace this line:
  $state['table_cell'] = $state['table']->addCell($cell_width, $state['current_style']);

   //with this:
  $state['table_cell'] = $state['table']->addCell($cell_width, $state['current_style'], $element->colspan);
And that's it. I tested it and worked great for me. Hope it's any help!