Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Laravel中将数据导出到Excel_Php_Excel_Laravel_Laravel 5_Laravel 5.1 - Fatal编程技术网

Php 在Laravel中将数据导出到Excel

Php 在Laravel中将数据导出到Excel,php,excel,laravel,laravel-5,laravel-5.1,Php,Excel,Laravel,Laravel 5,Laravel 5.1,我想在我的项目中将数据导出到excel中,我已经做到了,但是在检查结果之后,结果与我想要的不一样。这里我想做一个结果有一个标题表 此代码用于我的控制器: public function getExport(){ Excel::create('Export data', function($excel) { $excel->sheet('Sheet 1', function($sheet) { $products=DB::table

我想在我的项目中将数据导出到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');
    }
这是我的问题结果:

它应该是:

我的问题是如何将数字更改为标题表中所需的文本

为了实现我的目标,我必须对代码进行哪些改进

NB:我使用maatwebsite/excel

来自官方:

默认情况下,导出将使用数组(或模型)的键 属性名称)作为第一行(标题列)。改变这个 您可以编辑默认的配置设置 (excel::导出。按索引生成标题)或将false作为第五个传递 参数:

更改:

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

如何将数字更改为标题表中所需的文本

然后,您可以定义自己的标题并将其前置到工作表的第一行

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

$sheet->prependRow(1, $headings);
这应该会使它起作用。

来自官方:

默认情况下,导出将使用数组(或模型)的键 属性名称)作为第一行(标题列)。改变这个 您可以编辑默认的配置设置 (excel::导出。按索引生成标题)或将false作为第五个传递 参数:

更改:

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

如何将数字更改为标题表中所需的文本

然后,您可以定义自己的标题并将其前置到工作表的第一行

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

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

这应该可以让它工作。

在你的控制器上试试这个

$data=Insertion::get()->toArray();
    return Excel::create('yourfilename', function($excel) use ($data) {
        $excel->sheet('mySheet', function($sheet) use ($data)
        {
            $sheet->cell('A1:C1',function($cell)
            {
                $cell->setAlignment('center');
                $cell->setFontWeight('bold');
            });

            $sheet->cell('A:C',function($cell)
            {
                $cell->setAlignment('center');
            });

            $sheet->cell('A1', function($cell) 
            {
                $cell->setValue('S.No');  

            });
            $sheet->cell('B1', function($cell) 
            {
                $cell->setValue('Name'); 
            });
            $sheet->cell('C1', function($cell) 
            {
                $cell->setValue('Father Name');    
            });
            if (!empty($data)) {
                $sno=1;
                foreach ($data as $key => $value) 
                {
                    $i= $key+2;
                    $sheet->cell('A'.$i, $sno); 
                    $sheet->cell('B'.$i, $value['name']); 
                    $sheet->cell('C'.$i, $value['fathername']);  
                    $sno++;
                }
            }
        });
    })->download(xlsx);

在你的控制器里试试这个

$data=Insertion::get()->toArray();
    return Excel::create('yourfilename', function($excel) use ($data) {
        $excel->sheet('mySheet', function($sheet) use ($data)
        {
            $sheet->cell('A1:C1',function($cell)
            {
                $cell->setAlignment('center');
                $cell->setFontWeight('bold');
            });

            $sheet->cell('A:C',function($cell)
            {
                $cell->setAlignment('center');
            });

            $sheet->cell('A1', function($cell) 
            {
                $cell->setValue('S.No');  

            });
            $sheet->cell('B1', function($cell) 
            {
                $cell->setValue('Name'); 
            });
            $sheet->cell('C1', function($cell) 
            {
                $cell->setValue('Father Name');    
            });
            if (!empty($data)) {
                $sno=1;
                foreach ($data as $key => $value) 
                {
                    $i= $key+2;
                    $sheet->cell('A'.$i, $sno); 
                    $sheet->cell('B'.$i, $value['name']); 
                    $sheet->cell('C'.$i, $value['fathername']);  
                    $sno++;
                }
            }
        });
    })->download(xlsx);

啊,好的,这对我有帮助。也许我可以制作样式,就像我想要制作边框表一样?关于边框样式,请参考和查看可用的边框样式。此外,如果这解决了您的问题,您可以将此标记为答案。。谢谢。:)只有第一个单元格可以工作,但之后给我编号?啊,好的,这对我有帮助。也许我可以像制作边框表一样制作样式?关于边框样式,请参考和查看可用的边框样式。此外,如果这解决了您的问题,您可以将此标记为答案。。谢谢。:)只有第一个单元格有效,但在那之后给我编号?