Welcome to WebmasterWorld Guest from 54.166.224.46

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

generate excel file for download

     
3:05 pm on Aug 21, 2007 (gmt 0)

5+ Year Member



hi all,
I'm trying to create a functionn that will generate an excel spreadsheet, and present it for download. after the user downloads it, the file should be deleted automatically. any suggestions?
3:10 pm on Aug 21, 2007 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member



You want the file deleted off the server or you want the version the user gets to be deleted from their computer?
3:13 pm on Aug 21, 2007 (gmt 0)

5+ Year Member



i want the file to be deleted off the server.
3:18 pm on Aug 21, 2007 (gmt 0)

10+ Year Member



Do you want an actual Excel file? or do you want a CSV file compatible with Excel and other spreadsheet programs?
3:25 pm on Aug 21, 2007 (gmt 0)

5+ Year Member



at this point, i need the easiest would be to generate a CSV file. I thikn i know how to do this, but to delete it automatically, thats the hard part.
3:29 pm on Aug 21, 2007 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member



What you can do then is create a page called example.xls put PHP code into it that will dynamically create the file.

An excel file is just a tab delimited file at it's most basic and the rows are delimited by carriage returns.

So being cautious of extra white space write a loop that will set up the first row with column headers if you want them. DO NOT name the first column just id if you want id use, store_id or something as Microsoft will think it is a different file if it starts with id for some reason.

So the code should look something like You will have to PHP this if that is what you are using:
***********
header_array = ['column1', 'column2', 'column3', 'column4']
data = sql_execute('select column1, column2, column3, column4 from table where condition = condition')
for value in header_array:
..echo(value) echo(chr(9))
..if end_of_loop:
....echo(chr(13))
for value in data:
..echo(data['column1'])
..echo(data['column2'])
..echo(data['column3'])
..echo(data['column4'])
..echo(chr(13))
***********

So what you will want to do is give the user a link or a form to submit and make this page the landing page.

Their browser will see the .xls file extension and ask the user want they want to do with the file. The file will be written and sent to the browser without a copy having ever really been made on your server.

I hope this will work for you.

3:33 pm on Aug 21, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member



there's one in our library [webmasterworld.com] thread
Good PHP solutions to small problems [webmasterworld.com]
3:46 pm on Aug 21, 2007 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member



^^^^^

Ya that is nice.... my solution is something meant more for Python. Although the poster doesn't say what language. I foresee using that example so thanks J

4:04 pm on Aug 21, 2007 (gmt 0)

5+ Year Member



hey thanks guys for all the help.

demaestro:
i have some problems with your method. everything thats inside the example.xls file will not be evaluated. so even if i put php code into it, it wil just display it as text. thanks anyway, regardless.

jatar:
your script seems to work ok. i think i'll build on that and see how it goes.

i appreciate your help.

5:14 pm on Aug 21, 2007 (gmt 0)

WebmasterWorld Senior Member demaestro is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Sorry I always forget about the stuff I have running in the background.

You would have had to name it example.xls.php and then do Apache trick to remove the .php when presenting to the user after the code is rendered.

Jatar's solution is the best for PHP by far though.

5:20 pm on Aug 21, 2007 (gmt 0)

5+ Year Member



haha but just out of curiousity, what is the apache trick you are speaking of?
 

Featured Threads

Hot Threads This Week

Hot Threads This Month