Php 将一个特定行记录导出到excel文件
在Laravel5.1中,使用maatweb/Excel软件包,我需要将特定的行记录导出到当前页面id所在的Excel文件中 在我的VlistController中Php 将一个特定行记录导出到excel文件,php,excel,export,laravel-5.1,Php,Excel,Export,Laravel 5.1,在Laravel5.1中,使用maatweb/Excel软件包,我需要将特定的行记录导出到当前页面id所在的Excel文件中 在我的VlistController中 public function export() { Excel::create('Company List', function($excel) { $excel->sheet('companies', function($sheet)
public function export()
{
Excel::create('Company List', function($excel)
{
$excel->sheet('companies', function($sheet)
{
$data = Vlist::all();
$data = json_decode(json_encode($data),true);
$companies = [];
foreach ($data as $key => $value) {
$company['vname']= $value['vname'];
$company['vaddress']= $value['vaddress'];
$companies[] = $company;
}
$sheet->fromArray($companies);
});
})->download('xlsx');
}
在我的路由文件中
Route::get('/vlist/{vlist}/export' , 'VlistController@export');
在我看来
<li><a href="{{ action('VlistController@export', [$vlist->id]) }}"><i class='fa fa-link'></i> <span>Export Supplier : {!! $vlist->vname !!}</span></a></li>
上面的控制器代码列出了excel工作表中的所有记录,我只需要一条具有活动id的特定记录。如果我了解您的问题,您可以尝试使用此代码。您可以通过主键查询以检索特定行
public function export($id)
{
Excel::create('Company List', function($excel) use ($id)
{
$excel->sheet('companies', function($sheet) use ($id)
{
$data = Vlist::where('id', $id)->get();
$data = json_decode(json_encode($data),true);
$companies = [];
foreach ($data as $key => $value) {
$company['vname']= $value['vname'];
$company['vaddress']= $value['vaddress'];
$companies[] = $company;
}
$sheet->fromArray($companies);
});
})->download('xlsx');
}
我不认为下面的代码是必要的
$data = json_decode(json_encode($data),true);
$companies = [];
foreach ($data as $key => $value) {
$company['vname']= $value['vname'];
$company['vaddress']= $value['vaddress'];
$companies[] = $company;
}
您可以简单地使用检索特定数据
$data = Vlist::where('id', $id)->get(['vname', 'vaddress']);
并将其传递给方法
$sheet->fromArray($data);
谢谢我不确定你的问题,我认为你想把(显示)记录表格放在指定的行,那么我的解决方案就行了
$projectArr[] = ["col1","col2","col3","col4","col5","col6"];
Excel::create('project_directory_result_sheet', function($excel) use($projectArr) {
$excel->sheet('Project_Directory_Result', function($sheet) use( $projectArr ) {
$sheet->fromArray($projectArr,null, 'A3');
}
}
在我的解决方案中,值得注意的一点是$sheet->fromArray($projectArr,null,'A3')代码>
这里我给出了3个参数值
第一:项目值数组。
第二:显示标题true或false
第三:要表示记录的行号 虽然不太熟悉Laravel,但您似乎将id作为参数传递给了方法,但没有尝试在方法中检索或使用它。假设您将在方法中检索id,然后使用它来限制vlist结果,类似于
vlist::where('id','=',$id)代码>同样,json编码/解码是没有意义的,只需使用对象表示法就可以了$company['vname']=$value->vname
请添加OP如何检索方法中的$id
-当前这将导致未定义的变量错误,并且查询将无法添加此$data=Vlist::where('id','=',$id);给出未定义的($id)好的,正如我在评论中提到的,我并没有真正使用Laravel,所以我不知道这是否是传递参数的正确方法,但看起来对我来说是正确的。您应该通过路由传递id并从方法参数中检索它。或者,如果您知道$id,您应该使用它。公共函数导出($id){Excel::create('Company List',function($excel)use($id){$excel->sheet('companys',function($sheet)use($id){$data=Vlist::where('id',$id)->get(['vname','vaddress']);$sheet->fromArray($data);});})->下载('xlsx');}