Re: generate xls file on fly
| From: | Hiep Nguyen | Date: | Mon, 11 Feb 2008 12:41:11 +0000 |
| Subject: | Re: generate xls file on fly | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-269078@lists.php.net to get a copy of this message | ||
On Fri, 8 Feb 2008, Jason Pruim wrote:
On Feb 8, 2008, at 10:14 AM, Hiep Nguyen wrote:why not store the whole SQL statement in a session variable, so it can work with any table??? i never use session before, but i'll look into it to see if it works for me. any suggestion for a tutorial on session in php??? thanks.On Fri, 8 Feb 2008, Per Jessen wrote:I have actually done what you are looking for... Here is how I do it: On the search page, write the search phrase into a session variable so you can access it from another page and then run this code: <?PHP $sortOrder = $_SESSION['order']; $search = $_SESSION['search']; $select = "SELECT * FROM ".$table." WHERE blah blah blah blah blah"";Hiep Nguyen wrote:i already got this method, but the problem that i have is the parameters is mysql statement and it's very long. i don't think a good idea to pass this in the url. also, the page that i'm working on is a search page, therefore the mysql statement is very complicate and long. that's why i don't want to pass via url. is there way to do within this page???is there anyway to generate this into xls file w/o using fopen & fwrite to the server? my goal is to have a link after the table and user can click on that link and a save window pop up to allow user to save to local disk.Yes - have a link like this: <a href="<yourscript.php?parameters>">Get XLS file</a> in yourscript.php, you evaluate the parameters given and build the XSL file as output: header("Content-Type: application/excel"); header("Content-Disposition: attachment; filename=\"filename\""); print . . . . . done.
$export = mysql_query($select); $fields = mysql_num_fields($export); for ($i = 0; $i < $fields; $i++) { $header .= mysql_field_name($export, $i) . "\t"; } while($row = mysql_fetch_row($export)) { $line = ''; foreach($row as $value) { if ((!isset($value)) or ($value == "")) { $value = "\t"; } else { $value = str_replace('"', '""', $value); $value = '"' . $value . '"' . "\t"; } $line .= $value; } $data .= trim($line). "\n"; } $data = str_replace("\r", "", $data); if ($data =="") { $data ="\n(0) Records Found!\n"; } header("Content-type: application/x-msdownload"); header("Content-Disposition: attachment; filename=Export.xls"); header("Pragma: no-cache"); header("Expires: 0"); print "$header\n$data"; ?> What that does is it writes the current search pattern into an excel file called Export.xls and downloads it to your harddrive. I am in the process of re-writing it to work as a function, but have had mixed results so far. I'm learning functions right now :) If you know functions, and want to take a stab at re-writing it, I can provide you with the code I have so far :) -- Jason Pruim Raoset Inc. Technology Manager MQC Specialist 3251 132nd ave Holland, MI, 49424 www.raoset.com japruim@raoset.com