我正在尝试允许用户使用带有产品信息的Laravel Excel文件下载Excel。我当前的网络路线如下:
Route::get('/excel/release', 'ExcelController@create')->name('Create Excel');
我当前的导出看起来像这样:
class ProductExport implements FromQuery
{
use Exportable;
public function __construct(int $id)
{
$this->id = $id;
}
public function query()
{
return ProductList::query()->where('id', $this->id);
}
}
我当前的控制器如下:
public function create(Request $request) {
# Only alowed tables
$alias = [
'product_list' => ProductExport::class
];
# Ensure request has properties
if(!$request->has('alias') || !$request->has('id'))
return Redirect::back()->withErrors(['Please fill in the required fields.'])->withInput();
# Ensure they can use this
if(!in_array($request->alias, array_keys($alias)))
return Redirect::back()->withErrors(['Alias ' . $request->alias . ' is not supported'])->withInput();
# Download
return (new ProductExport((int) $request->id))->download('iezon_solutions_' . $request->alias . '_' . $request->id . '.xlsx');
}
当我转到https://example.com/excel/release?alias=product_list&id=1
时,它会正确执行并返回一个excel文件。但是,行没有列标题。数据如下所示:
1 150 1 3 2019-01-16 16:37:25 2019-01-16 16:37:25 10
但是,其中应包含ID,费用等列标题。如何在此输出中包含列标题?
答案 0 :(得分:1)
根据documentation,您可以将您的班级更改为使用WithHeadings
接口,因此它应如下所示:
<?php
namespace App;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
class ProductExport implements FromQuery, WithHeadings
{
use Exportable;
public function __construct(int $id)
{
$this->id = $id;
}
public function query()
{
return ProductList::query()->where('id', $this->id);
}
public function headings()
{
return ["your", "headings", "here"];
}
}
答案 1 :(得分:0)
<?php
namespace App\Exports;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
use DB;
class LocationTypeExport implements FromCollection,WithHeadings
{
public function collection()
{
$type = DB::table('location_type')->select('id','name')->get();
return $type ;
}
public function headings(): array
{
return [
'id',
'name',
];
}
}
答案 2 :(得分:0)
files
答案 3 :(得分:0)
此代码对我有用
RUN set -ex && \
yum install -y tar gzip && \
<Other set of commands which includes mkdir, curl, tar>
rm -vr properties && \
if [ ${arg} == "prod" ]; then rm -v conf/args.properties; fi
答案 4 :(得分:-1)
您可以将其与 array_keys
结合使用以动态获取列标题:
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
class ProductExport implements FromQuery, WithHeadings
{
use Exportable;
public function __construct(int $id)
{
$this->id = $id;
}
public function query()
{
return ProductList::query()->where('id', $this->id);
}
public function headings(): array
{
return array_keys($this->query()->first()->toArray());
}
}
如果您将它与集合一起使用,您可以这样做:
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithHeadings;
class ProductExport implements FromCollection, WithHeadings
{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
// for selecting specific fields
//return ProductList::select('id', 'product_name', 'product_price')->get();
// for selecting all fields
return ProductList::all();
}
public function headings(): array
{
return array_keys($this->collection()->first()->toArray());
}
}