title: PHP Excel library sheet naming and order
body: <p>So I Am using <code>PHPExcel</code> library in order to create dynamic reports from my <code>PSQL</code> database.</p>
  
  <p>I Will first show what I am doing:</p>
  
  <pre><code>$objPHPExcel = new PHPExcel();
  $objPHPExcel-&gt;getProperties()-&gt;setCreator("Koordynator")
                               -&gt;setLastModifiedBy("SystemTest")
                               -&gt;setTitle("GeneratedTest")
                               -&gt;setKeywords("Test")
                               -&gt;setCategory("TestCategory");
  
  $db = ConnectionFactory::CreateConnection();
  $systemArray= array (
                      "systems which are processed"=&gt;"system",
                      "Persons responsible for systems"=&gt;"person",
                      "Software used inside of implementation"=&gt;"software",
                      "Hardware used inside of solution"=&gt;"hardware");
  
  $system= array ("id"=&gt;"system_id","System name"=&gt;"name","System price"=&gt;"price");
  $person= array("Firstname of admin"=&gt;"firstname","Lastname of admin"=&gt;"lastname");
  $software=array("Software id"=&gt;"id, "System Name"=&gt;"name", "System License"=&gt;"license");
  $hardware=array("id"=&gt;"id","Graphic"=&gt;"gpu","Procesor"=&gt;"cpu","Memory"=&gt;"ram");
  </code></pre>
  
  <p>In this place I will describe what those arrays are for. First one <code>$systemArray</code> contains list of tables in my database where <code>"key"=&gt;"value"</code> stands for <code>key</code>- name of sheet and A1 cell, <code>value</code> is table name.</p>
  
  <p>arrays <code>$system, $person $software and $hardware</code> are arrays which contains definition of column in each array where <code>"key"=&gt;"value"</code> stands for <code>key</code>- name of column in sheet, <code>value</code> name of column in table.</p>
  
  <p>and this is how i create the <code>XLS</code></p>
  
  <pre><code>foreach ($systemArray as $tableKey=&gt;$tableName) {
      //$q = "select * from $tableName where date &gt;= '2013-11-01' and date &lt;= '2013-11-30' order by date asc;";
      $i=0;
      foreach($$tableName as $key=&gt;$value){
          if ($i==0)
              $selectValue = $value ;
          else
              $selectValue = $selectValue." , ".$value ;
          $i++;
      }
  
      $q= "select $selectValue, date from $tableName where date &gt;= '2013-11-01' and date &lt;= '2013-11-30' order by date asc";
      $i=3;
      $s=0;
      $aa=preg_replace('/[^a-zA-Z0-9_ %\[\]\[\]\.\(\)%&amp;-]/s', '', $tableKey);
      //echo "klucz: $tableKey &lt;br&gt; a teraz $aa&lt;br&gt;";
      $objPHPExcel-&gt;createSheet($s);
      $objPHPExcel-&gt;getActiveSheet()-&gt;setTitle(substr($aa,0,30));
  
      $letters = range('A','Z');
      foreach ($db-&gt;query($q)-&gt;fetchall(PDO::FETCH_ASSOC) as $row){
              $k=1;
              //$objPHPExcel-&gt;getActiveSheet()-&gt;setTitle('1');
              $objPHPExcel-&gt;setActiveSheetIndex($s)-&gt;setCellValue('A1', $tableKey);
              foreach($$tableName as $columnkey=&gt;$columnValue){
                  if ($i == 3){
                      $objPHPExcel-&gt;setActiveSheetIndex($s)-&gt;setCellValue('A'.($i-1), 'Data');
                      if ( $k &lt;25)
                          $objPHPExcel-&gt;setActiveSheetIndex($s)-&gt;setCellValue($letters[$k].($i-1), $columnkey);
                      else 
                          $objPHPExcel-&gt;setActiveSheetIndex($s)-&gt;setCellValue('A'.$letters[($k-25)].($i-1), $columnkey);
                  }
                  if ($k&lt;25)
                      $objPHPExcel-&gt;setActiveSheetIndex($s)-&gt;setCellValue($letters[$k].$i, $row[$columnValue]);
                  else
                      $objPHPExcel-&gt;setActiveSheetIndex($s)-&gt;setCellValue('A'.$letters[($k-25)].$i, $row[$columnValue]);
                  $objPHPExcel-&gt;setActiveSheetIndex($s)-&gt;setCellValue('A'.$i, $row['date']);
                  //echo $letters[$k].$i." $columnName&lt;br&gt;";
                  $k++;
              }
              //$objPHPExcel-&gt;setActiveSheetIndex(0)-&gt;setCellValue($k.$i, $row['date']);
              $i++;
      }
      $s++;
  }
  
  $objPHPExcel-&gt;setActiveSheetIndex(0);
  </code></pre>
  
  <p>The problem I encountered is that somehow <code>sheets</code> in the <code>XLS</code> are being displayed in the wrong order and what I mean by that they are not in the order I put them in <code>$systemArray</code> the other thing is that they are named in a weird way. first one is named <code>Worksheet 1</code>, and the rest is ok but there is always <code>1</code> added at the end of the name like <code>systems which are processed 1</code>.</p>
  
  <p>Can anyone point me where I am doing the mistake?</p>