title: Export html table to xls template PHPExcel
body: <p>All in the title, </p>
  
  <p>I just want to export a <strong>html table</strong> to <strong>.xls template</strong> using <strong>PHPExcel</strong>.</p>
  
  <p>I googled the last two days but I couldn't find a working solution.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I found this in stackoverflow, but I was unable to work with it</p>
  
  <pre><code>  // $sql = sql query e.g "select * from mytablename"
      // $filename = name of the file to download 
          function queryToExcel($sql, $fileName = 'name.xlsx') {
                  // initialise excel column name
                  // currently limited to queries with less than 27 columns
          $columnArray = array("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z");
                  // Execute the database query
                  $result =  mysql_query($sql) or die(mysql_error());
  
                  // Instantiate a new PHPExcel object
                  $objPHPExcel = new PHPExcel();
                  // Set the active Excel worksheet to sheet 0
                  $objPHPExcel-&gt;setActiveSheetIndex(0);
                  // Initialise the Excel row number
                  $rowCount = 1;
      // fetch result set column information
                  $finfo = mysqli_fetch_fields($result);
  // initialise columnlenght counter                
  $columnlenght = 0;
                  foreach ($finfo as $val) {
  // set column header values                   
    $objPHPExcel-&gt;getActiveSheet()-&gt;SetCellValue($columnArray[$columnlenght++] . $rowCount, $val-&gt;name);
                  }
  // make the column headers bold
                  $objPHPExcel-&gt;getActiveSheet()-&gt;getStyle($columnArray[0]."1:".$columnArray[$columnlenght]."1")-&gt;getFont()-&gt;setBold(true);
  
                  $rowCount++;
                  // Iterate through each result from the SQL query in turn
                  // We fetch each database result row into $row in turn
  
                  while ($row = mysqli_fetch_array($result, MYSQL_NUM)) {
                      for ($i = 0; $i &lt; $columnLenght; $i++) {
                          $objPHPExcel-&gt;getActiveSheet()-&gt;SetCellValue($columnArray[$i] . $rowCount, $row[$i]);
                      }
                      $rowCount++;
                  }
  // set header information to force download
                  header('Content-type: application/vnd.ms-excel');
                  header('Content-Disposition: attachment; filename="' . $fileName . '"');
                  // Instantiate a Writer to create an OfficeOpenXML Excel .xlsx file        
                  // Write the Excel file to filename some_excel_file.xlsx in the current directory                
                  $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
                  // Write the Excel file to filename some_excel_file.xlsx in the current directory
                  $objWriter-&gt;save('php://output');
              }
  </code></pre>