简体   繁体   English

在magento面板管理员中仅导出到Excel选定的行

[英]Export to Excel only selected rows in magento panel admin

I have a question. 我有个问题。 How to export only selected lines in grid to CSV or Excel file? 如何仅将网格中的选定行导出到CSV或Excel文件?

public function exportCsvAction()
{
    $fileName = 'Summary_employe_export.csv';
    $content = $this->getLayout()->createBlock('monogo_harvest/adminhtml_summaryEmployee_edit_dayHarvest')->getCsv();
    $this->_prepareDownloadResponse($fileName, $content);
}

public function exportExcelAction()
{
    $fileName = 'Summary_Employe_export.xls';
    $content = $this->getLayout()->createBlock('monogo_harvest/adminhtml_summaryEmployee_edit_dayHarvest')->getExcelFile();
    $this->_prepareDownloadResponse($fileName, $content);
}

//Grid
$this->addExportType('*/*/exportCsv',
        Mage::helper('monogo_workmanagement')->__('CSV'));
    $this->addExportType('*/*/exportExcel',
        Mage::helper('monogo_workmanagement')->__('Excel'));

在此处输入图片说明

Create a function getCsv() like bellow 创建一个像下面这样的函数getCsv()

public function getCsv()
{
    $shipmentIds = $this->getRequest()->getParam('internal_shipment_ids'); // Parameter will be like internal_'checkboxName'
    $csv = '';
    $this->_isExport = true;
    $this->_prepareGrid();
    if($shipmentIds)
    {
        $shipmentIds = explode(',' , $shipmentIds);
        $this->getCollection()->addAttributeToFilter('main_table.entity_id', array('in' => $shipmentIds))->getSelect()->limit();
        $this->getCollection()->setPageSize(0);
        $this->getCollection()->load();
        $this->_afterLoadCollection();
    }

    else
    {
        $this->getCollection()->getSelect()->limit();
        $this->getCollection()->setPageSize(0);
        $this->getCollection()->load();
        $this->_afterLoadCollection();
    }
    $data = array();
    foreach ($this->_columns as $column) {
        if (!$column->getIsSystem()) {
            $data[] = '"'.$column->getExportHeader().'"';
        }
    }
    $csv.= implode(',', $data)."\n";

    foreach ($this->getCollection() as $item) {
        $data = array();
        foreach ($this->_columns as $column) {
            if (!$column->getIsSystem()) {
                $data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'),
                        $column->getRowFieldExport($item)) . '"';
            }
        }
        $csv.= implode(',', $data)."\n";
    }

    if ($this->getCountTotals())
    {
        $data = array();
        foreach ($this->_columns as $column) {
            if (!$column->getIsSystem()) {
                $data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'),
                        $column->getRowFieldExport($this->getTotals())) . '"';
            }
        }
        $csv.= implode(',', $data)."\n";
    }

    return $csv;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM