Results 1 to 4 of 4

Thread: php export to excel

  1. #1
    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.

  2. #2
    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 : http://forums.techarena.in/software-...nt/1039042.htm

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    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;
    
    ?>

  4. #4
    Join Date
    Oct 2005
    Posts
    2,393

    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.

Similar Threads

  1. How to export ical to excel
    By Xmen in forum Windows Software
    Replies: 5
    Last Post: 02-09-2010, 07:11 AM
  2. Export Gridview to Excel in VB.Net
    By Ariadne in forum Software Development
    Replies: 4
    Last Post: 15-06-2010, 06:00 PM
  3. Export OLE object to Excel?
    By Kelsey in forum Windows Software
    Replies: 3
    Last Post: 31-03-2009, 08:59 PM
  4. How can i export datatable to excel
    By Leena in forum Software Development
    Replies: 3
    Last Post: 30-01-2009, 11:15 PM
  5. Export to Excel
    By nehal_serpa in forum Microsoft Project
    Replies: 6
    Last Post: 11-12-2008, 12:14 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,638,250,156.97250 seconds with 17 queries