3

When I run the Order export to CSV from the Order Grid I'm getting a random character "Â" before the "£" symbol.

I can't find the file that outputs the code for the CSV export, I'd imagine I just need to edit the encoding or something simple but I'm unsure what file to edit. The code from the Order Grid is:

$this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV'));

Where do I find */*/exportCsv?

  • try to change the exported file encoding to UTF8 – Marius Nov 7 '14 at 10:17
  • Could you tell me what file I'd have to edit to do that? – Christopher Thrower Nov 7 '14 at 10:24
  • Just open the exported file with notepad++ and change the encoding from the Encoding menu. – Marius Nov 7 '14 at 12:14
  • But this wouldn't fix the problem on all future exports. Unfortunately the client wouldn't want to edit the encoding each time they run their Order Export. – Christopher Thrower Nov 7 '14 at 12:18
3

To answer your question:

app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php

There are ways to 'fix' your problem:

Instruct whomever it is that opens these files to do it 'properly'. That means using their Excel style program as follows: 1) Start the Excel-style program, create a new 'sheet' on an existing spreadsheet or a new one, 2) import the csv file into a sheet (rather than clicking on it directly), 3) specify that the file to be imported is UTF-8 in the dialogue box presented.

There are advantages to this work-flow in that they can have a master document for the month and then import each sheet. At the end of the year that means 12 spreadsheets rather than 365.25 of them.

If that is going to be waaaaaay to difficult for them and they really are capable of only clicking on files with 'import file' being just beyond their abilities then you can do things on the Magento side.

Open up the CSV file and see if it has a utf-8 BOM header. This is three bytes at the start that identify the file as utf-8. http://en.wikipedia.org/wiki/Byte_order_mark

You will need to make sure that you open the file in some program that doesn't hide things - if you use linux 'octal dump' then you should have no problems. 'od -c {file} | more' will show you what there is at the start quite reliably.

You can then add the utf-8 BOM by copying app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php to app/code/local/Mage/Adminhtml/controllers/Sales/OrderController.php and then changing the export function to be something like this:

/**
 * Export order grid to CSV format
 */
public function exportCsvAction()
{
    $fileName   = 'orders.csv';
    $grid       = $this->getLayout()->createBlock('adminhtml/sales_order_grid');
    $utf8        = chr(239) . chr(187) . chr(191) . $grid->getCsvFile();
    $this->_prepareDownloadResponse($fileName, $utf8);
}

If that actually works then the job is done. I don't think you can just override the above in your own module that easily.

| improve this answer | |
  • The code added to OrderController gives me the following error: Notice: Array to string conversion in /home/independ/public_html/test/app/code/local/Mage/Adminhtml/controllers/Sales/OrderController.php on line 703 - Line 703 is: $utf8 = chr(239) . chr(187) . chr(191) . $grid->getCsvFile(); – Christopher Thrower Nov 7 '14 at 14:11
  • I've given the client instructions to "Import" instead of double-clicking the CSV file – Christopher Thrower Nov 7 '14 at 14:20
  • Sorry about that - can you try creating the $utf8 as an array, e.g. $bom = array(chr(239) . chr(187) . chr(191)); $utf8 = $bom + $grid->getCsvFile(); The general problem - no UTF8 header is the key to what is happening, fixing it is 'implementation'... – Henry's Cat Nov 7 '14 at 14:32
  • above code is not work for me, is any other solution for that – krupa parikh Jan 24 '18 at 12:11
0

Henry's Cat already pointed to the correct direction. However $grid->getCsvFile() does not need to be the raw file content but may be:

/**
* Retrieve a file container array by grid data as CSV
*
* Return array with keys type and value
*
* @return array
*/

like

    return array(
        'type'  => 'filename',
        'value' => $file,
        'rm'    => true // can delete file after use
    );

so in this case you have to manipulate the getCsvFile() implementation.

You can use:

$io->streamLock(true);
// BOM fo UTF8 -> Begin Fix
$io->streamWrite(chr(239) . chr(187) . chr(191));
// End Fix
$io->streamWriteCsv($this->_getExportHeaders(),";");

I changed it in my inherited classes of Mage_Adminhtml_Block_Sales_Creditmemo_Grid and Mage_Adminhtml_Block_Sales_Invoice_Grid

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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