我想在我的项目中将数据导出到excel,我已经做到了,但是在我检查结果之后,结果并不像我想要的那样。这里我想做一个结果有一个标题表。
这段代码我的 Controller :
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
最佳答案
来自官方docs :
By default the export will use the keys of your array (or model attribute names) as first row (header column). To change this behaviour you can edit the default config setting (excel::export.generate_heading_by_indices) or pass false as 5th parameter:
更改:
$sheet->fromArray($data);
至$sheet->fromArray($data, null, 'A1', false, false);
how to change the number into text what i want in the header table.
然后,您可以定义自己的标题并将其添加到工作表的第一行。
$headings = array('date start', 'date end', 'status condition', 'security', 'company');
$sheet->prependRow(1, $headings);
那应该使它起作用。
关于php - 在 Laravel 中将数据导出到 Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37958668/