homepage Welcome to WebmasterWorld Guest from 54.196.63.93
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
generate excel file for download
d40sithui

5+ Year Member



 
Msg#: 3427937 posted 3:05 pm on Aug 21, 2007 (gmt 0)

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?

 

Demaestro

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



 
Msg#: 3427937 posted 3:10 pm on Aug 21, 2007 (gmt 0)

You want the file deleted off the server or you want the version the user gets to be deleted from their computer?

d40sithui

5+ Year Member



 
Msg#: 3427937 posted 3:13 pm on Aug 21, 2007 (gmt 0)

i want the file to be deleted off the server.

Mike12345

10+ Year Member



 
Msg#: 3427937 posted 3:18 pm on Aug 21, 2007 (gmt 0)

Do you want an actual Excel file? or do you want a CSV file compatible with Excel and other spreadsheet programs?

d40sithui

5+ Year Member



 
Msg#: 3427937 posted 3:25 pm on Aug 21, 2007 (gmt 0)

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.

Demaestro

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



 
Msg#: 3427937 posted 3:29 pm on Aug 21, 2007 (gmt 0)

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.

jatar_k

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



 
Msg#: 3427937 posted 3:33 pm on Aug 21, 2007 (gmt 0)

there's one in our library [webmasterworld.com] thread
Good PHP solutions to small problems [webmasterworld.com]

Demaestro

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



 
Msg#: 3427937 posted 3:46 pm on Aug 21, 2007 (gmt 0)

^^^^^

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

d40sithui

5+ Year Member



 
Msg#: 3427937 posted 4:04 pm on Aug 21, 2007 (gmt 0)

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.

Demaestro

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



 
Msg#: 3427937 posted 5:14 pm on Aug 21, 2007 (gmt 0)

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.

d40sithui

5+ Year Member



 
Msg#: 3427937 posted 5:20 pm on Aug 21, 2007 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved