在Grid的_prepareColumns中加上下面的兩句代碼在Grid頁面會出現導出文件選項:
$this->addExportType('*/*/exportCsv', $this->__('CSV'));
$this->addExportType('*/*/exportXml', $this->__('XML'));
點進去這個方法可以看到如下:
public function addExportType($url, $label)
{
$this->_exportTypes[] = new Varien_Object(
array(
'url' => $this->getUrl($url, array('_current'=>true)),
'label' => $label
)
);
return $this;
}
這就能看出來第一個參數是URL是點擊export按鈕的時候請求的URL,另一個這是下拉框中展示的Label
在該模塊的控制器中分別加入對應的導出控制器,如下:
public function exportCsvAction()
{
$fileName = 'test.csv';
$content = $this->getLayout()->createBlock('test/adminhtml_test_grid')->getCsvFile();
$this->_prepareDownloadResponse($fileName, $content);
}
public function exportXmlAction()
{
$fileName = 'test.xml';
$content = $this->getLayout()->createBlock('test/adminhtml_test_grid')->getExcelFile();
$this->_prepareDownloadResponse($fileName, $content);
}
這麼寫默認是將Grid中的字段全部導出,
$this->getLayout()->createBlock(‘test/adminhtml_test_grid’)->getExcelFile();這句話的意思是在test/adminhtml_test_grid的block下調用getExcelFile方法,該方法在Grid的父類中,該方法是將Grid中的數據先暫存到指定文件中,然後將文件相關數據返回,如下:
return array(
'type' => 'filename',
'value' => $file,
'rm' => true // can delete file after use
);
$file是文件的存放路徑, ‘rm’ => true是文件使用完之後可以刪除掉
這裏面有幾個方法解釋一下:
如果想去掉導出文件中的標題,則去掉如下代碼:
$io->streamWriteCsv($this->_getExportHeaders());
$io->streamWrite($parser->getRowXml($this->_getExportHeaders()));
下面這個是整理Grid中的數據:
Mage::helper("core")->getEscapedCSVData($this->_getExportTotals())
$io->streamWrite($parser->getRowXml($this->_getExportTotals()));
上面寫的是導出整個Grid中包括的字段的數據,下面我講一下導出指定數據以及新加自己想要的數據:
在Block/Widget/Grid,php中有下面一段代碼:
protected $_isExport = false;
這句代碼可以控制字段的展示以及隱藏以及字段的導出,如下是在Grid中該字段是隱藏的,但是在導出數據的時候該字段是包含在導出中的,
if ($this->_isExport) {
$this->addColumn('entity_id', array(
'header' => 'ID',
'index' => 'entity_id'
));
}
具體原理是在展示Grid時判斷改值是false所以隱藏,但是在導出時,調用的相關導出代碼中包含一段代碼,這裏拿CSV舉例:
public function getCsvFile()
{
$this->_isExport = true;
$this->_prepareGrid();
$io = new Varien_Io_File();
通過這段代碼就可以看出在導出的時候$this->_isExport的值是true了,所以該字段是導出的;
那麼我想加字段在導出的數據中加入我想要,那麼久可以重寫下面的方法:
public function setCollection($collection)
{
$this->_collection = $collection;
}
如下是我重寫的:
public function setCollection($collection)
{
//導出字段
if($this->_isExport){
$collection->getSelect()->joinLeft(array(
'addr'=>$collection->getTable('sales/order_address')),
'addr.parent_id=`main_table`.entity_id AND addr.address_type="shipping"',
array('region','city')
);
}
parent::setCollection($collection);
}
通過該聯表增加了字段,這樣在導出的時候就會增加該字段,這裏面也用了$this->_isExport,也是爲了在展示Grid的時候不走這個聯表代碼;
下面的是導出指定代碼的方法:
public function exportCsvAction()
{
$fileName = 'orders.csv';
$grid = $this->getLayout()->createBlock('adminhtml/sales_order_grid');
$csvDataArr = $grid->getArray();
$data = "";
$data .="order_id,mobile"."\n";
foreach ($csvDataArr['items'] as $item) {
$incrementId = $item['increment_id'];
$mobile = $item['mobile'];
$data .='"'.$incrementId.'","'.$mobile.'"'."\n";
}
echo "\xEF\xBB\xBF";
$this->_prepareDownloadResponse($fileName, $data);
}
這裏我使用了$grid->getArray(),這個方法是將暫存的文件中的數據組裝成數組,然後篩選自己想要的數據導出,這裏面根據CSV的數據格式使用如下樣式:
$data .="order_id,mobile"."\n";
$data .='"'.$incrementId.'","'.$mobile.'"'."\n";
第一句是導出文件的標題,下面是數據,用\n換行
下面是XML的格式組裝:
public function exportExcelAction()
{
$fileName = 'orders.xml';
$grid = $this->getLayout()->createBlock('adminhtml/sales_order_grid');
$csvDataArr = $grid->getArray();
$data = "";
$header = "<?xml version=\"1.0\"?><?mso-application progid=\"Excel.Sheet\"?><Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:x2=\"http://schemas.microsoft.com/office/excel/2003/xml\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns:c=\"urn:schemas-microsoft-com:office:component:spreadsheet\"><OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\"></OfficeDocumentSettings><ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\"></ExcelWorkbook>";
$title = "</ExcelWorkbook><Worksheet ss:Name=\"Sheet 1\"><Table><Row><Cell><Data ss:Type=\"String\">order_id</Data></Cell><Cell><Data ss:Type=\"String\">mobile</Data></Cell></Row>";
$data = $header . $title;
foreach ($csvDataArr['items'] as $item) {
$incrementId = $item['increment_id'];
$mobile = $item['mobile'];
$data .="<Row><Cell><Data ss:Type=\"Number\">$incrementId</Data></Cell><Cell><Data ss:Type=\"Number\">$mobile</Data></Cell></Row>";
}
$this->_prepareDownloadResponse($fileName, $data);
}
這裏面根據XML的數據格式使用如下樣式:
$header = "<?xml version=\"1.0\"?><?mso-application progid=\"Excel.Sheet\"?><Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:x2=\"http://schemas.microsoft.com/office/excel/2003/xml\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:html=\"http://www.w3.org/TR/REC-html40\" xmlns:c=\"urn:schemas-microsoft-com:office:component:spreadsheet\"><OfficeDocumentSettings xmlns=\"urn:schemas-microsoft-com:office:office\"></OfficeDocumentSettings><ExcelWorkbook xmlns=\"urn:schemas-microsoft-com:office:excel\"></ExcelWorkbook>";
$title = "</ExcelWorkbook><Worksheet ss:Name=\"Sheet 1\"><Table><Row><Cell><Data ss:Type=\"String\">order_id</Data></Cell><Cell><Data ss:Type=\"String\">mobile</Data></Cell></Row>";
$data = $header . $title;
$data .="<Row><Cell><Data ss:Type=\"Number\">$incrementId</Data></Cell><Cell><Data ss:Type=\"Number\">$mobile</Data></Cell></Row>";
這裏面的$header是xml的一些定義信息
$title是文件的標題
最後一句是數據內容