public function testPaginationBeforeModels() { $query = new Query(); $provider = new ActiveDataProvider(['db' => $this->getConnection(), 'query' => $query->from('order')->orderBy('id')]); $pagination = $provider->getPagination(); $this->assertEquals(0, $pagination->getPageCount()); $this->assertCount(3, $provider->getModels()); $this->assertEquals(1, $pagination->getPageCount()); $provider->getPagination()->pageSize = 2; $this->assertEquals(3, count($provider->getModels())); $provider->refresh(); $this->assertEquals(2, count($provider->getModels())); }
/** * Вывод в файл * @param ActiveDataProvider $dataProvider * @param string $sFileName * @return boolean */ public function exportToFile($dataProvider, $sFileName) { $dataProvider->prepare(); $nMaxCount = $dataProvider->pagination->totalCount; $objPHPExcel = new PHPExcel(); $oSheet = $objPHPExcel->getSheet(0); $cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_sqlite3; $bCache = PHPExcel_Settings::setCacheStorageMethod($cacheMethod); $oDefaultStyle = $objPHPExcel->getDefaultStyle(); $oDefaultStyle->getFont()->setName('Arial'); $oDefaultStyle->getFont()->setSize(8); // $oSheet->getPageSetup() // ->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT) // ORIENTATION_LANDSCAPE // ->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4) // ->setFitToPage(true) // ->setFitToWidth(1) // ->setFitToHeight(0); // // $oSheet->getPageMargins() // ->setTop(0.5) // ->setRight(0.35) // ->setLeft(0.35) // ->setBottom(1); // $oSheet->getHeaderFooter() // ->setEvenFooter('&CСтраница &P [&N]') // ->setOddFooter('&CСтраница &P [&N]'); $nPageCount = $dataProvider->pagination->pageCount; if ($dataProvider->pagination->totalCount > $nMaxCount) { $nPageCount = floor($nMaxCount / $dataProvider->pagination->pageSize); } $cou = 0; $nRow = $this->nStartRow; foreach ($this->columnWidth as $k => $v) { if ($v !== null) { $oSheet->getColumnDimension($this->colIndexToName($k + 1))->setWidth($v); } } $sLastCol = $this->colIndexToName(max(count($this->columnWidth), count($this->columnTitles), count($this->columnValues))); $n = $nRow; $sTit = 'A' . $nRow++; $oSheet->setCellValue($sTit, 'Выгрузка от ' . date('d.m.Y H:i')); $objPHPExcel->getActiveSheet()->mergeCells($sTit . ':' . $sLastCol . $n); $sdataTitle = $this->dataTitle === null ? Yii::$app->name : $this->dataTitle; if ($sdataTitle != '') { $n = $nRow; $sTit = 'A' . $nRow++; $oSheet->setCellValue($sTit, $sdataTitle); $objPHPExcel->getActiveSheet()->mergeCells($sTit . ':' . $sLastCol . $n); } if (count($this->columnTitles) > 0) { $nRow++; $oSheet->fromArray($this->columnTitles, null, 'A' . $nRow++); } for ($page = 0; $page < $nPageCount; $page++) { $dataProvider->pagination->setPage($page); $dataProvider->refresh(); foreach ($dataProvider->getModels() as $model) { $aData = []; foreach ($this->columnValues as $v) { $aData[] = $v instanceof Closure ? call_user_func($v, $model, $cou) : $model->{$v}; } $oSheet->fromArray($aData, null, 'A' . $nRow); $cou++; $nRow++; } } $oSheet->getPageSetup()->setPrintArea('A' . $this->nStartRow . ':' . $sLastCol . ($nRow - 1)); $format = $this->getFileExt($sFileName, 'xls'); if ($format == 'xls') { $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); } else { $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); } $objWriter->save($sFileName); return true; }