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

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-08-2009
Member
 
Join Date: Oct 2008
Posts: 77
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
Member
 
Join Date: Apr 2008
Posts: 1,948
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
Member
 
Join Date: Apr 2008
Posts: 2,001
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
Member
 
Join Date: Oct 2005
Posts: 2,389
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
Tags: , , ,



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 07:41 AM
Export Gridview to Excel in VB.Net Ariadne Software Development 4 15-06-2010 06:30 PM
Export OLE object to Excel? Kelsey Windows Software 3 31-03-2009 09:29 PM
How can i export datatable to excel Leena Software Development 3 30-01-2009 11:45 PM
Export to Excel nehal_serpa Microsoft Project 6 11-12-2008 12:44 AM


All times are GMT +5.5. The time now is 02:57 AM.