Write in a Excel file with PHPExcel
<p>Well, I would like to write in an Excel file using a PHP script.
Actually, when an user submit the form, we get the mail and the current date in order to put it in the Excel.</p>

<p>The mail must be in one column, and the date too.</p>

<p>So this is my form (a classic form):</p>

<pre><code>&lt;form action="mail.php" method="POST"&gt;
    &lt;label for="name"&gt;Nom*&lt;br /&gt;
        &lt;input id="name" type="text" name="Nom" /&gt;
    &lt;/label&gt;&lt;br /&gt;&lt;br /&gt;

    &lt;label for="mail"&gt;Mail*&lt;br /&gt;
        &lt;input id="mail" type="email" name="Email" /&gt;
    &lt;/label&gt;&lt;br /&gt;&lt;br /&gt;

    &lt;label for="tel"&gt;Téléphone*&lt;br /&gt;
        &lt;input id="tel" type="text" name="Téléphone" /&gt;
    &lt;/label&gt;&lt;br /&gt;&lt;br /&gt;

    &lt;label for="comment"&gt;Commentaire*&lt;br /&gt;
        &lt;textarea id="comment" type="text" name="Commentaires"&gt;    &lt;/textarea&gt;
    &lt;/label&gt;&lt;br /&gt;&lt;br /&gt;

    &lt;input type="submit" value="Envoyer" /&gt;
&lt;/form&gt;
</code></pre>

<p>And my mail.php, so this is here that I use PHPExcel classes :</p>

<pre><code>&lt;?php
/** Error reporting */
error_reporting(E_ALL);

/** PHPExcel */
include './Classes/PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
include 'PHPExcel/Writer/Excel2007.php';
include './Classes/PHPExcel/IOFactory.php';

/* Set the variables */
$mail = $_POST['Email'];

// Line number
$ligne = 1;

// Excel object
$fichier = new PHPExcel();
$fichier-&gt;getProperties()-&gt;setCreator("Don't know");
//Set the size
$fichier-&gt;getActiveSheet()-&gt;getColumnDimension('A')-&gt;setWidth(30);
$fichier-&gt;getActiveSheet()-&gt;getColumnDimension('B')-&gt;setWidth(30);

$fichier-&gt;setActiveSheetIndex(0);
$feuille = $fichier-&gt;getActiveSheet();

function writeExcel($objExcel, $sheet, $email){
    $sheet-&gt;SetCellValue('A'.$ligne, $email);
    $sheet-&gt;SetCellValue('B'.$ligne, date("Y-m-d H:i"));
    $objWriter = new PHPExcel_Writer_Excel2007($objExcel);
    $objWriter-&gt;save(str_replace('.php', '.xlsx', __FILE__));
    $ligne++;
}

writeExcel($fichier, $feuille, $mail);
?&gt;
</code></pre>

<p>I think it's the line number is a problem, I don't know how to organize my code.
Each time a user submit the form, we put his mail (cell A1) and the current date (cell B1), and the next mail must be at A2 cell, etc ...</p>

<p>I don't know if you know what I mean ^^</p>

<p><strong>EDIT</strong>
With the comments I modified my code ! But it's the same ^^</p>

<pre><code>&lt;?php
/** Error reporting */
error_reporting(E_ALL);

/** PHPExcel */
include './Classes/PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
include 'PHPExcel/Writer/Excel2007.php';
include './Classes/PHPExcel/IOFactory.php';

/* Set the variable */
$mail = $_POST['Email'];

// Load file if it doesn't exists
if (file_exists("mail.xlsx"))
{
    $fichier = PHPExcel_IOFactory::load("mail.xlsx");
}
else
{
    $fichier = new PHPExcel();
}
// Line number
$ligne = $fichier-&gt;getActiveSheet()-&gt;getHighestRow() + 1;

// Excel object
$fichier-&gt;getProperties()-&gt;setCreator("Don't know");

//Set the size
$fichier-&gt;getActiveSheet()-&gt;getColumnDimension('A')-&gt;setWidth(30);
$fichier-&gt;getActiveSheet()-&gt;getColumnDimension('B')-&gt;setWidth(30);

$fichier-&gt;setActiveSheetIndex(0);
$feuille = $fichier-&gt;getActiveSheet();

// Line number
global $ligne = $fichier-&gt;getActiveSheet()-&gt;getHighestRow() + 1;

function writeExcel($objExcel, $sheet, $email){
    $sheet-&gt;SetCellValue('A'.global $ligne, $email);
    $sheet-&gt;SetCellValue('B'.global $ligne, date("Y-m-d H:i"));
    $objWriter = new PHPExcel_Writer_Excel2007($objExcel);
    $objWriter-&gt;save(str_replace('.php', '.xlsx', __FILE__));
    global $ligne++;
}

writeExcel($fichier, $feuille, $mail);
?&gt;
</code></pre>

<p><strong>LAST EDIT (I HOPE)</strong>
Ok it's working :D</p>

<p>This is my code :</p>

<pre><code>&lt;?php
/** Error reporting */
error_reporting(E_ALL);

/** PHPExcel */
include './Classes/PHPExcel.php';

/** PHPExcel_Writer_Excel2007 */
include 'PHPExcel/Writer/Excel2007.php';
include './Classes/PHPExcel/IOFactory.php';

/* Set the variable */
$mail = $_POST['Email'];

// Load file if it doesn't exists
if (file_exists('mail.xlsx'))
{
    $objExcel = PHPExcel_IOFactory::load('mail.xlsx');
}
else
{
    $objExcel = new PHPExcel();
}

$objExcel-&gt;getProperties()-&gt;setCreator("Don't know");

//Set the size
$objExcel-&gt;getActiveSheet()-&gt;getColumnDimension('A')-&gt;setWidth(30);
$objExcel-&gt;getActiveSheet()-&gt;getColumnDimension('B')-&gt;setWidth(30);

$objExcel-&gt;setActiveSheetIndex(0);
$sheet = $objExcel-&gt;getActiveSheet();

// Line number
$line = $objExcel-&gt;getActiveSheet()-&gt;getHighestRow() + 1;

/*
    This function write in the Excel file.
    Note that the variables are passed by reference.
*/
function writeExcel(&amp;$objExcel, &amp;$sheet, $email, &amp;$line){
    $sheet-&gt;SetCellValue('A'.$line, $email);
    $sheet-&gt;SetCellValue('B'.$line, date("Y-m-d H:i"));
    $objWriter = new PHPExcel_Writer_Excel2007($objExcel);
    $objWriter-&gt;save(str_replace('.php', '.xlsx', __FILE__));
}    

writeExcel($objExcel, $sheet, $mail, $line);
?&gt;
</code></pre>