Jump to content

help with a tutorial, excel download


simon551

Recommended Posts

I'm trying out the suggestions in <a href="http://www.phpfreaks.com/tutorials/114/5.php?linear=true#top>this </a> tutorial.

 

This is my code:

<?php 
require_once('../Connections/conn_org.php'); 
require_once('../scripts/math.php');
require_once('../sources-php/user_roles.php');
session_start();

$div=$_SESSION['divAdmin'];
//echo $div;
mysql_select_db($database_conn_org, $conn_org);
$query = "SELECT * FROM entries";


$export = mysql_query($query);
$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=extraction.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$headern$data";  

?>

It's downloading to excel but when the file opens all data is in one cell and looks really gobbledy-gooky like this:

planning"t"0"t"30"t"2007-04-25 18:07:52"t"2007-04-25 18:07:52"t"0"tn"24"t"91"t"2006-09-06"ttt"2007-04-24 21:23:34"t"61244"t"47"t"24"t"894"tttt"Finalize

 

Link to comment
https://forums.phpfreaks.com/topic/61155-help-with-a-tutorial-excel-download/
Share on other sites

  • 1 month later...

I tried many of the tutorials out there for excel sheet downloads.

 

I found this method to be much easier and reusable.

 

1:) create an object on the page where your original table is.

<?php ob_start();  //create the object, and start writing table 
?>
<table width="50%" border="1" align="center" cellspacing="2">
<tr bgcolor="#333333" class="style33">

 

2:) save the object table as a variable, and output the table to the page

 

<?php 
$table_excel = ob_get_contents(); //copy contents of table to var
ob_flush(); //output the table to the page, for viewing.
$_SESSION['page'] = $table_excel; // set the session var to be passed to the 'excel.php' page. 
?>

 

3:) which is then passed to an excel.php page for download.

 

<?php //excel.php this page will send the table data to client formatted as a .xls  excel sheet.
session_start();

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=Report.xls"); //set filename to your liking.
header("Pragma: no-cache");
header("Expires: 0");
echo $_SESSION['page'];
?>

 

4:) then you can use some type of link to the excel.php page which will send the file as a download.

 

  • 4 months later...

I filed this response to my post away to try and finally got around to it. If anyone can help me, that would be great.

 

I can't get the example to work but not sure why.

 

this is my first page, excellent.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:spry="http://ns.adobe.com/spry">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>excellent</title>
</head>
<body>
<?php ob_start();  //create the object, and start writing table 
?>
<table width="50%" border="1" align="center" cellspacing="2">
<tr>
<td>1</td>
<td>2</td>
</tr>
</table>
<?php 
$table_excel = ob_get_contents(); //copy contents of table to var
ob_flush(); //output the table to the page, for viewing.
$_SESSION['page'] = $table_excel; // set the session var to be passed to the 'excel.php' page. 
?>
<a href="excel.php">link</a>
</body>
</html>

 

and the download page, excel.php

<?php //excel.php this page will send the table data to client formatted as a .xls  excel sheet.
session_start();

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=Report.xls"); //set filename to your liking.
header("Pragma: no-cache");
header("Expires: 0");
echo $_SESSION['page'];
?>

 

When I click the link, a download dialog opens and excel opens but the worksheet is blank. any ideas?

  • 6 months later...

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.