1

getExcelFile() does not work properly, please give me suggestion/idea how i export data in xml according to magento 2 way

this is my block file code

namespace Raveinfosys\Amazonorders\Block\Adminhtml\Allamazonorders\Allamazonorders;

class AmazonGrid extends \Magento\Backend\Block\Widget\Grid\Extended
{
  protected $_orderCollectionFactory;

  public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\Backend\Helper\Data $backendHelper,
    \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
    array $data = []
  ) {
    $this->_orderCollectionFactory = $orderCollectionFactory;
    parent::__construct($context, $backendHelper, $data);
  }

  protected function _construct()
    {
        parent::_construct();
        $this->setId('freightlogGrid');
        $this->setDefaultSort('freightlog_id');
        $this->setDefaultDir('ASC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
    }
    

  protected function _prepareCollection()
  {
    $collection = $this->_orderCollectionFactory->create()->join(
                array('soa' => 'sales_order_address'), 
                'soa.parent_id=main_table.entity_id and soa.address_type = "billing"', 
                array(
                    'billing_company'=> 'soa.company',
                    'billing_postcode'=> 'soa.postcode', 
                    'billing_state'=> 'soa.region', 
                    'billing_city'=> 'soa.city'
                ), 
                null,
                'left'
            )->join(
                array('soas' => 'sales_order_address'), 
                'soas.parent_id=main_table.entity_id and soas.address_type = "shipping"', 
                array(
                    'shipping_company'=> 'soas.company',
                    'shipping_zipcode'=> 'soas.postcode', 
                    'shipping_state'=> 'soas.region', 
                    'shipping_city'=> 'soas.city',
                    'shipping_street'=> 'soas.street',
                    'shipping_firstname'=> 'soas.firstname',
                    'shipping_lastname'=> 'soas.lastname'
                ), 
                null,
                'left'
            );
    $collection->addFieldToFilter('order_source',array('in' => array(\Raveinfosys\Salescustom\Helper\Data::ORDER_TYPE_AMZON,\Raveinfosys\Salescustom\Helper\Data::ORDER_TYPE_FULLFILL_BY_AMZON,\Raveinfosys\Salescustom\Helper\Data::ORDER_TYPE_FULLFILL_BY_SELLER)));
    $this->setCollection($collection);
    return parent::_prepareCollection();
  }
  protected function _prepareColumns()
  {
    $this->addColumn('created_at', array(
        'header' => __('Date'),
        'index' => 'created_at',
        'type' => 'date',
        'width' => '150px',
        'totals_label'      => __('Total')
        ));

    if($this->getRequest()->getActionName() == 'Orderxls' || $this->getRequest()->getActionName() == 'Ordercsv'  || $this->getRequest()->getActionName() == 'Orderpdf') {
            $this->addColumn('real_order_id',
            array(
                'header'    => __('Job Number'),
                'align'     =>'left',
                'index'     => 'increment_id',                  
                'filter'    => false
            ));
        }
        else{
            $this->addColumn('real_order_id',
            array(
                'header'    => __('Job Number'),
                'align'     =>'left',
                'index'     => 'increment_id',
                'width'   => '80px',
                'filter'    => false
            ));
        }
    $this->addColumn(
      'heared4us',
      [
        'header' => __('Reference Number'),
        'type' => 'text',
        'index' => 'heared4us',
        'filter'    => false,
        'header_css_class' => 'col-id',
        'column_css_class' => 'col-id',
      ]
    );
    $this->addColumn('billing_company', array(
            'header' => __('Customer Billing'),
            'index' => 'billing_company',
            'width' => '200px',
            'filter_index'=>'soa.company',
            'renderer'  => \Raveinfosys\Reports\Block\Adminhtml\Audit\Audit\Grid\Renderer\Billname::class,
            'filter'    => false
     ));
    $this->addColumn('shipping_company', array(
            'header' => __('Customer Shipping'),
            'index' => 'shipping_company',
            'width' => '200px',
            'renderer'  => \Raveinfosys\Amazonorders\Block\Adminhtml\Allamazonorders\Grid\Renderer\Shipname::class,
            'filter'    => false
        ));
    $this->addColumn('shipping_zipcode',
            array(
                  'header'    => __('Shipping Zip Code'),
                  'align'     =>'left',
                  'index'     => 'shipping_zipcode',
                  'filter'    => false
          ));
    $this->addColumn('county', array(
            'header' => __('County'),
            'align' => 'right',
            'width' => '150px',
            'renderer'  => \Raveinfosys\Reports\Block\Adminhtml\Audit\Audit\Grid\Renderer\Country::class,
            'filter'    => false,
            'sortable'  => false
        ));
    $this->addColumn('subtotal', array(
            'header' => __('Material Amount'),
            'type' => 'price',
            'index' => 'subtotal',
            'filter'    => false,
            'currency_code' => 'USD'
        ));
    $this->addColumn('tax_amount', array(
            'header' => __('Taxable Amount'),
            'type' => 'price',
            'index' => 'tax_amount',
            'filter'    => false,
            'currency_code' => 'USD'
        ));
    $this->addColumn('shipping_amount', array(
            'header' => __('Shipping'),
            'type' => 'price',
            'index' => 'shipping_amount',
            'filter'    => false,
            'currency_code' => 'USD'
        ));
    $this->addColumn('grand_total', array(
            'header' => __('Total Amount'),
            'type' => 'price',
            'index' => 'grand_total',
            'filter'    => false,
            'currency_code' => 'USD'
        ));
    $this->addColumn('total_refunded', array(
            'header' => __('Refunds'),
            'width' => '80px',
            'type' => 'price',
            'index' => 'total_refunded',
            'filter'    => false,
            'currency_code' => 'USD'
        ));
        $this->addExportType('*/*/Ordercsv', __('CSV'));
        $this->addExportType('*/*/Orderxls', __('Excel'));
        $this->addExportType('*/*/Orderpdf', __('PDF'));

    return parent::_prepareColumns();
  }
  public function getGridUrl()
  {
    return $this->getUrl('*/*/grid', ['_current' => true]);
  }
  
  public function getExcelData(){
        $this->_isExport = true;
        $this->_prepareGrid();
        $this->getCollection()->getSelect()->limit();
        $this->getCollection()->setPageSize(0);
        $this->getCollection()->load();
        $this->_afterLoadCollection();
        $headers = array();
        $data = array();
        foreach ($this->getColumns() as $column) {
            if (!$column->getIsSystem()) {
                $headers[] = $column->getHeader();
            }
        }
        
        $material = 0.00;
        $tax = 0.00;
        $freight = 0.00;
        $refund = 0.00;
        foreach ($this->getCollection() as $item) {
            $row = array();
            $material = $material + $item->getSubtotal();
            $tax = $tax + $item->getTaxAmount();
            $freight = $freight + $item->getShippingAmount();
            $refund = $refund + $item->getTotalRefunded();
            foreach ($this->getColumns() as $column) {
                if (!$column->getIsSystem()) {
                    $row[] = $column->getRowField($item);
                }
            }
            $data[] = $row;
        }
        $data[] = array('Total', '','','','','','','','$'.$material,'$'.$tax,'$'.$freight,'$'.$refund);

        if(count($row))
        {
          $fileName = "amazonorders.xls";
          $helper = $this->getHelper();
          $helper->initMemberData();
          $helper->addHeader($headers);
          foreach($data as $xl){
            $newRow = array();
            foreach ($xl as $key => $value) {
              array_push($newRow, $value);
            }
            $helper->addRow($newRow);
            unset($newRow);        
          }
          $helper->sendFile($fileName);
          unset($helper);
          
        }
    }
  protected function _prepareMassaction()
    {
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('entity_id');
        $this->getMassactionBlock()->addItem(
             'addressupdate',
                [
                'label' => __('Import address from PO'),
                'url' => $this->getUrl('amazonorders/allamazonorders/orderaddress'),
                'confirm'  => ('Are you sure?')
                ]
         );
         return $this;
    }
}

 - This my my controller file code.

resultPageFactory = $resultPageFactory; $this->fileFactory = $fileFactory; parent::__construct($context); } public function execute() { $fileName = 'Amazonorder.xls'; $content = $this->_view->getLayout()->createBlock( \Raveinfosys\Amazonorders\Block\Adminhtml\Allamazonorders\Allamazonorders\AmazonGrid::class )->getExcelFile(); // echo get_class($content); // die('ok'); return $this->fileFactory->create($fileName, $content, DirectoryList::VAR_DIR); } public function _isAllowed() { return $this->_authorization->isAllowed("Raveinfosys_Amazonorders::allamznorders"); } } ```

1 Answer 1

0

As far as I know, MS Excel (.xls, .xlsx) files use their own type of encode - Windows-1252. Simply it is not UTF-8, so I suppose PHP cannot process this type of files, at least without any help of external extension or library.

If you do not want to tackle this in a technical way, maybe we can deal with this the easy way on the user's end. XLS(X) files can easily be exported/converted into CSV files, which is encoded with UTF-8 and PHP-friendly. If you can have your data in CSV files instead, then PHP can definitely work with that, the easy part is on you!

Let me know if you still need further assistance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.