将数据导出到Laravel的Excel中

我想在我的项目中将导出数据转换为excel,但是在检查结果后,结果并不像我想要的那样。 在这里我想做一个结果是有一个标题表。

这个代码我的控制器:

public function getExport(){ Excel::create('Export data', function($excel) { $excel->sheet('Sheet 1', function($sheet) { $products=DB::table('log_patrols') ->join("cms_companies","cms_companies.id","=","log_patrols.id_cms_companies") ->join("securities","securities.id","=","log_patrols.id_securities") ->select("log_patrols.*","cms_companies.name as nama_companies","securities.name as nama_security") ->get(); foreach($products as $product) { $data[] = array( $product->date_start, $product->date_end, $product->condition_status, $product->nama_security, $product->nama_companies, ); } $sheet->fromArray($data); }); })->export('xls'); } 

这是我的问题结果: 结果

它应该是:

应该

我的问题是如何将数字更改为文本我想在表头中。

为了实现我的目标,我必须对代码做些什么改进?

注意 :我使用maatwebsite / excel

从官方文档 :

默认情况下,导出将使用数组的键(或模型属性名称)作为第一行(标题列)。 要改变这种行为,你可以编辑默认的configuration设置(excel :: export.generate_heading_by_indices)或者传递false作为第五个参数:

更改:

$sheet->fromArray($data); to $sheet->fromArray($data, null, 'A1', false, false);

如何将数字更改为文本我想要在标题表中。

然后,您可以定义自己的标题,并将其预先添加到工作表的第一行。

 $headings = array('date start', 'date end', 'status condition', 'security', 'company'); $sheet->prependRow(1, $headings); 

这应该使它工作。