Jump to content

New problem with download to excel


ady01

Recommended Posts

I have posted somthing simlar here before and got the output working however its displaying in HTML on the web page not in excel as required, can anyone spot what might be wrong with my code ?

 

<?php
include('config.php');

$db=mysql_connect($AddressBook_HOST,$AddressBook_Username,$AddressBook_Password);
mysql_select_db($AddressBook_DatabaseName,$db);

$result = mysql_query('select * from Addresses') or die(mysql_error());
$count = mysql_num_fields($result);

for ($i = 0; $i < $count; $i++){
    $header .= mysql_field_name($result, $i)."\t";
}

while($row = mysql_fetch_row($result)){
  $line = '';
  foreach($row as $value){
    if(!isset($value) || $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 = "\nSQL table is empty, no data to display. Please add entries to database before using the download function\n";
}


header("Content-type: application/octet-stream");


header("Content-Disposition: attachment; filename=buddylist.xls");
header("Pragma: no-cache");
header("Expires: 0");

echo $header."\n".$data;
?> 

Link to comment
https://forums.phpfreaks.com/topic/105120-new-problem-with-download-to-excel/
Share on other sites

Just thought that the out put might help here, this is what i get when the above is run : the out put matches whats in the table but its not in excel and it doesn't look like the formatting is right either !

 

 

ID Name DOB HouseNumber Street City Country Telephone Fax Email Remarks "50" "522380" "Ptechnology group" "alivemore" "tgoater" "[email protected]" "example" "example" "Joanne A" "[email protected]" "None" "51" "358225" "eservices" "person a" "Person 1" "[email protected]" "example" "example" "Robert" "[email protected]" "Place order Via EDI link"

 

 

redone code now as getting nowhere, getting error like this now - (new code also below) Any idears why i cant get this working  ?

 

 

Parse error: syntax error, unexpected T_VARIABLE in /data/members/paid/a/d/example_co_uk/htdocs/smb1buddylist/excel.php on line 13

 

<?php

$dbToExport=(isset($_GET["db"]))?$_GET["db"]:"Test";

include 'config.php'

$mTblQuery="show tables";
$mTblResult=mysql_query($mTblQuery);

$dataStr="";

while($tblRow=mysql_fetch_assoc($mTblResult)){
    
    $dataStr.="Table : \t".$tblRow["Tables_in_".$dbToExport]."\r\n";
    
   
    $mQuery="select * from `".$tblRow["Tables_in_".$dbToExport]."`";    
    $mResult=mysql_query($mQuery);
    
    
    $numFields=mysql_num_fields($mResult) or die(mysql_error());
    
    
    $tblFields=array();
    for($i=0;$i<$numFields;$i++){
        $tblFields[]=mysql_field_name($mResult,$i);
    }
    
   
    $dataStr.=implode("\t",$tblFields);
    $dataStr.="\r\n";
    
  
    while($row=mysql_fetch_assoc($mResult)){
        $rec=array();
        foreach($tblFields as $tblField){
            $recData=str_replace("\r\n"," ",$row[$tblField]);
            $recData=str_replace("\n"," ",$recData);
            $recData=str_replace("\t"," ",$recData);

            $rec[]=$recData;
        }
        $dataStr.=implode("\t",$rec);
        $dataStr.="\r\n";
    }
    $dataStr.="\r\n";
    $dataStr.="\r\n";
}


header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=export_$dbToExport.xls");
header("Pragma: no-cache");
header("Expires: 0");

echo $dataStr;
?>

Being lazy, I thought that perhaps this class might be of some assistance:

 

 

http://www.phpclasses.org/browse/package/2038.html

 

quote from author:

This class is used for exporting the data from a MySql to spreadsheet files in the Excel format.

 

The class executes a given database query and extract the result set data to generate the Excel spreadsheet.

 

The generated Excel spreadsheet can be stored in a given file or be made available immediately for download.

 

Lite...

 

 

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.