<?php
namespace
App\Http\Controllers;
use
Illuminate\Foundation\Bus\DispatchesJobs;
use
Illuminate\Routing\Controller
as
BaseController;
use
Illuminate\Foundation\Validation\ValidatesRequests;
use
Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use
PHPExcel;
use
IOFactory;
use
DB;
class
ExcelController
extends
Controller
{
public
function
Excel(){
$query
=DB::table(
'change'
)->get();
if
(!
$query
)
return
false;
$obj
=
new
PHPExcel();
include_once
(
'../app/libs/PhpExcel/PHPExcel/IOFactory.php'
);
$obj
->getProperties()-> setTitle(
"export"
) ->setDescription(
"none"
);
$obj
-> setActiveSheetIndex(0);
$fields
= DB::select(
"SHOW COLUMNS FROM `change`"
);
$col
= 0;
foreach
(
$fields
as
$field
){
$field
=
$field
[
'Field'
];
$obj
-> getActiveSheet() -> setCellValueByColumnAndRow(
$col
, 1,
$field
);
$col
++;
}
$row
= 2;
foreach
(
$query
as
$data
)
{
$col
=0;
foreach
(
$fields
as
$field
)
{
$field
=
$field
[
'Field'
];
$obj
->getActiveSheet()->setCellValueByColumnAndRow(
$col
,
$row
,!
empty
(
$data
[
"$field"
])?
$data
[
"$field"
]:
''
);
$col
++;
}
$row
++;
}
$obj
-> setActiveSheetIndex(0);
$objWriter
=IOFactory :: createWriter(
$obj
,
'Excel5'
);
header(
'Content-Type:application/vnd.ms-excel'
);
header(
'Content-Disposition:attachment;filename="Brand_'
.
date
(
'Y-m-d'
) .
'.xls"'
);
header(
'Cache-Control:max-age=0'
);
$objWriter
-> save(
'php://output'
);
}
}