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?

5
  • 3
    possible duplicate of Alternative for PHP_excel - though the question is not the same, the list in there will answer your question. Commented Oct 19, 2010 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? Commented Oct 19, 2010 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 Commented Oct 19, 2010 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. 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". Commented Oct 19, 2010 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 Commented Jun 3, 2011 at 16:45

7 Answers 7

22

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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?
@MarkBaker : is it possible to write into new excel using your lib
@Hitesh - perfectly possible to create an Excel spreadsheet using PHPExcel
@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.
Somebody anonymous has just tried to edit this answer to ask another question.... that's not the way things work on StackOverflow
|
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

Comments

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

1 Comment

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." WARNING: Spreadsheet::WriteExcel and Office Service Pack 3
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

Comments

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.

1 Comment

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.
0

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

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

Comments

0

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

1 Comment

the OP asks for XLS and the class you suggest export CSVs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.