Export table data to excel - error when open by excel 2007

Discussion in 'PHP' started by figo2476, Jul 25, 2007.

  1. #1
    Hi:

    I use a php script to export table data to excel. Excel 2007 says the file format is different specified by the file extension...

    
    
    <?php
    	$dbhost = "localhost"; // Usually localhost.
    	$dbname = "testdb"; // Database name.
    	$dbuser = "root"; // Database username.
    	$dbpass = "pass"; // Database password.
    	$dataname = "web-data"; // FILENAME PREFIX THAT WILL BE ADDED TO XL FILE.
    	$tablename = "user"; // name of table to extract data from.
    	$fields = "id, username, password, regi_time"; // the fields in the table you want. seperated with comma. Example ($fields = "field1, field2, field3";)
    	//
    	//////////////////////////////////////////////////
    	/////////// DO NOT EDIT THIS PART OF FILE ////////
    	//////////////////////////////////////////////////
    	$datestamp = date("d-m-Y");
    	$dbh=mysql_connect ($dbhost, $dbuser, $dbpass) or die ('I cannot connect to the database because: ' . mysql_error());
    	mysql_select_db ($dbname);
    	$query = ("SELECT $fields FROM $tablename");
    	$result=mysql_query($query) or die('Error, unsuccesseful when querying database');
    	$myarray = array();
    	while($row = mysql_fetch_array($result, MYSQL_NUM))
    	{
    	$myarray[] = implode("\t", $row);
    	}
    	$myarray = implode("\r\n", $myarray);
    	$filename = "$dataname-$datestamp.xls";
    	
    	header("Content-Disposition: attachment; filename=$filename"); 
    	header("Content-Type: application/vnd.ms-excel");
    	
    	echo $myarray;
    ?>
    
    
    
    PHP:

     
    figo2476, Jul 25, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It looks like you're creating the file in tab delimited format, yet naming it "*.xls"?

    Try naming it "*.txt" instead.
     
    ecentricNick, Jul 25, 2007 IP
  3. metalidx

    metalidx Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I do this all the time, but I use .csv as the extension instead of .xls.

    Works great :D
     
    metalidx, Jul 25, 2007 IP
  4. figo2476

    figo2476 Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    * Ya, it works with .csv
    * I realize that all data stick to one column...
     
    figo2476, Jul 25, 2007 IP
  5. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    All your data is in one column because you used tabs to delimit your data and called it csv (Comma seperated values). If you use commas instead of tabs, it'll go into different columns.

    However, if you call it *.txt, instead of *.csv, Excel will treat it as tab delimited and will put your data in seperate columns.
     
    ecentricNick, Jul 25, 2007 IP
  6. figo2476

    figo2476 Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    * thx
    * Almost there. Timestamp can be displayed correctly in .txt, but not .csv....
    It looks like ######
     
    figo2476, Jul 25, 2007 IP
  7. bloodredxxx

    bloodredxxx Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    just expand the width of the column with the "#####" data to be able to view it properly. The correct data is in there, it's just that excel doesnt automatically resize column widths.
     
    bloodredxxx, Jul 25, 2007 IP
  8. figo2476

    figo2476 Peon

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thank you~~~~
     
    figo2476, Jul 26, 2007 IP