在 Laravel 中将数据导出到 Excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37958668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:03:04  来源:igfitidea点击:

Export data to Excel in Laravel

phpexcellaravellaravel-5laravel-5.1

提问by Adam Projo

I want to make export data into excel in my project, i have made it, but after I check the results, the results doesnt like what I want. here I would like to make a result there is a title table.

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

This code my 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');
    }

this my problem result : result

这是我的问题结果: 结果

and it should be :

它应该是:

should

应该

my problem is how to change the number into text what i want in the header table.

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

what improvements do i have to make to the code to achieve my goal?

我必须对代码进行哪些改进才能实现我的目标?

NB: i use maatwebsite/excel

注意:我使用 maatwebsite/excel

回答by Ana Liza Pandac

From the official 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 5thparameter:

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

Change:

改变:

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

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

how to change the number into text what i want in the header table.

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

Then you can define your own heading and prepend it to the first row of the sheet.

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

$headings = array('date start', 'date end', 'status condition', 'security', 'company');

$sheet->prependRow(1, $headings);

That should make it work.

那应该使它起作用。