dmaru09 Posted June 15, 2009 Share Posted June 15, 2009 Hey guys... Very new to everything with PHP, but I am working on a project that was passed down to me. Here's basically what I'm looking to do. I am making an invoice system, which you can generate reports from. On my page Display.php there is a table, which is generated based on the search the user performed on the page before. It pulls the desired objects from an SQL database and places them in a nicely formatted HTML Table. There is other HTML on this page, other then the table. What I'd like to do is put a button on the page where when it is clicked it opens a new file, with all of the HTML from the table (and ONLY the html from the table) reproduced and simply placed in an XLS file, as raw HTML can be read and understood by Excel. Is this easily achieved? Link to comment https://forums.phpfreaks.com/topic/162250-html-generated-from-php-into-an-xls-file/ Share on other sites More sharing options...
rhodesa Posted June 15, 2009 Share Posted June 15, 2009 sure, make the link like so: <a href="?download=true">Download</a> that will link to the same page with an extra GET argument. then in your page, check for the arg, then print accordingly: <?php $download = isset($_GET['download']) ? true : false; //Some headers for downloading if($download header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" ); header ( "Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT" ); header ( "Pragma: no-cache" ); header ( "Content-type: application/x-msexcel" ); header ( "Content-Disposition: attachment; filename=data.xls" ); } if(!$download){ ?> Here is the header stuff <?php } //Now the table echo "<table>"; foreach($rows as $row){ echo "<tr><td>$row</td></tr>"; echo "</table>"; ?> Link to comment https://forums.phpfreaks.com/topic/162250-html-generated-from-php-into-an-xls-file/#findComment-856291 Share on other sites More sharing options...
dmaru09 Posted June 15, 2009 Author Share Posted June 15, 2009 Wow, thank you for the quick response. Ran into an issue though: Getting an error when I try to run my display.php page now. Refers me to line 17 which is: header ( "Expires: Mon, 1 Apr 1974 05:00:00 GMT" ); Error: Parse error: syntax error, unexpected T_STRING in /export/home/oracle/itcrapp/ITInvoice/Display.php on line 17 Link to comment https://forums.phpfreaks.com/topic/162250-html-generated-from-php-into-an-xls-file/#findComment-856308 Share on other sites More sharing options...
Mark Baker Posted June 15, 2009 Share Posted June 15, 2009 Even Aaron occasionally typos if($download should be if($download) Link to comment https://forums.phpfreaks.com/topic/162250-html-generated-from-php-into-an-xls-file/#findComment-856312 Share on other sites More sharing options...
dmaru09 Posted June 15, 2009 Author Share Posted June 15, 2009 yep just caught that myself - thanks again - shoulda looked deeper before i asked. Link to comment https://forums.phpfreaks.com/topic/162250-html-generated-from-php-into-an-xls-file/#findComment-856313 Share on other sites More sharing options...
dmaru09 Posted June 15, 2009 Author Share Posted June 15, 2009 Wow Works better then I could have expected, and very fast reponses/help. Thanks a lot guys, this forum is great. Link to comment https://forums.phpfreaks.com/topic/162250-html-generated-from-php-into-an-xls-file/#findComment-856316 Share on other sites More sharing options...
Mark Baker Posted June 15, 2009 Share Posted June 15, 2009 PS. <BLATANT PLUG> If you want to generate a real Excel file rather than an HTML file with an extension of .xls, just look to my signature Link to comment https://forums.phpfreaks.com/topic/162250-html-generated-from-php-into-an-xls-file/#findComment-856326 Share on other sites More sharing options...
dmaru09 Posted June 15, 2009 Author Share Posted June 15, 2009 Hmm, currently what I have working is more then enough. Thank you though. I do have a question... the table i actually want to appear in the excel file is slightly different then the one viewed on the site. Is there anyone I can create the new table below the old one, but make it not visible? (already have the export working correctly, but theres a table under the table on the actual webpage) Link to comment https://forums.phpfreaks.com/topic/162250-html-generated-from-php-into-an-xls-file/#findComment-856338 Share on other sites More sharing options...
rhodesa Posted June 15, 2009 Share Posted June 15, 2009 Even Aaron occasionally typos if($download should be if($download) Even Aaron FREQUENTLY typos Link to comment https://forums.phpfreaks.com/topic/162250-html-generated-from-php-into-an-xls-file/#findComment-856355 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.