Solved

How to insert data into existing MS Excel document from php?

Posted on 2004-04-08
6
596 Views
Last Modified: 2007-12-19
I have a web app written in php/mysql running on linux server.  I also have an existing excel document (preformatted with fonts, formulas,etc).  I want to be able to insert data from the mysql database into specific cells in the Excel worksheet.

I have fairly extensive knowledge of handling the database stuff with php/mysql.  What I need is a way to get the data from php to the specific cells in Excel.  I've seen stuff on creating dynamic excel documents, but my excel sheet is already created, I just need to get data into it.  I'd also like to be able to save that excel document with a new name each time i do an export.  This way I can keep a blank template and repeat this process with new data as often as I want.

So, in short, I need to be able to open an existing excel document, drop some data in some specific cells, and then save the excel document with a new name while preserving the original excel document.

Any help is much appreciated!
0
Comment
Question by:ericwolfe
  • 3
  • 2
6 Comments
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 300 total points
Comment Utility
Did you try

Spreadsheet_Excel_Writer: http://pear.php.net/package-info.php?pacid=156

There are several retail products available but this is one free..
0
 

Author Comment

by:ericwolfe
Comment Utility
I hadn't seen that one yet.  Checked it out.  Looks promising....BUT...it does not appear you can edit an already existing worksheet.  At least, by what I gather from it you can't.

If I use something that generates a new page from scratch each time, I'll have to add the sheet's formatting each time I create a page.  This would be acceptable if there is an easy way to export the formatting of the page without having to hand code it.  Yeah, I am kinda lazy, but there could potentially be a lot of worksheets I would have to do this to which would be very time consuming.

If you've used the Spreadsheet_Excel_Writer and I am overlooking something about opening existing files - let me know.

Thanks.
0
 

Author Comment

by:ericwolfe
Comment Utility
Ok, I think I'm on to something here.  I can save my current excel documents as html files.  Then, I should be able to populate the correct cells with the data using php to edit the html file.

Now, I just need an excel generator that will accept html to create the excel file.  The pear solution mentioned above does not appear to accept html to build the excel file.  
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 6

Assisted Solution

by:jkna_gunn
jkna_gunn earned 200 total points
Comment Utility
you should be able to put data into an existing excel sheet,
i have managed to alter data in active word and powerpoint files, the same should be true for excel.

im not at my development machine at the mo, so can't get the code.
but should be able to later today, if you still need it...
0
 
LVL 6

Expert Comment

by:jkna_gunn
Comment Utility
0
 

Author Comment

by:ericwolfe
Comment Utility
The problem with the COM stuff is you have to running on Windows (so far as I can tell).  I'm running on linux.  And actually, today I came up with a solution.  Here's what I got working:

1. Save excel document as html.

2. Add necessary php to html to fill cells.

3. Save as php file.

4. Run another php script that opens and reads it into buffer and spits out a new file with an .xls extension.

5. Send file to user with headers

6. Delete .xls file on server.


So far, so good!  since the html generated by excel includes xml (i think thats what it is) - when you rename it back to a xls file, excel has no problems opening it.  Step 2 was a little weird...had to play around with the file in excel to see what changes it made to the html.  In my case, this didnt turn out being too bad - but I can see how this would get weird.

Anyway, thanks to both of you for contributing.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now