13

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
zod
  • 12,092
  • 24
  • 70
  • 106
  • 3
    possible duplicate of [Alternative for PHP_excel](http://stackoverflow.com/questions/3930975/alternative-for-php-excel) - though the question is not the same, the list in there will answer your question. – Gordon Oct 19 '10 at 13:50
  • What format is your Excel file? Is it a CSV file, a BIFF5 or 8 .xls file, or an MS Office OpenXML .xlsx file? – Mark Baker Oct 19 '10 at 13:51
  • @Gordon . Read the question Mr. I have an excel already.i want to write into that excel using fwrite without classes. . Some guys are here to just down vote . They cant give answers. FTW – zod Oct 19 '10 at 13:56
  • 1
    I did not downvote. I did read the question. I gave 1393 answers up to now. I already agreed the one I closevoted with is not exactly what you asked for. Still, there is plenty other [questions about how to append to an excel file](http://stackoverflow.com/search?q=append+excel+file+php). Unfortunately, you fail to mention what the file format of your Excel file is (like Mark Baker already pointed out), so it's hard to find a proper duplicate. IMO, you are better off using "excel classes". – Gordon Oct 19 '10 at 14:14
  • i ask this on october last year 2010. i got one downvote that month .thats ok . and i accept an answer too. Today i got a downvote(Jul 2011) .Nasty – zod Jun 03 '11 at 16:45

7 Answers7

21

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.

phsource
  • 2,326
  • 21
  • 32
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Thanks . As you are the other can i ask you something.How wil be performance if i write more than 65000 records. Is there any option to add another sheet when it reaches the excel max limit 65536 rows? – zod Oct 19 '10 at 13:52
  • @MarkBaker : is it possible to write into new excel using your lib – Hitesh Mar 04 '14 at 07:34
  • @Hitesh - perfectly possible to create an Excel spreadsheet using PHPExcel – Mark Baker Mar 04 '14 at 08:21
  • @Hitesh - If you are having problems, then "ask a question" (this is a Q&A board, not a forum), don't simply comment on the answer to a different question; and if you're getting errors then tell what you're doing (showing code where appropriate) and what errors you're getting. – Mark Baker Mar 04 '14 at 09:56
  • 1
    Somebody anonymous has just tried to edit this answer to ask another question.... that's not the way things work on StackOverflow – Mark Baker Oct 26 '14 at 12:14
  • In answer to that question: PHPExcel loads data from file into memory, and is constrained by PHP's memory limitations, but there are methods in PHPExcel (such as cell caching) to reduce the memory required when working with large files (although at a cost in execution speed). These are documented, so I'd suggest reading the documentation – Mark Baker Oct 26 '14 at 12:15
  • @MarkBaker The link on this is broken :( – Jeff Davis Feb 17 '17 at 15:26
  • @JeffDavis - It' a pretty old link, on a pretty old answer.... we've been on github for nearly 5 years now... I've updated the answer with the [github link](https://github.com/PHPOffice/PHPExcel) – Mark Baker Feb 17 '17 at 15:47
6

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

Silver Light
  • 44,202
  • 36
  • 123
  • 164
1

to write you can use : Spreadsheet_Excel_Writer is a tool for creating Excel files without the need for COM components

http://pear.php.net/manual/en/package.fileformats.spreadsheet-excel-writer.php but you cant appaend to file , only to create it.

or using phpexcel (support excel 2007)

http://phpexcel.codeplex.com/

and you can append see a example :

http://phpexcel.codeplex.com/Thread/View.aspx?ThreadId=82996

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
  • The Spreadsheet_Excel_Writer is currently unmaintained (last release 2012-01-26). Also causes error "File Error: data may have been lost." [Bug #12848 Excel "File Error: data may have been lost."](http://pear.php.net/bugs/bug.php?id=12848) [WARNING: Spreadsheet::WriteExcel and Office Service Pack 3](https://groups.google.com/forum/?fromgroups=#!topic/spreadsheet-writeexcel/Pc6kDmYgrzo) – sumid Apr 10 '13 at 22:03
1

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

mamal
  • 1,791
  • 20
  • 14
0

if you just want/need to create a very simple table, you just need to make a *.csv-file which can be opened by excel - but not: you can't use formulas in this and you can't do any kind of formatting.

oezi
  • 51,017
  • 10
  • 98
  • 115
  • 2
    Actually, you can use formulas in a CSV file, though you have to take more care that the formula references the correct rows and columns: R1C1 format is often easier to use than A1 format addressing. – Mark Baker Oct 19 '10 at 13:50
0

you can try this library http://phpexcel.codeplex.com/

or you can create .csv file for example and then import them to excel.

samrockon
  • 913
  • 3
  • 12
  • 17
0

I wrote a simple library for exporting Excel-friendly XML files from PHP: http://github.com/elidickinson/php-export-data

Eli
  • 5,500
  • 1
  • 29
  • 27