php 使用 Laravel 将整个表导出为 CSV
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16036075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Export whole table to CSV using laravel
提问by Anuj
I am new to laravel and having a tough time figuring out a way to export one table to csv. I have tried the following code in the controller class, but it gives me an error:
我是 laravel 的新手,很难找到一种将一个表导出到 csv 的方法。我在控制器类中尝试了以下代码,但它给了我一个错误:
public function get_export()
{
$table = Cpmreport::all();
$file = fopen('file.csv', 'w');
foreach ($table as $row) {
fputcsv($file, $row);
}
fclose($file);
return Redirect::to('consolidated');
}
Model Class for Cpmreport:
Cpmreport 的模型类:
class Cpmreport extends Eloquent
{
public static $table='cpm_report';
}
The error :
错误 :
Message:
fputcsv() expects parameter 2 to be array, object given
Location:
C:\xampp\htdocs\cpm_report\application\controllers\cpmreports.php on line 195
Any help would be appreciated.
任何帮助,将不胜感激。
回答by Dinesh Rabara
Easy way
简单的方法
Route::get('/csv', function() {
$table = Cpmreport::all();
$output='';
foreach ($table as $row) {
$output.= implode(",",$row->toArray());
}
$headers = array(
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="ExportFileName.csv"',
);
return Response::make(rtrim($output, "\n"), 200, $headers);
});
回答by sqwk
fputcsv($file, $table);
should be fputcsv($file, $row)
, shouldn't it?
fputcsv($file, $table);
应该是fputcsv($file, $row)
,不是吗?
And convert the object to an array using Eloquent's to_array()
method: http://laravel.com/docs/database/eloquent#to-array
并使用 Eloquent 的to_array()
方法将对象转换为数组:http: //laravel.com/docs/database/eloquent#to-array
public function get_export()
{
$table = Cpmreport::all();
$file = fopen('file.csv', 'w');
foreach ($table as $row) {
fputcsv($file, $row->to_array());
}
fclose($file);
return Redirect::to('consolidated');
}
回答by Somnath Muluk
Select query of MySQL data.
选择查询 MySQL 数据。
$data = \DB::connection('mysql')->select($select);
Call following function:
调用以下函数:
query_to_csv($data, 'data.csv');
function data_to_csv($data, $filename)
{
$fp = fopen($filename, 'w');
foreach ($data as $row) {
fputcsv($fp, $row);
}
fclose($fp);
}
0.1 Million records takes 1 second to create.
创建 10 万条记录需要 1 秒。
回答by Tejendra Pareek
this is better and simple.
这更好更简单。
$file_name = "abc";
$postStudent = Input::all();
$ck = DB::table('loan_tags')->select('LAN')->where('liabilitiesId', $postStudent['id'])->get();
$i = 0;
foreach ($ck as $row) {
$apps[$i]['LAN'] = $row->LAN;
$apps[$i]['Account_number'] = $postStudent['account_number'];
$apps[$i]['Bank_Name'] = $postStudent['bank_name'];
$i++;
}
ob_end_clean();
ob_start();
Excel::create($file_name, function($excel) use($apps){
$excel->sheet('Sheetname', function($sheet) use($apps){
$sheet->row(1, array(
'LAN', 'Account number' , 'Bank Name'
));
$k = 2;
foreach ($apps as $deta) {
$sheet->row($k, array($deta['LAN'], $deta['Account_number'], $deta['Bank_Name']
));
$k++;
}
});
})->download('xlsx');