use*_*861 3 laravel-5 maatwebsite-excel
我正在使用 maatwebsite 的 Laravel Excel,但在将数据导出到 xls 文件时遇到问题。我有这个查询,但我不需要显示 xls 文件中的所有列,但我需要在下载文件之前选择所有这些列来执行操作。在我的选择中,我有 8 列,但在标题中,我只有 7 列要显示,但这不起作用,因为 8\xc2\xaa 列也出现了。
\n\n我的职能:
\n\n public function toExcel($id) { $b = Budget::find($id);\n\n$budget = Budget_Item::join('budgets', 'budget__items.id_budget', '=', 'budgets.id')\n ->join('items_sizes', 'budget__items.id_itemSize', '=', 'items_sizes.id')\n ->join('items', 'items_sizes.id_item', '=', 'items.id')\n ->join('material_types', 'items_sizes.id_materialType', '=', 'material_types.id')\n ->select('items.reference AS Refer\xc3\xaancia', 'items.name AS Descri\xc3\xa7\xc3\xa3o', 'items_sizes.size AS Tamanho', 'material_types.material_type AS Material', 'budget__items.amount AS Quantidade', 'items_sizes.unit_price AS Val.Unit\xc3\xa1rio', 'budget__items.price AS Val.Total', 'budget__items.purchasePrice')\n ->where('id_budget', '=', $id)\n ->get();\n\n$budgetUpdate = [];\n$budgetUpdate[] = ['Refer\xc3\xaancia', 'Descri\xc3\xa7\xc3\xa3o', 'Tamanho', 'Material', 'Quantidade', 'Val.Unit\xc3\xa1rio', 'Val.Total'];\nforeach ($budget as $key)\n{\n if ($key->purchasePrice > 0)\n {\n $key->unit_price = $key->purchasePrice;\n }\n\n $budgetUpdated[] = $key->toArray();\n}\nExcel::create('Proposta_'.$b->reference, function($excel) use($budgetUpdated)\n{\n // Set the title\n $excel->setTitle('Proposta');\n\n $excel->sheet('Sheetname', function($sheet) use($budgetUpdated)\n {\n $sheet->fromArray($budgetUpdated, null, 'A1', false, true);\n });\n\n})->download('xls');}\nRun Code Online (Sandbox Code Playgroud)\n\n我该如何解决这个问题?
\n\n谢谢
\n在 Laravel excel 3.1 文档上测试
在控制器上
public function exportAsCsv()
{
return Excel::download(new MyExport, 'invoices.xlsx');
}
Run Code Online (Sandbox Code Playgroud)
在MyExport上,查看地图功能
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;
}
}
Run Code Online (Sandbox Code Playgroud)
还要检查这个
| 归档时间: |
|
| 查看次数: |
13650 次 |
| 最近记录: |