Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 204
zod Avatar asked Oct 19 '10 13:10

zod


People also ask

Can PHP write to an Excel file?

It is called PHP Excel library. It enables you to read and write spreadsheets in various formats including csv, xls, ods, and xlsx. You will need to ensure that you have PHP's upgraded version not older than PHP 5.2 .

How connect Excel to PHP?

Establish a ConnectionOpen the connection to Excel by calling the odbc_connect or odbc_pconnect methods. To close connections, use odbc_close or odbc_close_all. $conn = odbc_connect("CData ODBC Excel Source","user","password"); Connections opened with odbc_connect are closed when the script ends.

How do I insert data from a website into Excel?

Select Data > Get & Transform > From Web. Press CTRL+V to paste the URL into the text box, and then select OK. In the Navigator pane, under Display Options, select the Results table.


2 Answers

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.

like image 110
Mark Baker Avatar answered Oct 07 '22 00:10

Mark Baker


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

like image 41
mamal Avatar answered Oct 06 '22 22:10

mamal