我需要将数据导出到 csv 文件的一个表中.我可以正常获取数据,但浏览器没有生成 CSV 文件.
I need to export data in one table in a csv file. I'm able to get the data fine but the CSV file is not being generated by the browser.
我的代码是这样的:标题的问题.我只得到带有逗号分隔值的输出,但没有得到 csv 文件.
My code is like this: its the problem with headers. I'm getting only the output with commas separated values but not getting csv file.
/* Converting data to CSV */
public function CSV_GENERATE($getTable)
{
ob_clean();
global $wpdb;
$field='';
$getField ='';
if($getTable){
$result = $wpdb->get_results("SELECT * FROM $getTable");
$requestedTable = mysql_query("SELECT * FROM ".$getTable);
// echo "hey";die;//var_dump($result);die;
$fieldsCount = mysql_num_fields($requestedTable);
for($i=0; $i<$fieldsCount; $i++){
$field = mysql_fetch_field($requestedTable);
$field = (object) $field;
$getField .= $field->name.',';
}
$sub = substr_replace($getField, '', -1);
$fields = $sub; # GET FIELDS NAME
$each_field = explode(',', $sub);
$csv_file_name = $getTable.'_'.date('Ymd_His').'.csv';
# CSV FILE NAME WILL BE table_name_yyyymmdd_hhmmss.csv
# GET FIELDS VALUES WITH LAST COMMA EXCLUDED
foreach($result as $row){
for($j = 0; $j < $fieldsCount; $j++){
if($j == 0) $fields .= "
"; # FORCE NEW LINE IF LOOP COMPLETE
$value = str_replace(array("
", "
", "
", "
"), " ", $row->$each_field[$j]); # REPLACE NEW LINE WITH TAB
$value = str_getcsv ( $value , ",", """ , "\"); # SEQUENCING DATA IN CSV FORMAT, REQUIRED PHP >= 5.3.0
$fields .= $value[0].','; # SEPARATING FIELDS WITH COMMA
}
$fields = substr_replace($fields, '', -1); # REMOVE EXTRA SPACE AT STRING END
}
header("Content-type: text/x-csv"); # DECLARING FILE TYPE
header("Content-Transfer-Encoding: binary");
header("Content-Disposition: attachment; filename=".$csv_file_name); # EXPORT GENERATED CSV FILE
header("Pragma: no-cache");
header("Expires: 0");
header("Content-type: application/x-msdownload");
//header("Content-Disposition: attachment; filename=data.csv");
return $fields;
}
现在运行良好.我们可以将其用作插件.我修改了 this 帖子.感谢 sruthi sri.
This is working perfectly now. we can use this as a plugin. I modified this post. thanks to sruthi sri.
希望这对某人有所帮助:)
Hope this helps some one :)
<?php
class CSVExport
{
/**
* Constructor
*/
public function __construct()
{
if(isset($_GET['download_report']))
{
$csv = $this->generate_csv();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename="report.csv";" );
header("Content-Transfer-Encoding: binary");
echo $csv;
exit;
}
// Add extra menu items for admins
add_action('admin_menu', array($this, 'admin_menu'));
// Create end-points
add_filter('query_vars', array($this, 'query_vars'));
add_action('parse_request', array($this, 'parse_request'));
}
/**
* Add extra menu items for admins
*/
public function admin_menu()
{
add_menu_page('Download Report', 'Download Report', 'manage_options', 'download_report', array($this, 'download_report'));
}
/**
* Allow for custom query variables
*/
public function query_vars($query_vars)
{
$query_vars[] = 'download_report';
return $query_vars;
}
/**
* Parse the request
*/
public function parse_request(&$wp)
{
if(array_key_exists('download_report', $wp->query_vars))
{
$this->download_report();
exit;
}
}
/**
* Download report
*/
public function download_report()
{
echo '<div class="wrap">';
echo '<div id="icon-tools" class="icon32">
</div>';
echo '<h2>Download Report</h2>';
//$url = site_url();
echo '<p>Export the Subscribers';
}
/**
* Converting data to CSV
*/
public function generate_csv()
{
$csv_output = '';
$table = 'users';
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output = $csv_output . $row['Field'].",";
$i++;
}
}
$csv_output .= "
";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].",";
}
$csv_output .= "
";
}
return $csv_output;
}
}
// Instantiate a singleton of this plugin
$csvExport = new CSVExport();
这篇关于导出到 csv wordpress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!