kinldy让我知道添加CSV导出按钮并在magento2中为自定义网格创建CSV。我创建了一个网格和表单。需要在magento2中添加csv导出功能。
答案 0 :(得分:3)
创建你的控制器
<?php
namespace Yourpackage\Yourmodule\Controller\Adminhtml\Sample;
class ExportCsv extends \Magento\Backend\App\Action
{
protected $_fileFactory;
protected $_response;
protected $_view;
protected $directory;
protected $converter;
protected $resultPageFactory ;
protected $directory_list;
public function __construct( \Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
public function execute()
{
$fileName = 'yourfilename.csv';
$resultPage = $this->resultPageFactory ->create();
$content = $resultPage->getLayout()->getBlock('yourblockname')->getCsv();;
$this->_sendUploadResponse($fileName, $content);
}
protected function _sendUploadResponse($fileName, $content, $contentType='application/octet-stream') {
$this->_response->setHttpResponseCode(200)
->setHeader('Pragma', 'public', true)
->setHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0', true)
->setHeader('Content-type', $contentType, true)
->setHeader('Content-Length', strlen($content), true)
->setHeader('Content-Disposition', 'attachment; filename="' . $fileName . '"', true)
->setHeader('Last-Modified', date('r'), true)
->setBody($content)
->sendResponse();
die;
}
}
创建你的布局xml yourmodule_yourcontroller_exportcsv
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<update handle="yourmodule_yourcontroller_grid"/>
</page>
答案 1 :(得分:1)
使用magento2中的UI组件在网格中导出
在容器标签下,在ui列表组件xml中添加以下行
<exportButton name="export_button">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="selectProvider" xsi:type="string">vendor_listing.vendor_listing.example_blog_columns.ids</item>
</item>
</argument>
</exportButton>
这将为magento2中的网格中的已加载集合生成csv
答案 2 :(得分:1)
创建你的控制器
<?php
namespace Yourpackage\Yourmodule\Controller\Adminhtml\Report;
use Magento\Framework\App\Filesystem\DirectoryList;
class ExportCsv extends \Magento\Backend\App\Action {
protected $resultPageFactory;
protected $fileFactory;
/**
*
* @param \Magento\Backend\App\Action\Context $context
* @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
*/
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\App\Response\Http\FileFactory $fileFactory,
\Magento\Framework\View\Result\PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
$this->fileFactory = $fileFactory;
parent::__construct($context);
}
/**
*
* @return type
*/
public function execute() {
$fileName = 'salesreport.csv';
$content = $this->_view->getLayout()->createBlock(
\Ktpl\RepresentativeReport\Block\Adminhtml\Salesdata\Grid::class
)->getCsvFile();
return $this->fileFactory->create($fileName, $content, DirectoryList::VAR_DIR);
}
}
答案 3 :(得分:1)
由于我找不到完整的答案。这是您应继续进行的操作,以便可以从管理网格中过滤数据。
请从execute方法中删除不需要的行。当我提出这个问题时,我正在研究这段代码
首先创建您的UI组件
<listingToolbar name="listing_top">
<exportButton class="Magento\Ui\Component\ExportButton" component="Magento_Ui/js/grid/export" displayArea="dataGridActions">
<settings>
<options>
<option name="cvs" xsi:type="array">
<item name="value" xsi:type="string">csv</item>
<item name="label" xsi:type="string" translate="true">CSV</item>
<item name="url" xsi:type="string">controller/results/export</item>
</option>
<option name="xml" xsi:type="array">
<item name="value" xsi:type="string">xml</item>
<item name="label" xsi:type="string" translate="true">Excel XML</item>
<item name="url" xsi:type="string">skininc/results/export</item>
</option>
</options>
</settings>
</exportButton>
</listingToolbar>
下一步创建用于skininc / results / export的控制器
<?php
/**
* @category Magento 2 Module
* @package Overdosedigital\Frontendflow
* @author Don Nuwinda
*/
namespace MyModule\Frontendflow\Controller\Adminhtml\Results;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Ui\Component\MassAction\Filter;
use Magento\Ui\Model\Export\ConvertToCsv;
use Magento\Framework\App\Response\Http\FileFactory;
use MyModule\Frontendflow\Model\ResourceModel\MyData\CollectionFactory;
class Export extends \Magento\Backend\App\Action
{
/**
* @var \Magento\Backend\Model\View\Result\ForwardFactory
*/
protected $resultForwardFactory;
/**
* Massactions filter
*
* @var Filter
*/
protected $filter;
/**
* @var MetadataProvider
*/
protected $metadataProvider;
/**
* @var WriteInterface
*/
protected $directory;
/**
* @var ConvertToCsv
*/
protected $converter;
/**
* @var FileFactory
*/
protected $fileFactory;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
Filter $filter,
Filesystem $filesystem,
ConvertToCsv $converter,
FileFactory $fileFactory,
\Magento\Ui\Model\Export\MetadataProvider $metadataProvider,
\MyModule\Frontendflow\Model\ResourceModel\MyData $resource,
CollectionFactory $collectionFactory
) {
$this->resources = $resource;
$this->filter = $filter;
$this->_connection = $this->resources->getConnection();
$this->directory = $filesystem->getDirectoryWrite(DirectoryList::VAR_DIR);
$this->metadataProvider = $metadataProvider;
$this->converter = $converter;
$this->fileFactory = $fileFactory;
parent::__construct($context);
$this->resultForwardFactory = $resultForwardFactory;
$this->collectionFactory = $collectionFactory;
}
/**
* export.
*
* @return \Magento\Backend\Model\View\Result\Forward
*/
public function execute()
{
$collection = $this->filter->getCollection($this->collectionFactory->create());
$component = $this->filter->getComponent();
$this->filter->prepareComponent($component);
$dataProvider = $component->getContext()->getDataProvider();
$dataProvider->setLimit(0, false);
$ids = [];
foreach ($collection as $document) {
$ids[] = (int)$document->getId();
}
$searchResult = $component->getContext()->getDataProvider()->getSearchResult();
$fields = $this->metadataProvider->getFields($component);
$options = $this->metadataProvider->getOptions();
$name = md5(microtime());
$file = 'export/'. $component->getName() . $name . '.csv';
$this->directory->create('export');
$stream = $this->directory->openFile($file, 'w+');
$stream->lock();
$stream->writeCsv($this->metadataProvider->getHeaders($component));
foreach ($searchResult->getItems() as $document) {
if( in_array( $document->getId(), $ids ) ) {
$this->metadataProvider->convertDate($document, $component->getName());
$stream->writeCsv($this->metadataProvider->getRowData($document, $fields, $options));
}
}
$stream->unlock();
$stream->close();
return $this->fileFactory->create('export.csv', [
'type' => 'filename',
'value' => $file,
'rm' => true // can delete file after use
], 'var');
}
}
答案 4 :(得分:1)
以下是默认类,它仅导出显示在管理网格中的数据,而不导出整个数据
在网格列中查找,
并仅将此代码添加到ui contactus_grid.xml或其他任何文件中。
<exportButton class="Magento\Ui\Component\ExportButton" component="Magento_Ui/js/grid/export" displayArea="dataGridActions">
<settings>
<options>
<option name="csv" xsi:type="array">
<item name="value" xsi:type="string">csv</item>
<item name="label" xsi:type="string" translate="true">CSV</item>
<item name="url" xsi:type="string">mui/export/gridToCsv</item>
</option>
<option name="xml" xsi:type="array">
<item name="value" xsi:type="string">xml</item>
<item name="label" xsi:type="string" translate="true">Excel XML</item>
<item name="url" xsi:type="string">mui/export/gridToXml</item>
</option>
</options>
</settings>
</exportButton>
答案 5 :(得分:0)
将CSV导出按钮添加到网格块布局(XML):
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="your.grid.container">
<block class="Yourpackage\Yourmodule\Block\Adminhtml\Sample\Grid" name="sample.grid" as="grid">
<!-- Arguments or blocks -->
<!-- Export Widget -->
<block class="Magento\Backend\Block\Widget\Grid\Export" name="sample.grid.export" as="grid.export">
<arguments>
<argument name="exportTypes" xsi:type="array">
<item name="csv" xsi:type="array">
<item name="urlPath" xsi:type="string">*/*/exportCsv</item>
<item name="label" xsi:type="string" translate="true">CSV</item>
</item>
</argument>
</arguments>
</block>
<!-- Columns block -->
</block>
</referenceBlock>
</body>
</page>
创建您的控制器:
<?php
namespace Yourpackage\Yourmodule\Controller\Adminhtml\Sample;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\App\Filesystem\DirectoryList;
class ExportCsv extends \Yourpackage\Yourmodule\Controller\Adminhtml\Sample
{
/**
* Export data grid to CSV format
*
* @return ResponseInterface
*/
public function execute()
{
$this->_view->loadLayout();
$fileName = 'sample_data.csv';
$content = $this->_view->getLayout()->getChildBlock('sample.grid', 'grid.export');
return $this->_fileFactory->create(
$fileName,
$content->getCsvFile($fileName),
DirectoryList::VAR_DIR
);
}
}
答案 6 :(得分:0)
只需在listingToolbar开头标记下添加此行,对我来说就成功了
<listingToolbar name="listing_top">
<exportButton name="export_button"/>
答案 7 :(得分:0)
下面是默认类,它仅导出显示在管理网格中的数据,而不导出整个数据
>>> from collections import Counter
>>> d = Counter({'d': 4, 'b': 3, 'c': 1, 'e': 3, 'a': 2, 'g': 1})
>>> [k for k, v in d.items() if v == 1]
['c', 'g']
如果要获取所有数据,请创建一个自定义导出按钮,以导出所有数据。
在显示管理网格的Ui组件文件中进行编辑
[供应商名称] \ [模块名称] \ view \ adminhtml \ ui_component
添加此代码以将Button添加到管理网格
Ui-ComponentFIleName.xml
<exportButton class="Magento\Ui\Component\ExportButton">
现在创建一个导出或导入数据的文件。文件名必须与您在Ui组件文件中定义的名称相同
<item name="buttons" xsi:type="array">
<item name="import" xsi:type="array">
<item name="name" xsi:type="string">import</item>
<item name="label" xsi:type="string" translate="true">Import</item>
<item name="class" xsi:type="string">secondary</item>
<item name="url" xsi:type="string">*/*/importdata</item>
<item name="sortOrder" xsi:type="number">20</item>
</item>
<item name="export" xsi:type="array">
<item name="name" xsi:type="string">export</item>
<item name="label" xsi:type="string" translate="true">Export</item>
<item name="class" xsi:type="string">secondary</item>
<item name="url" xsi:type="string">*/*/exportdata</item>
<item name="sortOrder" xsi:type="number">30</item>
</item>
</item>
文件路径必须是这样
[供应商名称] \ [模块名称] \控制器\ Adminhtml \ [控制器名称] \ Exportdata.php
Exportdata.php
<item name="url" xsi:type="string">*/*/exportdata</item>
<item name="url" xsi:type="string">*/*/importdata</item>
现在,您可以单击“导出”按钮,并查看下面下载的.csv文件。
希望对您有所帮助。