我在视图中有数据
<div class="box-body table-responsive no-padding">
<div id="users-table-wrapper">
<table id="users_table" class="table table-hover table-striped">
<tbody>
<tr>
<th>@lang('app.soldto')</th>
<th>@lang('app.soldby')</th>
<th>@lang('app.network')</th>
<th>@lang('app.plan')</th>
<th>@lang('app.sales_date')</th>
</tr>
@if (count($sales))
@foreach ($sales as $sale)
<tr>
<td>{{ $sale->soldto ?: trans('app.n_a') }}</td>
<td>{{ $sale->first_name . ' ' . $sale->last_name }}</td>
<td>{{ $sale->name }}</td>
<td>{{ $sale->plan }}</td>
<td>{{ $sale->salesdate }}</td>
</tr>
@endforeach
@else
<tr>
<td colspan="6"><em>@lang('app.no_records_found')</em></td>
</tr>
@endif
</tbody>
</table>
{!! $sales->render() !!}
</div>
</div>
</div>
我使用分页来显示每页10行。如下所示
我想将所有这些数据下载到CSV。不再运行db查询。
答案 0 :(得分:0)
Datatable本身提供了导出csv,xls,pdf等的按钮。这是代码
$('#example').DataTable( {
dom: 'Bfrtlp',
buttons: ['csv','pdf']
} );
&#39;#示例&#39;是你的表的id
答案 1 :(得分:-1)
您可以使用以下工作代码:
public static function exportCsv()
{
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$rows = DB::select( DB::raw("select * from users
;") );
$rows = json_decode(json_encode((array) $rows), true);
//\Log::info('query'.print_r($rows,true));
ob_end_clean();
$out = fopen('php://output', 'w');
foreach($rows as $key=>$values)
{
$arr = [];
foreach ($values as $key=>$value) {
array_push($arr, $key);
}
$line = $arr;
fputcsv($out, $line);
break;
}
//fputcsv($out, array('Registration', 'Name', 'Checkin Time'));
foreach($rows as $lines)
{
$line = $lines;
fputcsv($out, $line);
}
die;
fclose($out);
}