Array-to-CSV-export function is facing an issue in WordPress-plugin Array-to-CSV-export function is facing an issue in WordPress-plugin wordpress wordpress

Array-to-CSV-export function is facing an issue in WordPress-plugin


I'm guessing that WP is carrying on with its normal operations after this function is called so you'll get the HTML template output after the CSV. Putting in an exit statement after fpassthru should do it, but you need to be careful that this doesn't mess up anything else that Wordpress does at the end of each page response. Drupal for example has a drupal_exit() function for just this purpose. I'm not familiar enough with WP to know for sure, but the documentation for the wp_die() function suggests that you can use PHP exit without too many problems


You can use this code to generate excel file with .xlsx extension

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/alasql/0.3/alasql.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.7.12/xlsx.core.min.js"></script><script type="text/javascript">    $(document).ready(function() {    var insuranceArray = new Array(insuranceInfo);    var bills = db.bills;    //Create tabs in excel file    var opts = [{sheetid:'Insurance Information',header:true},{sheetid:'bills Info',header:false}];    alasql('SELECT * INTO XLSX("Data.xlsx",?) FROM ?', [opts, insuranceArray,bills]]);    // Close the window once you save the file    window.onfocus=function(){         window.close();    }</script>

This code works for me...


Try this at the top of the export function.

    global $wpdb;    $wpdb->hide_errors();    @set_time_limit(0);    if ( function_exists( 'apache_setenv' ) )        @apache_setenv( 'no-gzip', 1 );    @ini_set('zlib.output_compression', 0);    //@ob_clean();    @ob_end_clean(); // to prevent issue that unidentified characters when opened in MS-Excel in some servers        header( 'Content-Type: text/csv; charset=UTF-8' );        header( 'Content-Disposition: attachment; filename=export.csv' );        header( 'Pragma: no-cache' );        header( 'Expires: 0' );        $fp = fopen('php://output', 'w');