Skip to content

Instantly share code, notes, and snippets.

@iredun iredun/example.php
Created Sep 26, 2019

Embed
What would you like to do?
Быстрый экпорт в эксель, собираем массив $arrayData первый массив содержит заголовки таблиц, дальше массивы данных
<?
public function actionExportNumbersData()
{
ini_set("memory_limit", "-1");
set_time_limit(0);
$numbers = Numbers::find()->all();
$spreadsheet = new Spreadsheet();
$myWorkSheet = new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, 'РАО');
$spreadsheet->addSheet($myWorkSheet, 0);
$sheet = $spreadsheet->getSheet(0);
$arrayData = [
[
'Название произведения',
'Жанр',
'Автор',
'Индекс (К, АТ, АХ и т.д.)'
]
];
/* @var $number Numbers*/
foreach ($numbers as $number) {
foreach ($number->used_work_owners_ar['name'] as $key => $workInfo) {
if ($workInfo == '') continue;
$arrayData[] = [
$workInfo,
$number->used_work_owners_ar['janr'][$key],
$number->used_work_owners_ar['author'][$key],
$number->used_work_owners_ar['index'][$key]
];
}
}
$sheet->fromArray(
$arrayData,
NULL,
'A1'
);
$nCols = count($arrayData[0]);
foreach (range(0, $nCols) as $col) {
$sheet->getColumnDimensionByColumn($col)->setAutoSize(true);
}
$myWorkSheet = new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, 'ВОИС');
$spreadsheet->addSheet($myWorkSheet, 1);
$sheet = $spreadsheet->getSheet(1);
$arrayData = [
[
'Исполнители',
'Изготовители фонограммы',
'Название фонограммы'
]
];
/* @var $number Numbers*/
foreach ($numbers as $number) {
foreach ($number->used_track_owners_ar['name'] as $key => $trackInfo) {
if ($trackInfo == '') continue;
$arrayData[] = [
$number->used_track_owners_ar['isp'][$key],
$number->used_track_owners_ar['makers'][$key],
$trackInfo
];
}
}
$sheet->fromArray(
$arrayData,
NULL,
'A1'
);
$nCols = count($arrayData[0]);
foreach (range(0, $nCols) as $col) {
$sheet->getColumnDimensionByColumn($col)->setAutoSize(true);
}
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");
$tmpResource = tmpfile();
$tmpResourceMetaData = stream_get_meta_data($tmpResource);
$tmpFileName = $tmpResourceMetaData['uri'];
//$tmpFileName = Yii::getAlias('@storage').'/export_'.date('y-m-d__H_i').'.xlsx';
$writer->save($tmpFileName);
$response = Yii::$app->getResponse();
unset($writer);
$tmpFileStatistics = fstat($tmpResource);
if ($tmpFileStatistics['size'] > 0) {
return Yii::$app->getResponse()->sendStreamAsFile($tmpResource, 'export_'.date('y-m-d_H:i').'.xlsx', []);
}
$response->on(Response::EVENT_AFTER_SEND, function() use ($tmpResource) {
fclose($tmpResource);
});
return $response->sendFile($tmpFileName, 'export_'.date('y-m-d_H:i').'.xlsx', []);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.