title: PHPExcel CSV to XLSX
body: <p>I have a problem converting file feom CSV to XLSX format:</p>
  
  <p>Index.php</p>
  
  <pre><code>&lt;?php
  if (!isset($_FILES["file"]))
  {
  ?&gt;
  &lt;html&gt;
      &lt;body&gt;
          &lt;h1&gt;Convert CSV to XLSX&lt;/h1&gt;
          &lt;form action="index.php" method="post" enctype="multipart/form-data"&gt;
              &lt;input type="file" name="file"/&gt;
              &lt;input type="submit"/&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  &lt;?php
      exit;
  }
  
  //obtain PHPExcel from http://phpexcel.codeplex.com
  require_once('Classes\PHPExcel.php');
  require_once('CSVToExcelConverter.php');
  
  if ($_FILES["file"]["error"] &gt; 0)
  {
      echo "Error: " . $_FILES["file"]["error"];
      exit;
  }
  
  try
  {
      header('Content-type: application/ms-excel');
      header('Content-Disposition: attachment; filename='.'example.xlsx');
  
      CSVToExcelConverter::convert($_FILES['file']['tmp_name'], 'php://output');
  } catch(Exception $e) {
      echo $e-&gt;getMessage();
  }
  </code></pre>
  
  <p>CSVToExcelConverter.php</p>
  
  <pre><code>class CSVToExcelConverter
  {
      /**
       * Read given csv file and write all rows to given xls file
       * 
       * @param string $csv_file Resource path of the csv file
       * @param string $xls_file Resource path of the excel file
       * @param string $csv_enc Encoding of the csv file, use utf8 if null
       * @throws Exception
       */
      public static function convert($csv_file, $xls_file, $csv_enc=null) {
          //set cache
          $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
          PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
  
          //open csv file
          $objReader = new PHPExcel_Reader_CSV();
          if ($csv_enc != null)
              $objReader-&gt;setInputEncoding($csv_enc);
          $objPHPExcel = $objReader-&gt;load($csv_file);
          $in_sheet = $objPHPExcel-&gt;getActiveSheet();
  
          //open excel file
          $objPHPExcel = new PHPExcel();
          $out_sheet = $objPHPExcel-&gt;getActiveSheet();
  
          //row index start from 1
          $row_index = 0;
          foreach ($in_sheet-&gt;getRowIterator() as $row) {
              $row_index++;
              $cellIterator = $row-&gt;getCellIterator();
              $cellIterator-&gt;setIterateOnlyExistingCells(false);
  
              //column index start from 0
              $column_index = -1;
              foreach ($cellIterator as $cell) {
                  $column_index++;
                  $out_sheet-&gt;setCellValueByColumnAndRow($column_index, $row_index, $cell-&gt;getValue());
              }
          }
  
          //write excel file
          $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
          $objWriter-&gt;save($xls_file);
      }
  }
  </code></pre>
  
  <p>CSV File format: <a href="http://i.stack.imgur.com/EsU8Y.png" rel="nofollow">CSV file opened with Excel</a></p>
  
  <p><a href="http://i.stack.imgur.com/iMGtm.png" rel="nofollow">xlsx file I get after conversion</a></p>
  
  <p>Basicaly I would like to get output similar to original csv file, but in xmlx format, how to do that?</p>