How can I write data into an excel using PHP?

Is it possible to append content to an .xls file using PHP fwrite()?

When I try this using fwrite(), the resulting file causes an error message in Excel 2007.

Is there a specific separator I can use to accomplish this?

Is it possible without a third party library?


Solution 1:

You can use the PhpSpreadsheet library, to read an existing Excel file, add new rows/columns to it, then write it back as a real Excel file.

Disclaimer: I am one of the authors of this library.

Solution 2:

You can try and create a CSV file, like this:

name;surname;blabla
name;surname;blabla
name;surname;blabla
name;surname;blabla

Excel should eat this :)

It is convenient to use PHP CVS functions: http://php.net/manual/en/function.fputcsv.php

Solution 3:

use from fputcsv function for example :

 $data[] = array("item 1", "item 1");
    $export = fopen("file.csv", "w");
    foreach ($data as $row) {
        fputcsv($export, $row, "\t");
    }
    fclose($export);

for more example : https://www.php.net/manual/en/function.fputcsv.php