Export Excel file in laravel

1. Put this export button code in the view


Export

2. Put this code in the route
Route::get(‘tds/export’, [
‘as’ => ‘admin.tds.export’,
‘uses’ => ‘[email protected]
]);

3. Put this code in the controller
// Do changes as per your details

add this library
use Maatwebsite\Excel\Facades\Excel;

public function exportTds(){

$tdsDataArr = Tds::select(
‘users.first_name’,
‘users.last_name’,
‘seller_tds.unique_id as UniqueId’,
‘seller_tds.id’,
‘seller_tds.name as Name’,
‘seller_tds.email as Email’,
‘seller_tds.financial_year as FinancialYear’,
‘seller_tds.quarter as Quarter’,
‘seller_tds.certificate_number as CertificateNumber’,
‘seller_tds.amount as Amount’,
‘seller_tds.created_at as ClaimDate’,
‘seller_tds.is_approved as ApprovalStatus’
)
->leftjoin(‘users’,’users.id’, ‘=’, ‘seller_tds.seller_id’)
->get()
->toArray();

if(!empty($tdsDataArr)){
$i=0;
foreach($tdsDataArr as $k=>$v){
$tdsDataArr[$k][‘SellerName’] = $v[‘first_name’].” “.$v[‘last_name’];
unset($tdsDataArr[$k][‘id’]);
unset($tdsDataArr[$k][‘first_name’]);
unset($tdsDataArr[$k][‘last_name’]);
$i++;
}
}

Excel::create(‘TDSData’, function ($excel) use ($tdsDataArr) {
$excel->sheet(‘TDSData’, function ($sheet) use ($tdsDataArr) {
$sheet->fromArray($tdsDataArr);
$sheet->setFontFamily(‘Verdana’);
$sheet->setFontSize(10);
$sheet->row(1, function ($row) {
$row->setFontWeight(‘bold’);
});
$sheet->setBorder(‘A1’, ‘thin’);
$sheet->setHeight(1, 20);
$sheet->freezeFirstRow();
});
})->export(‘xls’)->download(‘xls’);
}

3.

Add Comment

Required fields are marked *. Your email address will not be published.