title: Export / Download Table HTML-PHP to Excel file
body: <p>I have a table on HTML/PHP that extract data from mySQL. Please see in this <a href="https://anakpanti.com/ab-cargo/controllers_searchshipment/" rel="nofollow noreferrer">link</a>. For example, please search this shipment code to get the result inside the table: <strong>42-7278954</strong>.</p>
  
  <p>After that, I want to export the result to excel (you also can see the button there).</p>
  
  <p>Below is my table code in view (I've deleted some columns to shorten the code):</p>
  
  <pre><code>$body = "";
  
  $display =  "&lt;table class='table_searchshipment' id='table_searchshipment' cellpadding='0' cellspacing='0' border='0'&gt;";
  
              echo $display;
              $body .= $display;
  
              $display  = "&lt;thead&gt;
                          &lt;tr&gt;
                            &lt;th&gt;CN No.&lt;/th&gt;
                            &lt;th&gt;Doc No.&lt;/th&gt;
                          &lt;/tr&gt;
                        &lt;/thead&gt;";
              echo $display;
              $body .= $display;  
  
              while($row = mysqli_fetch_array($result)) {
                  $CN_no= $row['CN_no'];
                  $doc_no= $row['doc_no'];
  
                  $display  = "&lt;tr&gt;
                          &lt;td&gt;".$CN_no."&lt;/td&gt;
                          &lt;td&gt;".$doc_no."&lt;/td&gt;
                        &lt;/tr&gt;";
                 echo $display;
                 $body .= $display;
              }
  
              $display  = "&lt;/table&gt;";
              echo $display;
              $body .= $display;
              $body = htmlspecialchars($body);
  
              echo '&lt;form action = "setheader" method = "post"&gt;
                      &lt;input type = "hidden" name = "body" value = "&lt;?php echo $body ; ?&gt;"&gt;
                  &lt;input type = "submit" name = "submit" Value = "Export to excel"&gt;
              &lt;/form&gt;'; 
  
              if(isset($_POST['submit'])){ 
                      $body = $_POST['body'];
                      setHeader("export.xls");
  
                      echo $body;
                  }
  </code></pre>
  
  <p>And in the controller, I make function setheader:</p>
  
  <pre><code>public function setheader($excel_file_name)//this function used to set the header variable
  {   
      header("Content-type: application/octet-stream");//A MIME attachment with the content type "application/octet-stream" is a binary file.
      //Typically, it will be an application or a document that must be opened in an application, such as a spreadsheet or word processor. 
      header("Content-Disposition: attachment; filename=$excel_file_name");//with this extension of file name you tell what kind of file it is.
      header("Pragma: no-cache");//Prevent Caching
      header("Expires: 0");//Expires and 0 mean that the browser will not cache the page on your hard drive
     }
  </code></pre>
  
  <p>The download pop-up is appear, but it only download "setheader" file that contains of error notification. Please see it <a href="https://anakpanti.com/setheader" rel="nofollow noreferrer">here</a>.</p>