Download Excel (phpExcel) by clicking the button

Question

I am trying to download an Excel using the phpExcel library only when it receives a $ _POST parameter.

Currently I have a file called my-information.php where I have a table and a button that executes this function:

async function download_excel() {

     let myHeaders = new Headers();
     myHeaders.append("Cookie", "___asdadsadwqfqefqefqeeqasadsadsadasdsadas");

     var formdata = new FormData();

     formdata.append("generate_xlsx", 1);

     let requestOptions = {
          method: 'GET',
          headers: myHeaders,
          body: formdata,
          redirect: 'follow'
     };

     await fetch("<?= plugin_dir_url(__FILE__) ?>my-file-generator.php", requestOptions)
          .then(response => response.text())
          .then(result => console.log(result))
          .catch(err => console.log(err))
     }

In my my-file-generator.php file I have this code:

if (isset($_GET) && isset($_GET['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->getProperties()->setTitle(esc_html__('Test xlsx document', 'woocommerce'));
    $objPHPExcel->getProperties()->setSubject(esc_html__('Test xlsx document', 'woocommerce'));
    $objPHPExcel->getProperties()->setDescription(esc_html__('Test export users document for XLSX, generated using PHP classes.', 'woocommerce'));
    $objPHPExcel->getActiveSheet()->SetCellValue('A1', esc_html__('User Login', 'woocommerce'));

    $cell_counter = 1;
    $objPHPExcel->setActiveSheetIndex(0);
    for ($i = 0; $i < count($data); $i++) {
        $cell_counter++;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $cell_counter, $data[$i]['user_login']);

    }

    // Rename sheet
    $objPHPExcel->getActiveSheet()->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="usuarios.xlsx"');
    header('Cache-Control: max-age=0');

    // Save Excel file
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    ob_end_clean();
    $objWriter->save('php://output');

    exit();
}

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.

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:

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����"��%nY ��vAa �(�0����ؖg�w{&i���@�nbE�{��y��d۸l
    m��������X�(���)�����F���;WPTricks�_������c)j�x/%����E��y�
�QĿi!��K��y3��J�<���Z�1��0?Y��L%zV
c��Ib7�������a/l٥P1:��q�r���j���j0A������u�"��"����(�   �����W�M��)Tj�({ܲ�>���O���,X٭���>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�)���>��qQC���D���b!�]�i$L��3�����2n����o�T��:Z
�h����[��4�ი��]���yN�,ە��>�>��j
-'
V�)�#��EF�^6��n���8q"K��H��>_�ׄ����eƏ�<⇄�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���[=��
&$��w�7�4��r}$�M<)p��EJ2�GME���M�Ҩ9��ɨ�^w(We�$�����O��

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.

0
Romasanta 2 months 0 Answers 10 views 0

Leave an answer