Page 1 of 1

exporting mysql table to excel

Posted: Thu Jul 26, 2007 11:55 pm
by anna_cm
Hi All,

I need a script to download the mysql table to an excel file. I used the phpfreaks tutorial script but it is not opening in excel sheet.

Please help

Thanks,

anna

Posted: Fri Jul 27, 2007 12:50 am
by timvw
The first thing you need to do is read viewtopic.php?t=8815..

Posted: Fri Jul 27, 2007 6:01 am
by aceconcepts
Anna,

Follow this link: http://www.phpfreaks.com/tutorials/114/0.php

Try using search engines to find scripts i.e. in this instance search for something as simple as "php to excel".

Hope the link helps :D

Posted: Fri Jul 27, 2007 6:13 am
by Gente

use navicat

Posted: Fri Jul 27, 2007 6:58 am
by yacahuma
you can also use navicat, point and click no code needed

Posted: Fri Jul 27, 2007 7:31 am
by phpdevuk
There are loads of ways to create a file in php that can be opened in excel, you can export in HTML or CSV, or even write a binary xls file.

Posted: Fri Jul 27, 2007 11:44 pm
by anna_cm
Hi All,

Thanks for your reply.

I cannot use pear class as it is not installed in my clients server.

Below is my code, this is downloading an excel file but when opened it is showing as a text file with values seperated with tab.

Code: Select all

<?php
include('../../connection.php');
$result = mysql_query('select * from subscriber');
$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 = "\nno matching records found\n";
}

header("Content-type: application/x-excel");
//header("Content-type: application/x-msdownload");
header ("Cache-Control: no-cache, must-revalidate");  
header("Content-Disposition: attachment; filename=excelfile.xls");
header("Pragma: no-cache");
header("Expires: 0");

echo "$header\n$data"; 
?>
If anyone knows what went wrong in the code, Please let me know.

anna

Posted: Sat Jul 28, 2007 2:38 am
by timvw
That's because the code, which you obviously copied without understanding it, generates CSV (which Excel should be able to parse....)