How to export to csv with charset UTF-8

If you export to csv file and see Â£ instead of £. This problem happens because the csv file is not default charset UTF-8. It will show correct content if you open file with charset UTF-8. You also can use notepad to change it to charset UTF-8 and save it.

In magento, we often write th resource to export like:

/**
     * Export sales report grid to CSV format
     *
     * @return ResponseInterface
     */
    public function execute()
    {
        $fileName = 'sale_staff_daily.csv';
        $grid = $this->_view->getLayout()->createBlock('Magestore\Webpos\Block\Adminhtml\Report\Salestaffdaily\Grid');
        $this->_initReportAction($grid);
        return $this->_fileFactory->create($fileName, $grid->getCsvFile(), DirectoryList::VAR_DIR);
    }

Now you can solve the problem “£ instead of £” by using  command line: echo “\xEF\xBB\xBF”

/**
    * Export sales report grid to CSV format
    *
    * @return ResponseInterface
    */
   public function execute()
   {
       echo "\xEF\xBB\xBF";
       $fileName = 'sale_staff_daily.csv';
       $grid = $this->_view->getLayout()->createBlock('Magestore\Webpos\Block\Adminhtml\Report\Salestaffdaily\Grid');
       $this->_initReportAction($grid);
       return $this->_fileFactory->create($fileName, $grid->getCsvFile(), DirectoryList::VAR_DIR);
   }

 

Add Comment

Required fields are marked *. Your email address will not be published.