将WordPress表导出到EXCEL

时间:2011-02-26 作者:Rob Bennet

我以前创建过PHP脚本,用于将数据库表导出到。xls格式如下:

$select = "SELECT * FROM tracking";
$export = mysql_query ( $select ) or die ( "Sql error : " . mysql_error( )  );
$fields = mysql_num_fields ( $export );
$file = \'export\';
for ( $i = 0; $i < $fields; $i++ )
{
$header .= mysql_field_name( $export , $i ) . "\\t";
}
while( $row = mysql_fetch_row( $export ) )
{
$line = \'\';
foreach( $row as $value )
{
    if ( ( !isset( $value ) ) || ( $value == "" ) )
    {
        $value = "\\t";
    }
        else
    {
        $value = str_replace( \'"\' , \'""\' , $value );
        $value = \'"\' . $value . \'"\' . "\\t";
    }
        $line .= $value;
 }
 $data .= trim( $line ) . "\\n";
 }
 $data = str_replace( "\\r" , "" , $data );
 if ( $data == "" )
 {
 $data = "\\n(0) Records Found!\\n";
 }

$filename = $file."_".date("M-d-Y");

header("Content-type: application/octet-stream");
header( "Content-disposition: filename=".$filename.".xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\\n$data";
我想知道的是如何使用$wpdb类将其“转换”为wordpress格式?我可以得到初始查询以实际从我想要的表中选择数据,但我在wpdb类的一些语义中迷失了方向。我还想知道我将在wordpress中把这段代码放在哪里,所以如果用户单击链接,它将运行此查询并下载。xls文件?

非常感谢您的帮助!这个网站很棒!

5 个回复
SO网友:Derek Downey

为什么不使用SELECT INTO OUTFILE 语法:

$wpdb->query("SELECT * INTO OUTFILE \'/path/to/file\' 
 FIELDS TERMINATED BY \'\\t\' 
 LINES TERMINATED BY \'\\n\' 
 FROM tracking");

SO网友:sruthi

将其创建为“export\\u data”。php文件。然后从链接调用此php

<?php
    $host = \'localhost\';
    $user = \'mysqlUser\';
    $pass = \'myUserPass\';
    $db = \'myDatabase\';
    $table = \'products_info\';
    $file = \'export\';

    $link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

    $result = mysql_query("SHOW COLUMNS FROM ".$table."");
    $i = 0;
    if (mysql_num_rows($result) > 0) {
        while ($row = mysql_fetch_assoc($result)) {
            $csv_output .= $row[\'Field\']."; ";
            $i++;
        }
    }
    $csv_output .= "\\n";

    $values = mysql_query("SELECT * FROM ".$table."");
    while ($rowr = mysql_fetch_row($values)) {
        for ($j=0;$j<$i;$j++) {
            $csv_output .= $rowr[$j]."; ";
        }
        $csv_output .= "\\n";
    }

    $filename = $file."_".date("Y-m-d_H-i",time());
    header("Content-type: application/vnd.ms-excel");
    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
    header( "Content-disposition: filename=".$filename.".csv");
    print $csv_output;
    exit;
?>

SO网友:Nuno Sarmento

我肯定来不及帮你回答问题了。。我希望:),但我的目标是其他任何需要更新WordPress函数才能将自定义表导出到一个漂亮干净的行显示CSV文件的人

    function ns_contact_form_csv_pull() {

       global $wpdb;

       $table = \'ns_contact_form\';// table name
       $file = \'ns_contact_form_csv\'; // csv file name
       $results = $wpdb->get_results("SELECT * FROM $wpdb->prefix$table",ARRAY_A );

       if(count($results) > 0){
          foreach($results as $result){
          $result = array_values($result);
          $result = implode(", ", $result);
          $csv_output .= $result."\\n";
        }
      }

      $filename = $file."_".date("Y-m-d_H-i",time());
      header("Content-type: application/vnd.ms-excel");
      header("Content-disposition: csv" . date("Y-m-d") . ".csv");
      header( "Content-disposition: filename=".$filename.".csv");
      print $csv_output;
      exit;

    }
   add_action(\'wp_ajax_csv_pull\',\'ns_contact_form_csv_pull\');
下载CSV(需要登录)只需将此URL添加到浏览器http://example.com/wp-admin/admin-ajax.php?action=csv_pull ... 使用您的更改域:)

您可以使用以下命令在WordPress中的任何位置调用它:

$ajax_url = admin_url(\'admin-ajax.php?action=csv_pull\');

SO网友:Ashfame

如果您有phpmyadmin,那么只需转到export并选择Excel 而不是SQL.

SO网友:Rarst

类似这样:

$results = $wpdb->get_results(\'SELECT * FROM tracking;\');
请参见SELECT generic results 有关详细信息,请参见法典。

不确定如何最好地实现指向此的链接,使用自定义命名模板的页面可能会起作用。

结束