wordpress export csv在管理菜单页面中无效

时间:2017-11-22 06:35:40

标签: php wordpress

我在自定义主题的functions.php文件中创建了一个管理菜单页面,我想在点击该菜单时以csv格式导出数据。

我已经为此编写了以下代码,但是当我点击该菜单时,我在excel中得到了垃圾格式的结果。

enter image description here

以下是我在自定义主题的functions.php文件中编写的代码

1)添加自定义管理菜单页面

<?php
add_action('admin_menu', 'my_menu_pages');
function my_menu_pages(){
      add_menu_page('Export Payment History', 'Export Payment History', 'manage_options', 'export-history', 'export_payment_history','dashicons-image-rotate-right' );
}
?>

2)回电功能

function export_payment_history(){
global $wpdb, $user_id;
$c_user = get_current_user_id();

$filename = "users_csv.csv";
$data_rows = array();


$results = $wpdb->get_results("SELECT * FROM wp_custom_payment");

$header_row = array(
    0 => 'ID',
    1 => 'TRANSACTION ID',
    2 => 'EMAIL',
    3 => 'PAYMENT DATE',
    4 => 'TOTAL AMOUNT',

  );


$i = 1;
foreach( $results as $result ) {

  $item_unserialize = unserialize($result->item_data);

  $user_info = get_userdata($result->user_id);
  $user_email =  $user_info->user_email;

  $row = array();
    $row[0] = $i;
    $row[1] =  $result->txn_id;
    $row[2] = $user_email;
    $row[3] =  $result->payment_date;
    $row[4] = $item_unserialize['payment_total']['payment_gross'];

    $data_rows[] = $row;

  $i++;
}

$fh = @fopen( 'php://output', 'w' );
fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) );

header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Description: File Transfer' );
header( 'Content-type: text/csv' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Expires: 0' );
header( 'Pragma: public' );



fputcsv($fh, $header_row);

  foreach ($data_rows as  $data_row) {  
    fputcsv($fh, $data_row);
  }

  fclose( $fh );
  exit();

}

我在excel表中获得了垃圾价值,没有得到任何数据,请帮助我。

我输出如下 enter image description here

1 个答案:

答案 0 :(得分:0)

首先感谢所有人,我已经通过自己解决了这个问题

我刚添加stream_get_contents

完整代码..

function export_payment_history(){
global $wpdb, $user_id;
$c_user = get_current_user_id();

$filename = "users_csv.csv";
$data_rows = array();


$results = $wpdb->get_results("SELECT * FROM wp_custom_payment");

$header_row = array(
    0 => 'ID',
    1 => 'TRANSACTION ID',
    2 => 'EMAIL',
    3 => 'PAYMENT DATE',
    4 => 'TOTAL AMOUNT',    
  );


$i = 1;
foreach( $results as $result ) {

  $item_unserialize = unserialize($result->item_data);

  $user_info = get_userdata($result->user_id);
  $user_email =  $user_info->user_email;

  $row = array();
    $row[0] = $i;
    $row[1] =  $result->txn_id;
    $row[2] = $user_email;
    $row[3] =  $result->payment_date;
    $row[4] = $item_unserialize['payment_total']['payment_gross'];

    $data_rows[] = $row;

  $i++;
}

ob_end_clean();
$fh = @fopen( 'php://output', 'w' );
fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) );

header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Content-Description: File Transfer' );
header( 'Content-type: text/csv' );
header( 'Content-Disposition: attachment; filename=' . $filename );
header( 'Expires: 0' );
header( 'Pragma: public' );

fputcsv($fh, $header_row);

  foreach ($data_rows as  $data_row) {  
    fputcsv($fh, $data_row);
  }

$csvFile = stream_get_contents($fh);
fclose($fh);
die();
}