2

If a csv file with id, name, price columns reside in magento root dir

How I can read this file and retrieve data using Varien_File_Csv() from custom model?

3 Answers 3

10

You can use it in this way to get data from csv file:

public function getCsvData($file){
    $csvObject = new Varien_File_Csv();
    try {
        return $csvObject->getData($file);
    } catch (Exception $e) {
        Mage::log('Csv: ' . $file . ' - getCsvData() error - '. $e->getMessage(), Zend_Log::ERR, 'exception.log', true);
        return false;
    }

}

The $file variable contains the path to the csv file.

The loop to read the columns if $data is the result of the function above could be:

 foreach ($data as $lines => $line) {
            $rows[] = array(
                'column 1' => $line[0],
                'column 2' => $line[1],
                'column 3' => $line[2],
            );
  }
3
  • Thanks! Kindly mention how to fetch data by columns in loop?
    – Sandesh
    Jul 11, 2014 at 8:31
  • A bit late but I added an example for a loop. Jul 14, 2014 at 10:58
  • This worked like a charm. Thanks. Dec 13, 2021 at 22:30
0

This is for excel sheet, you can similarly do for csv.

<?php 
error_reporting(E_ALL | E_STRICT);
$mageFilename = 'app/Mage.php';
require_once $mageFilename;
$app = Mage::app('default');

ini_set('display_errors', 1);

require_once 'reader.php';
$data = new Spreadsheet_Excel_Reader();
$data->setOutputEncoding('CP1251');
$path='Book5.xls';
$data->read($path);

$alldata = $data->sheets[0]['cells'];

  $count=0;
foreach($alldata as $data){
      if($count!=0){
$simpleProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$data['1']);
if($simpleProduct) {
$simpleProduct->setMetaDescription($data['2']);
$simpleProduct->setMetaKeyword($data['3']);
$simpleProduct->setMetaTitle($data['4']);

$simpleProduct->save();
echo "Updated product " . $data['1']. "<br>";
}

Reader.php file you can doqnload for any Spreadsheet_Excel_Writer for php.

2
  • The simpler solution would be to convert your .xls into a .csv, which is by far, less painful to read through using PHP. But thanks for the solution, I've always wondered how it was possible to read through an Excel file, without really looking for a solution. Jul 11, 2014 at 13:48
  • Thanks for new approach but I am look for Varien_File_Csv(). Even in plain PHP we can fetch csv as an array $csv_data= array();$filename='agbu/cstmp/ag-mrkt-stmp.csv'; $delimiter=',';if (($handle = fopen($filename, 'r')) !== FALSE){while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE){if(!$header) $header = $row;else $csv_data[] = array_combine($header, $row);}fclose($handle);}
    – Sandesh
    Jul 11, 2014 at 15:17
0

To read csv into array using column name as array keys, this worked for me - http://dltr.org/blog/magento/577/Magento-read-CSV-using-Varien_File_Csv-library

I found magento didn't have option to include first row as array keys, so ended up modifying like from above link myself and posted there on my magento blog.

1
  • Please dont just post links. Imagine if the URL fails to load someday and the information is lost.
    – paskl
    Jul 13, 2016 at 14:22

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.