title: Download Excel (phpExcel) by clicking the button
body: <p>I am trying to download an Excel using the phpExcel library only when it receives a $ _POST parameter.</p>
  <p>Currently I have a file called my-information.php where I have a table and a button that executes this function:</p>
  <pre><code>async function download_excel() {
  
       let myHeaders = new Headers();
       myHeaders.append(&quot;Cookie&quot;, &quot;___asdadsadwqfqefqefqeeqasadsadsadasdsadas&quot;);
  
       var formdata = new FormData();
  
       formdata.append(&quot;generate_xlsx&quot;, 1);
  
       let requestOptions = {
            method: 'GET',
            headers: myHeaders,
            body: formdata,
            redirect: 'follow'
       };
  
       await fetch(&quot;&lt;?= plugin_dir_url(__FILE__) ?&gt;my-file-generator.php&quot;, requestOptions)
            .then(response =&gt; response.text())
            .then(result =&gt; console.log(result))
            .catch(err =&gt; console.log(err))
       }
  </code></pre>
  <p>In my my-file-generator.php file I have this code:</p>
  <pre><code>if (isset($_POST) &amp;&amp; isset($_POST['generate_xlsx'])) {
  
      require_once '../../../../../wp-blog-header.php';
      require_once plugin_dir_path(__FILE__) . 'PHPExcel.php';
      require_once plugin_dir_path(__FILE__) . 'PHPExcel/Writer/Excel2007.php';
  
      $data = generate_data_for_xslx();
  
      // Create new PHPExcel object
      $objPHPExcel = new PHPExcel();
  
      $objPHPExcel-&gt;getProperties()-&gt;setTitle(esc_html__('Test xlsx document', 'woocommerce'));
      $objPHPExcel-&gt;getProperties()-&gt;setSubject(esc_html__('Test xlsx document', 'woocommerce'));
      $objPHPExcel-&gt;getProperties()-&gt;setDescription(esc_html__('Test export users document for XLSX, generated using PHP classes.', 'woocommerce'));
      $objPHPExcel-&gt;getActiveSheet()-&gt;SetCellValue('A1', esc_html__('User Login', 'woocommerce'));
  
      $cell_counter = 1;
      $objPHPExcel-&gt;setActiveSheetIndex(0);
      for ($i = 0; $i &lt; count($data); $i++) {
          $cell_counter++;
          $objPHPExcel-&gt;getActiveSheet()-&gt;SetCellValue('A' . $cell_counter, $data[$i]['user_login']);
  
      }
  
      // Rename sheet
      $objPHPExcel-&gt;getActiveSheet()-&gt;setTitle(esc_html__('Users', 'woocommerce'));
  
      $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
  
      header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
      header('Content-Type: application/vnd.ms-excel');
      header('Content-Disposition: attachment;filename=&quot;usuarios.xlsx&quot;');
      header('Cache-Control: max-age=0');
  
      // Save Excel file
      $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
      ob_end_clean();
      $objWriter-&gt;save('php://output');
  
      exit();
  }
  
  </code></pre>
  <p>This way the file does not run, it does not download the Excel document. But if I copy the code that is inside the conditional and paste it in the my-information.php file, it executes perfectly when opening the page.</p>
  <p>I tried adding the full conditional inside the my-information.php file and fetching the same file but it doesn't work either and the console gives me this error. Apparently the problem is to do it using the $ _POST parameter:</p>
  <pre><code>XHR POST https://example.com/wp-content/plugins/generateReporte/src/includes/my-file-generator.php [HTTP/1.1 404 Not Found 7578ms]
  
  PK��������z%jQG�D�X�����������[Content_Types].xml��MN�0����&quot;��%nY ��vAa �(�0����ؖg�w{&amp;i���@�nbE�{��y��d۸l
      m��������X�(���)�����F���;@1�_������c)j�x/%����E��y�
  �QĿi!��K��y3��J�&lt;���Z�1��0?Y��L%zV
  c��Ib7�������a/l٥P1:��q�r���j���j0A������u�&quot;��&quot;����(�   �����W�M��)Tj�({ܲ�&gt;���O���,X٭���&gt;B��~׭����Ӥ6�J=�oBZ����t��X4���Cg�,���Qg��mr�L����ٯc�e��t�� Z�?����hPv��±���u�j���R������}�Cv����PK��������z%jQ��78����K�������_rels/.rels���j�0���{
  �{���1F�^ʠ�2���l�$���-}�y�����Î��O��v�y��;�؋Ӱ.JP��޵�^������Y�ű�3G�Ww�g�)���&gt;��qQC���D���b!�]�i$L��3�����2n����o�T��:Z
  �h����[��4�ი��]���yN�,ە��&gt;�&gt;��j
  -'
  V�)�#��EF�^6��n���8q&quot;K��H��&gt;_�ׄ����eƏ�&lt;⇄�Ud�v��
  T�PK��������z%jQ�$�V������������xl/_rels/workbook.xml.rels���j�0�D��
  ��ZvZJ)�s)�\[�����LlIh7m��U�H�����Ĭؙ�H���8�OL����*J��M����|4��g���[=��
  &amp;$��w�7�4��r}$�M&lt;)p��EJ2�GME���M�Ҩ9��ɨ�^w(We�$�����O��
  </code></pre>
  <p>I need to make the file generate and download only when I click the button, I have tried many ways and I can't do it, could you please help me.</p>