CSV file is “Comma Separated Value” file in which you can store data in tabular format. CSV file is used to import and export data. You can import and export data on different system by CSV file.
In Prestashop we can export data in the form of CSV file by following way:
<?php class TestModuleFrontController extends ModuleFrontController { public function initContent() { $this->display_header = false; $this->display_footer = false; parent::initContent(); $customerList = array( array( 'Id Customer', 'Name' ), array( '1', 'John' ), array( '2', 'Bob' ) ); header('Content-Type: text/csv'); header('Content-Type: application/force-download; charset=UTF-8'); header('Cache-Control: no-store, no-cache'); header('Content-Disposition: attachment; filename="customer.csv"'); $file = fopen('php://output', 'w'); foreach ($customerList as $customer) { fputcsv($file, $customer); } fclose($file); } }
You must assign false in display_header and display_footer variable because if you do not assign false in above variable then Prestashop will add default header in you CSV file.
You can export data in CSV file from database, just assign data in $customerList variable which is fetched from database.
So in this way you can export data in CSV file.
Be the first to comment.