Laravel 5 Laravel Excel-选择要导出的列
我正在使用maatwebsite的Laravel Excel,但在将数据导出到xls文件时遇到问题。我有这个查询,但我不需要显示xls文件中的所有列,但我需要在下载文件之前选择所有这些列来执行操作。在我的选择中,我有8列,但在我的标题中,我只有7列要显示,但这不起作用,因为8ª列也出现了 我的职能:Laravel 5 Laravel Excel-选择要导出的列,laravel-5,maatwebsite-excel,Laravel 5,Maatwebsite Excel,我正在使用maatwebsite的Laravel Excel,但在将数据导出到xls文件时遇到问题。我有这个查询,但我不需要显示xls文件中的所有列,但我需要在下载文件之前选择所有这些列来执行操作。在我的选择中,我有8列,但在我的标题中,我只有7列要显示,但这不起作用,因为8ª列也出现了 我的职能: public function toExcel($id) { $b = Budget::find($id); $budget = Budget_Item::join('budgets', 'bu
public function toExcel($id) { $b = Budget::find($id);
$budget = Budget_Item::join('budgets', 'budget__items.id_budget', '=', 'budgets.id')
->join('items_sizes', 'budget__items.id_itemSize', '=', 'items_sizes.id')
->join('items', 'items_sizes.id_item', '=', 'items.id')
->join('material_types', 'items_sizes.id_materialType', '=', 'material_types.id')
->select('items.reference AS Referência', 'items.name AS Descrição', 'items_sizes.size AS Tamanho', 'material_types.material_type AS Material', 'budget__items.amount AS Quantidade', 'items_sizes.unit_price AS Val.Unitário', 'budget__items.price AS Val.Total', 'budget__items.purchasePrice')
->where('id_budget', '=', $id)
->get();
$budgetUpdate = [];
$budgetUpdate[] = ['Referência', 'Descrição', 'Tamanho', 'Material', 'Quantidade', 'Val.Unitário', 'Val.Total'];
foreach ($budget as $key)
{
if ($key->purchasePrice > 0)
{
$key->unit_price = $key->purchasePrice;
}
$budgetUpdated[] = $key->toArray();
}
Excel::create('Proposta_'.$b->reference, function($excel) use($budgetUpdated)
{
// Set the title
$excel->setTitle('Proposta');
$excel->sheet('Sheetname', function($sheet) use($budgetUpdated)
{
$sheet->fromArray($budgetUpdated, null, 'A1', false, true);
});
})->download('xls');}
我怎样才能解决这个问题
感谢它的价值,我遇到了一个类似的问题,没有找到多少解决方案,所以这里是我的解决方案。 1.我在回调中查询数据库,并获取所需的所有数据。 2.然后,我检查集合,在每次迭代中创建一个新数组,并分配一个值和头。非常适合从模型中拾取柱
由于它的价值,我遇到了一个类似的问题,并没有找到多少解决办法,所以这里是我的解决方案。 1.我在回调中查询数据库,并获取所需的所有数据。 2.然后,我检查集合,在每次迭代中创建一个新数组,并分配一个值和头。非常适合从模型中拾取柱 你可以这样试试。 $user\u id=Auth::user->id 你可以这样试试。 $user\u id=Auth::user->id 在Laravel excel 3.1上测试 在控制器上 在MyExport上,查看map函数 同时检查在Laravel excel 3.1上测试的 在控制器上 在MyExport上,查看map函数
同时检查我没有设置我不想要的列,但可能有excel api的解决方案。。。但是使用unset可以很好地工作。我做了我不想要的列的unset,但是可能有一个使用excel api的解决方案。。。但使用unset很好。
Excel::create('User List Export', function($excel) {
$excel->sheet('Users', function($sheet) {
$email = yourModelGoesHere::all();
foreach ($email as $key => $value) {
$payload[] = array('email' => $value['email'], 'name' => $value['name']);
}
$sheet->fromArray($payload);
});
})->download('xls');
Excel::create('User', function($excel) use ($user_id){
$excel->sheet('Sheet', function($sheet) use($user_id){
$user = User::where('user_id', $user_id)->select('name', 'email', 'role', 'address')->get();
});
})->export('xls');
return redirect('your route');
public function exportAsCsv()
{
return Excel::download(new MyExport, 'invoices.xlsx');
}
class MyExport implements FromCollection, WithHeadings, WithMapping{
public function collection(){
return MyTable:all();
}
// here you select the row that you want in the file
public function map($row): array{
$fields = [
$row->myfield2,
$row->myfield1,
];
return fields;
}
}