Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 608
  • Last Modified:

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

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
ericwolfe
Asked:
ericwolfe
  • 3
  • 2
2 Solutions
 
venkateshwarrCommented:
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
 
ericwolfeAuthor Commented:
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
 
ericwolfeAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jkna_gunnCommented:
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
 
jkna_gunnCommented:
0
 
ericwolfeAuthor Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now