title: PHPExcel - Clone sheet and keep its original style
body: <p>I've tried to check every possible similar solution both here and in the PHPExcel official documentation / forums, but I didn't find any solution to my issue.</p>
  
  <blockquote>
    <p><strong>The problem</strong></p>
  </blockquote>
  
  <p>I'm trying to <strong>clone</strong> (or copy, being honest) a sheet to parse it into another file created through phpexcel by <strong>keeping the style of the cloned sheet</strong>.</p>
  
  <p>The setup is:</p>
  
  <p><em>sheet.xls</em> &lt;--- File to OPEN &amp; COPY</p>
  
  <p>PHPExcel object &lt;-- File that gets created X times in a for loop, where I need to append Y Sheets according to a set of arrays.</p>
  
  <blockquote>
    <p><strong>What works</strong></p>
  </blockquote>
  
  <p>The cloning &amp; appending works beautifully, takes time because of some strange notices related to a phpexcel file:</p>
  
  <p>Notice: Undefined offset: 1 in \serverpath\PHPExcel\Classes\PHPExcel.php on line 729</p>
  
  <p>Notice: Undefined offset: 2 in \serverpath\PHPExcel\Classes\PHPExcel.php on line 729</p>
  
  <p>Notice: Undefined offset: 3 in \serverpath\PHPExcel\Classes\PHPExcel.php on line 729</p>
  
  <p>Notice: Undefined offset: 4 in \serverpath\PHPExcel\Classes\PHPExcel.php on line 729</p>
  
  <p>EDIT ::</p>
  
  <p>Line 729 refers to this:</p>
  
  <pre><code>foreach ($sheet-&gt;getCellCollection(false) as $cellID) {
      $cell = $sheet-&gt;getCell($cellID);
      ++$countReferencesCellXf[$cell-&gt;getXfIndex()]; // line 729
  }
  </code></pre>
  
  <p>Which is about styles as far as I can tell.
  &lt;-- There are thousand of these, no idea where they are coming from though, the files are getting generated correctly, they just lose their format as said above.</p>
  
  <blockquote>
    <p><strong>What doesn't work</strong></p>
  </blockquote>
  
  <p>The generated files LOSES the original format but keeps the formula, hence every single border (and any style) of the original "template" (sheet.xls) is lost.</p>
  
  <blockquote>
    <p><strong>The relevant part of the code</strong></p>
  </blockquote>
  
  <p>I'm only posting the really relevant code here, mostly because it's about a thousand lines of code.</p>
  
  <p>File that will later be saved creation (happens in parent foreach) :</p>
  
  <pre><code>$file       =   new PHPExcel();
  </code></pre>
  
  <p>Cloning (happens inside a child foreach after the creation above) :</p>
  
  <pre><code>$sd = $objReader-&gt;load("sheet.xls");
  $sc =   $sd -&gt;getActiveSheet()-&gt;copy();
  $clonedSheet = clone $sc;
  </code></pre>
  
  <p>Appending (happens N times inside a child foreach of the cloning above) :</p>
  
  <pre><code>$ficheName = "not relevant tbh and less than 31 characters";
  $temporarySheet = clone $clonedSheet;
  $temporarySheet-&gt;setTitle($ficheName);
  $file-&gt;addSheet($temporarySheet,0);
  $file-&gt;setActiveSheetIndex($file-&gt;getIndex($temporarySheet));
  unset($temporarySheet);
  
  // some actions are done here
  </code></pre>
  
  <p>Saving (outside of the foreach, happens in the same foreach where the PHPExcel object gets created: </p>
  
  <pre><code>$objWriter = PHPExcel_IOFactory::createWriter($file, 'Excel5');
  $objWriter-&gt;save($filename);
  </code></pre>
  
  <blockquote>
    <p><strong>Restrictions</strong></p>
  </blockquote>
  
  <p>I have absolutely no restrictions about what kind of excel format I'm supposed to use, I'm using 2003 because I have some machines that only works with excel 2003, but they will soon be upgrading to office 2010, so literally any reader and writer is okay, I'm using 2003 because I've always used it and had no problem so far.</p>
  
  <p>I am forced, though, to clone the XLS sheet inside another file, the only possible trick I can do is clone the sheet inside the same file and save it later by keeping the original one, but if there is any other chance to "export" the style I would really appreciate it.</p>
  
  <blockquote>
    <p>What I have already checked:</p>
  </blockquote>
  
  <p><a href="https://stackoverflow.com/questions/5913818/phpexcel-clone-xlsm-with-macros">PHPExcel clone .xlsm with macros</a></p>
  
  <p><a href="http://www.mindfiresolutions.com/Cloning-a-XLS-worksheet-in-PHP--Mindfire-Solutions-933.php" rel="nofollow noreferrer">http://www.mindfiresolutions.com/Cloning-a-XLS-worksheet-in-PHP--Mindfire-Solutions-933.php</a></p>
  
  <p><a href="https://stackoverflow.com/questions/26211682/phpexcel-1-8-0-creating-many-sheets-by-cloning-a-template-sheet-gets-slower-wi">PHPExcel 1.8.0 - Creating many sheets by cloning a template sheet gets slower with each clone</a></p>
  
  <p><a href="https://stackoverflow.com/questions/18258318/workaround-for-copying-style-with-phpexcel">Workaround for copying style with PHPExcel</a></p>
  
  <p>EDIT ::</p>
  
  <p>I've also tried to:</p>
  
  <ul>
  <li>Open the file and get the sheet instead of cloning the original one - Problem persists.</li>
  <li>Tried to use Excel2007 both for reading and writing - Problem persist.</li>
  <li>Tried NOT to use ->copy() - Problem persists.</li>
  <li>UPDATED phpexcel to 1.8, now the Notice above appears on line 1079, but refers to the same exact piece of code - Problem persists.</li>
  </ul>