Q: PHP : Write input text from HTML Form to specific Cell in Excel I'm trying to store result data from html form to specific cell in Excel, but for now i am only able to write data without formatting, where the value should be filled in. 
Here is the code :
    <body>
      <form action="excel.php" method="post">
        <table>
          <tr><td>Name</td><td><input type="text" name="name" /></td></tr>
          <tr><td>Email</td><td><input type="text" name="email" /></td></tr>
          <tr><td>Location</td><td><input type="text" name="location" /></td></tr>
          <tr><td> </td><td><input type="submit" /></td></tr>
        </table>
      </form>
    </body>
    </html>

Just basic html form, and here is the PHP code for exporting form result in csv format.
<?php
    $filename = "myexcel.csv";
    header( 'Content-Type: text/csv' );
    header( 'Content-Disposition: attachment;filename='.$filename);
    $file = fopen('php://output', 'w');            
    $cells[] = array('Name', 'Email', 'Location');
    $cells[] = array($_POST['name'], $_POST['email'], $_POST['location']);
    foreach($cells as $cell){
      fputcsv($file,$cell);
    }
    fclose($file); 
?>

From code above this is the result in excel :
Image 1
And this is the result that i expected :
Image 2
If there is a better and proper way to do this, any help is appreciated.

A: $filename = "myexcel.csv";
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename='.$filename);
$file = fopen('php://output', 'w');            

 $cells[] = array('Name', $_POST['name']);
 $cells[] = array('Email', $_POST['email']);
 $cells[] = array('Location',$_POST['location']);

foreach($cells as $cell){
  fputcsv($file,$cell);
}
fclose($file); 


A: CSV, as the name suggests, is just a text file that contains Comma Separated Values, but since it's plain text, it can't be formatted.
Microsoft Excel opens the CSV format for "kindness", but it isn't an excel format.
If you objective is to have a nice formatted spreadsheet, then I suggest you to use a 3rd party library like PHPSpreadsheet, that you can install via composer with composer require phpoffice/phpspreadsheet.
Of course you'll need to switch to Object Oriented Programming (in PHP it's supported since version 5.1.0).
Hope that this helped.
