Jump to content

HTML Generated from PHP into an XLS File


dmaru09

Recommended Posts

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?

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>";
?>

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

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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.