Brave publisher PUBLISHER
Web developer Front-End in Oise, France - Gary Deshayes

Symfony, Excel, CSV | Generating a CSV file for Excel with Symfony

Web developer Front-End in Oise, France - Gary Deshayes <--Traduction

Created at : Thursday 6 August 2020

How to generate the CSV file with a Symfony Controller

To generate the file, it must be constructed as in the following example, by defining the columns of the CSV file with semicolons (;) and line breaks (\n).



namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class HomeController extends AbstractController
     * @Route("/home", name="home")
    public function home()
	//Column names in the first rows
    // the \n at the end allows you to do a super important line break in CSV
    // the semicolon separates the data into columns
	$myVariableCSV = "Lastname; Firstname; Age;\n";
	//Adding data (with the . in front to add the data to the existing variable)
	$myVariableCSV .= "John; Doe; 26;\n";
    //If you wish to add a space
    $myVariableCSV .= " ; ; ; \n";
    //Other data
	$myVariableCSV .= "Chuck; Norris; 80;\n";
	//We give the variable in string to the response, we set the HTTP code to 200
    return new Response(
		 //Defines the content of the query as an Excel file
             'Content-Type' => 'application/',
		 //We indicate that the file will be in attachment so opening the download box, as well as the definition of the name of the file
             "Content-disposition" => "attachment; filename=Tutoriel.csv"

CSV file recovery

When accessing the route /home as in the example, a file save window will allow you to retrieve the file.

You just have to open your CSV file with Excel or other software.

Preview of the file in Excel

Symfony, Excel, CSV | Generating a CSV file for Excel with Symfony

You can see that our columns are well defined, that we have the desired data in the right place with a line break in between.

You now know how to generate a CSV file with Symfony

Thank you for reading, feel free to share!

News publications :

Symfony 5 | Return an image via a controller route


Created at : Wednesday 1 September 2021

VueJS 3, ExpressJS 4.17 | Uploading and resizing an image

Javascript Framework JS

Created at : Saturday 28 November 2020

Symfony Event Subscriber | Restricting an ip range with an Event Subscriber


Created at : Saturday 14 November 2020

Angular, ExpressJS | JWT authentication with Angular 10 and ExpressJS 4 (MySQL)

Javascript SQL Framework JS

Created at : Sunday 20 September 2020

Symfony, Excel, CSV | Generating a CSV file for Excel with Symfony


Created at : Thursday 6 August 2020

Symfony FormType Choice | Set data default to group of radio button


Created at : Sunday 26 July 2020

Doctrine, Symfony | Difference between two dates in MySQL (Day, month, year...)

Symfony SQL

Created at : Thursday 18 June 2020

JavaScript, jQuery et Regex | Secure a password in real time with JS

Javascript jQuery

Created at : Monday 23 March 2020

Symfony, Doctrine | Retrieve old data of FormType


Created at : Friday 14 February 2020

jQuery, Webpack and Symfony | Call jQuery in your twig files

Symfony Bugs

Created at : Sunday 26 January 2020