Created
March 11, 2021 10:37
-
-
Save thephucit/f4ecf332fcfaa339a07310c33f8a00a3 to your computer and use it in GitHub Desktop.
Export csv file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$collection = $this->service->accountRegistrantsCollection($this->params); | |
Utils::exportCSV($collection, Consts::EXPORT_CHUNK_SIZE, $this->file->file_name, [ | |
'email' => __('excel.account_registrant.mail_address'), | |
'full_name' => __('excel.account_registrant.user_name'), | |
'email_affiliate' => __('excel.account_registrant.referral_email'), | |
'email_verified' => __('excel.account_registrant.mail_auth'), | |
'input_info' => __('excel.account_registrant.registered_information'), | |
'phone_verified' => __('excel.account_registrant.phone_verify'), | |
'status' => __('excel.account_registrant.kyc_status'), | |
'link_fb' => __('excel.account_registrant.link_fb'), | |
'otp_verified' => __('excel.account_registrant.2fa'), | |
'registration_date' => __('excel.account_registrant.registered_date'), | |
'last_login' => __('excel.account_registrant.last_login'), | |
'risk_rating' => __('excel.account_registrant.risk_rating'), | |
], function($row, $field) { | |
switch ($field) { | |
case 'email_verified': | |
return $row->email_verified === 1 ? __('excel.account_registrant.already') : __('excel.account_registrant.not_yet'); | |
case 'input_info': | |
return $row->input_info ? __('excel.account_registrant.already') : __('excel.account_registrant.not_yet'); | |
case 'phone_verified': | |
return $row->phone_verified ? __('excel.account_registrant.already') : __('excel.account_registrant.not_yet'); | |
case 'status': | |
return $row->status ? __('excel.account_registrant.' . $row->status) : __('excel.account_registrant.empty'); | |
case 'link_fb': | |
return $row->link_fb ? __('excel.account_registrant.already') : __('excel.account_registrant.not_yet'); | |
case 'otp_verified': | |
return $row->otp_verified ? __('excel.account_registrant.enable') : __('excel.account_registrant.disable'); | |
case 'registration_date': | |
return $row->registration_date ? Utils::millisecondsToDateTime($row->registration_date, $this->timeOffset, 'Y-m-d H:i:s') : ''; | |
case 'last_login': | |
return $row->last_login ? Utils::millisecondsToDateTime($row->last_login, $this->timeOffset, 'Y-m-d H:i:s') : ''; | |
default: | |
return $row->$field; | |
} | |
}, false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Export csv file | |
* | |
* @param $collection | |
* @param $chunkSize | |
* @param $fileName | |
* @param $column | |
* [ | |
* 'db_column1' => 'Heading1', | |
* 'db_column2' => 'Heading2', | |
* ] | |
* @author Phuc Nguyen | |
* @since 24/02/2021 | |
*/ | |
public static function exportCSV($collection, $chunkSize, $fileName, $columns, $resolveValue = null, $download = true) | |
{ | |
File::isDirectory(storage_path('app/exports/')) or | |
File::makeDirectory(storage_path('app/exports/'), 0777, true, true); | |
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); | |
$worksheet = $spreadsheet->getActiveSheet(); | |
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Csv($spreadsheet); | |
$writer->setDelimiter(','); | |
$writer->setEnclosure('"'); | |
$writer->setLineEnding("\r\n"); | |
$writer->setSheetIndex(0); | |
$colNumber = 'A'; | |
$heading = array_values($columns); | |
$fields = array_keys($columns); | |
$colLength = count($heading); | |
for ($i = 0; $i < $colLength; $i++) { | |
$worksheet->getCell($colNumber . '1')->setValue($heading[$i]); | |
$colNumber++; | |
} | |
$no = 1; | |
$rowNumber = 2; | |
$colNumber = 'A'; | |
$collection->chunk($chunkSize, function($rows) use ($colLength, $fields, $resolveValue, &$worksheet, &$colNumber, &$no, &$rowNumber) { | |
foreach ($rows as $row) { | |
for ($i = 0; $i < $colLength; $i++) { | |
if ($fields[$i] === 'no') { | |
$worksheet->getCell($colNumber . $rowNumber)->setValue($no); | |
} else { | |
if ($resolveValue) { | |
$worksheet->getCell($colNumber . $rowNumber)->setValue($resolveValue($row, $fields[$i])); | |
} else { | |
$worksheet->getCell($colNumber . $rowNumber)->setValue($row->{$fields[$i]}); | |
} | |
} | |
$colNumber++; | |
} | |
$no++; | |
$rowNumber++; | |
$colNumber = 'A'; # reset column | |
} | |
}); | |
$writer->save(storage_path('app/exports/' . $fileName)); | |
return $download ? | |
response()->download(storage_path('app/exports/' . $fileName))->deleteFileAfterSend(true): | |
'app/exports/' . $fileName; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment