php - 在 Laravel 中将数据导出到 Excel

标签 php excel laravel laravel-5 laravel-5.1

我想在我的项目中将数据导出到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');
    }

这是我的问题结果:
result
它应该是:
should
我的问题是如何在标题表中将数字更改为我想要的文本。
我必须对代码进行哪些改进才能实现我的目标?
注意 : 我用 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/

相关文章:

excel - 清理 VBA 代码以引用代码而不是复制它

excel - 确定 VBA 2007 中对象变量的库名称..?

php - Laravel 作业将 exec() 错误转储到命令行而不是 $output

php - mysql查询选择取决于2个表

excel - 排序列表但保留单个单元格的格式

php - Laravel 5.3 中三个表的 sql 连接

php - Laravel 5.2 - 方法链接不存在

mysql - 如何在 Laravel 中显示使用同一数据库表中两个不同值的图表?

javascript - 带有代理的 html2canvas

php - 在 wsdl 中使用 targetNamespace