Need script to export mysql to ms-excel

Discussion in 'PHP' started by sundhararajhan, Aug 4, 2008.

  1. #1
    Any one know how to export data from mysql to ms-excel... ?

    Thanks in Advance
     
    sundhararajhan, Aug 4, 2008 IP
  2. alvas

    alvas Peon

    Messages:
    17
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    alvas, Aug 4, 2008 IP
  3. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
  4. blognol

    blognol Peon

    Messages:
    87
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
  5. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #5
    If you are using phpMyAdmin, there is a option to export a excel file. See the 'Export' tab.
     
    rohan_shenoy, Aug 4, 2008 IP
  6. alhen

    alhen Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Here's one I use:
    
    <?php 
    header("Content-type: application/octet-stream"); 
    header("Content-Disposition: attachment; filename=XL_".date("m-d-Y").".xls"); 
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    $hostname = "localhost";
    $username = "YOUR USERNAME";
    $password = "YOUR PASSWORD";
    $database = "YOUR DATABASE NAME";
    $usertable = "YOUR TABLE";
    mysql_connect(localhost,$username,$password); 
    @mysql_select_db($database) or die("Unable to select database"); 
    $select = "SELECT * FROM $usertable"; 
    $export = mysql_query($select); 
    $count = mysql_num_fields($export); 
    for ($i = 0; $i < $count; $i++) { 
    $header .= mysql_field_name($export, $i)."\t"; 
    } 
    while($row = mysql_fetch_row($export)) { 
    $line = ''; 
    foreach($row as $value) { 
    if ((!isset($value)) OR ($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 = "\n(0) Records Found!\n"; 
    } 
    print "$header\n$data"; 
    ?> 
    
    PHP:
     
    alhen, Aug 4, 2008 IP