How to create csv file using magento
Creating csv files in magento is very easy and flexiable
Example
$fileName = "example.csv";
$file_path = Mage::getBaseDir('media').DS.$fileName; // path of the file
$mage_csv = new Varien_Convert_Parser_Csv();
$rows = array();
$_data['n'] = 'name';
$_data['a'] = 'address';
$_data['g'] = 'gender';
$rows[]=$_data;
$data['n'] = 'Ram';
$data['a'] = 'Hyd';
$data['g'] = 'Male';
$rows []=$data;
try {
$mage_csv->saveData($file_path, $rows); //note $rows will be two dimensional array
$mage_csv->setData($rows);
$result = $mage_csv->unparse();
} catch (Exception $e) {
echo $e->getMessage();exit;
}
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$fileName");
header("Pragma: no-cache");
header("Expires: 0");
echo $result->getData();
exit;
$fileName = "example.csv";
$file_path = Mage::getBaseDir('media').DS.$fileName; // path of the file
$mage_csv = new Varien_Convert_Parser_Csv();
try {
$mage_csv->saveData($file_path, $rows); //note $rows will be two dimensional array
$mage_csv->setData($rows);
$result = $mage_csv->unparse();
} catch (Exception $e) {
echo $e->getMessage();exit;
}
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$fileName");
header("Pragma: no-cache");
header("Expires: 0");
echo $result->getData();
exit;
Example
$fileName = "example.csv";
$file_path = Mage::getBaseDir('media').DS.$fileName; // path of the file
$mage_csv = new Varien_Convert_Parser_Csv();
$rows = array();
$_data['n'] = 'name';
$_data['a'] = 'address';
$_data['g'] = 'gender';
$rows[]=$_data;
$data['n'] = 'Ram';
$data['a'] = 'Hyd';
$data['g'] = 'Male';
$rows []=$data;
try {
$mage_csv->saveData($file_path, $rows); //note $rows will be two dimensional array
$mage_csv->setData($rows);
$result = $mage_csv->unparse();
} catch (Exception $e) {
echo $e->getMessage();exit;
}
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$fileName");
header("Pragma: no-cache");
header("Expires: 0");
echo $result->getData();
exit;