Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , ,

Sponsored Links



php export to excel

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 25-08-2009
Member
 
Join Date: Oct 2008
Posts: 78
php export to excel

How to php export to excel ? i want to export the data in table to the excel file through php . Please reply if you know how to do this. Thanks for any help.
Reply With Quote
  #2  
Old 25-08-2009
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,937
Re: php export to excel

The below php code can help you :

Code:
<?php

// DB Connection here

$select = "SELECT * FROM table_name";

$export = mysql_query($select) or die("Sql error : " . mysql_error());

$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) || trim($value) == "")
        {
            $value = "\t";
        }
        else
        {
            $value = str_replace('"' , '""' , $value);
            $value = '"' . $value . '"' . "\t";
        }
        $line .= $value;
    }
    $data .= trim($line). "\n";
}
$data = str_replace("\r" , "" , $data);

if(trim($data) == "")
{
    $data = "\n(0)Records Found!\n";                        
}

header("Content-type: application/msexcel");
header("Content-Disposition: attachment; filename=your_desired_name.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\n$data";

?>
See this thread : export data in the bd Mysql to the Excel file
Reply With Quote
  #3  
Old 25-08-2009
kelfro's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,976
Re: php export to excel

Simplest way to generate an excel report.

Code:
<?php

      $filename ="testreport.xls";

      $contents = "testdata1 \t testdata2 \t testdata3 \t \n";

      header('Content-type: application/ms-excel');

      header('Content-Disposition: attachment; filename='.$filename);

      echo $contents;

?>
Reply With Quote
  #4  
Old 25-08-2009
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
Re: php export to excel

I have successfully generated this xml with PHP and then opening it up using ms excel. It's nothing but just using a template to generate "XML Spreadsheet" data instead of HTML.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "php export to excel"
Thread Thread Starter Forum Replies Last Post
How to export ical to excel Xmen Windows Software 5 02-09-2010 08:41 AM
data export to excel Irene Microsoft Project 1 11-12-2009 09:27 PM
Export OLE object to Excel? Kelsey Windows Software 3 31-03-2009 10:29 PM
How can i export datatable to excel Leena Software Development 3 30-01-2009 11:45 PM
Export to Excel BW Microsoft Project 7 11-12-2008 12:44 AM


All times are GMT +5.5. The time now is 11:13 AM.