如何在CakePHP中将mysql表数据转换为Excel或CSV格式?

时间:2023-01-15 08:40:28

I want to convert my sql data to csv files while clicking on a button. The code fragments I found for sql to CSV conversion were in PHP, and I'm trying to convert it to CakePHP since I'm working in CakePHP.

我想在点击按钮时将我的sql数据转换为csv文件。我发现用于sql到CSV转换的代码片段是在PHP中,我正在尝试将其转换为CakePHP,因为我在CakePHP中工作。

Here is the PHP code I'm tring to convert:

这是我要转换的PHP代码:

$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;

SOLUTION

Function in the Controller:

控制器中的功能:

function exporttocsv()
{       
 $this->set('headers',$this->Result->find('all',array('fields'=>'Result.label')));
 $this->set('values',$this->Result->find('all',array('fields'=>'Result.value')));       
}

exporttocsv.ctp file:

<?php

foreach($headers as $header):

     $csv_output .=$header['Result']['label'].", ";

endforeach;
$csv_output .="\n";

if(!empty($values)){
foreach($values as $value):

     $csv_output .=$value['Result']['value'].", ";

endforeach;
$csv_output .="\n";
}
else{
echo "There is no data to export.";
}

$filename = "export_".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;
?>

2 个解决方案

#1


4  

First of all, you don't do queries and output in the same file in Cake. You query the data as usual in the Controller, $this->set() the result to the view, and in the view you do something like this:

首先,您不在Cake中的同一文件中进行查询和输出。您可以像往常一样在Controller中查询数据,$ this-> set()将结果查看到视图,并在视图中执行以下操作:

foreach ($results as $result) {
    echo join(', ', $result['COLUMNS']);
    echo "\n";
}

Outputs something like this:

输出如下:

value, varchar(25), NO, , ,
submitter, int(11), NO, , ,
...

Since Cake automatically wraps a layout around your view, you'll have to set the layout to something different, like 'ajax' (which is simply an empty layout).

由于Cake会自动在视图周围包装布局,因此您必须将布局设置为不同的布局,例如'ajax'(这只是一个空布局)。

#2


1  

deceze is correct about outputting the results from the view file. You'll just need to set some headers so that it appears as a file download on the client side. You can simply put these 2 calls in the top of your view:

deceze对于从视图文件输出结果是正确的。您只需设置一些标题,使其在客户端显示为文件下载。您只需将这两个电话放在视图的顶部即可:

header("Content-type:application/vnd.ms-excel");
header("Content-disposition:attachment;filename=\"{$filename}\"" );

If you plan on doing csv downloads in more than one place in your application, I'd recommend this helper:

如果您计划在应用程序中的多个位置进行csv下载,我建议您使用以下帮助:

http://bakery.cakephp.org/articles/view/csv-helper-php5

I use it and it works well.

我用它,效果很好。

#1


4  

First of all, you don't do queries and output in the same file in Cake. You query the data as usual in the Controller, $this->set() the result to the view, and in the view you do something like this:

首先,您不在Cake中的同一文件中进行查询和输出。您可以像往常一样在Controller中查询数据,$ this-> set()将结果查看到视图,并在视图中执行以下操作:

foreach ($results as $result) {
    echo join(', ', $result['COLUMNS']);
    echo "\n";
}

Outputs something like this:

输出如下:

value, varchar(25), NO, , ,
submitter, int(11), NO, , ,
...

Since Cake automatically wraps a layout around your view, you'll have to set the layout to something different, like 'ajax' (which is simply an empty layout).

由于Cake会自动在视图周围包装布局,因此您必须将布局设置为不同的布局,例如'ajax'(这只是一个空布局)。

#2


1  

deceze is correct about outputting the results from the view file. You'll just need to set some headers so that it appears as a file download on the client side. You can simply put these 2 calls in the top of your view:

deceze对于从视图文件输出结果是正确的。您只需设置一些标题,使其在客户端显示为文件下载。您只需将这两个电话放在视图的顶部即可:

header("Content-type:application/vnd.ms-excel");
header("Content-disposition:attachment;filename=\"{$filename}\"" );

If you plan on doing csv downloads in more than one place in your application, I'd recommend this helper:

如果您计划在应用程序中的多个位置进行csv下载,我建议您使用以下帮助:

http://bakery.cakephp.org/articles/view/csv-helper-php5

I use it and it works well.

我用它,效果很好。