/** * Retrieve grid data as MS Excel 2003 XML Document * * @return string */ public function getExcel() { $collection = $this->_getPreparedCollection(); $headers = array(); $data = array(); foreach ($this->_getColumns() as $column) { if (!$column->getIsSystem()) { $headers[] = $column->getHeader(); } } $data[] = $headers; foreach ($collection as $item) { $row = array(); foreach ($this->_getColumns() as $column) { if (!$column->getIsSystem()) { $row[] = $column->getRowField($item); } } $data[] = $row; } if ($this->getCountTotals()) { $row = array(); foreach ($this->_getColumns() as $column) { if (!$column->getIsSystem()) { $row[] = $column->getRowField($this->_getTotals()); } } $data[] = $row; } $convert = new \Magento\Framework\Convert\Excel(new \ArrayIterator($data)); return $convert->convert('single_sheet'); }
/** * Retrieve grid data as MS Excel 2003 XML Document * * @return string */ public function getExcel() { $this->_isExport = true; $this->_prepareGrid(); $this->getCollection()->getSelect()->limit(); $this->getCollection()->setPageSize(0); $this->getCollection()->load(); $this->_afterLoadCollection(); $headers = []; $data = []; foreach ($this->getColumns() as $column) { if (!$column->getIsSystem()) { $headers[] = $column->getHeader(); } } $data[] = $headers; foreach ($this->getCollection() as $item) { $row = []; foreach ($this->getColumns() as $column) { if (!$column->getIsSystem()) { $row[] = $column->getRowField($item); } } $data[] = $row; } if ($this->getCountTotals()) { $row = []; foreach ($this->getColumns() as $column) { if (!$column->getIsSystem()) { $row[] = $column->getRowField($this->getTotals()); } } $data[] = $row; } $convert = new \Magento\Framework\Convert\Excel(new \ArrayIterator($data)); return $convert->convert('single_sheet'); }
/** * Test \Magento\Framework\Convert\Excel->convert() * \Magento\Framework\Convert\Excel($iterator, $callbackMethod) * * @return void */ public function testConvertCallback() { $convert = new \Magento\Framework\Convert\Excel(new \ArrayIterator($this->_testData), [$this, 'callbackMethod']); $this->assertContains('_TRUE_', $convert->convert(), 'Failed asserting that callback method is called.'); }