2012-09-21 67 views
0

我想從mysql表格導出數據到excel表格。我使用的是excel 2007.一旦這段代碼工作正常,但今天我遇到了問題。請指導我在哪裏做錯了。我有大約60,000行的大量數據。如何將mysql表格數據導出到excel 2007

<?php 
/* 
Export MySQL to Excel using PHP & HTML tables 
Author: Vlatko Zdrale, http://blog.zemoon.com 

Look but don't touch :) 
*/ 
    include "mysql_connection.php"; 
    //$dbTable = 'info';   // table name 
    $con=open_db_connection(); 


    $sql = "select info_id, name, category_list.category, company_name, company_address, company_phone, date from info, city_list, category_list where city=cid and info.category=id"; 
    $result = @mysql_query($sql) or die("Couldn't execute query:<br>".mysql_error().'<br>'.mysql_errno()); 

    header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //define header info for browser 
    header('Content-Disposition:attachment; filename=information-'.date('Ymd').'.xlsx'); 
    header('Pragma: no-cache'); 
    header('Expires: 0'); 

    echo '<table><tr>'; 
    for ($i = 0; $i < mysql_num_fields($result); $i++) // show column names as names of MySQL fields 
     echo '<th>'.mysql_field_name($result, $i).'</th>'; 
    print('</tr>'); 

    while($row = mysql_fetch_row($result)) 
    { 
     //set_time_limit(60); // you can enable this if you have lot of data 
     $output = '<tr >'; 
     for($j=0; $j<mysql_num_fields($result); $j++) 
     { 
      if(!isset($row[$j])) 
       $output .= '<td>&nbsp;</td>'; 
      else 
       $output .= "<td>$row[$j]</td>"; 
     } 
     print(trim($output))."</tr>\t\n"; 
    } 
    echo('</table>'); 
?> 

它很重要請引導我。提前致謝。

+0

您是否也有Microsoft Access? – JMK

+0

你有什麼問題? – Passerby

+0

重複http://stackoverflow.com/questions/7706093/exporting-mysql-data-into-excel-csv-via-php? –

回答

1

您將得到該消息,因爲該文件不是OfficeOpenXML xlsx文件,而是包含具有.xlsx擴展名的HTML標記的文件。您告訴Excel,當文件是另一個文件時,該文件是擴展名中的一種格式,並且讓您知道內容與擴展名不匹配。只要它可以乾淨地讀取HTML標記,它仍然應該加載成功,但總是會發出此消息。

最近的Excel版本比早期版本更挑剔。

如果您想刪除該消息,則要麼將.xlsx重命名爲.html文件,以便擴展名與內容匹配(然後您需要導入到Excel中,或者使用文件關聯來使文件關聯使用Excel打開html文件);或者給它一個帶有csv擴展名(可以通過雙擊打開)的製表符分隔的值文件,但是不能使用此選項添加任何格式;或者給它一個真正的OfficeOpenXML .xlsx文件

+0

是的,你是正確的。但我只想要一個OfficeOpenXML xlsx我怎麼才能實現上述代碼的簡單修改? –

+0

你不能通過對代碼的簡單修改來實現它,你需要使用一個庫,例如PHPExcel,它實際上創建了真正的OfficeOpenXML文件 –

+0

Ohhk謝謝,但現在我已經厭倦了代碼,所以我只是把excel表連接到mysql使用odbc連接器。現在沒有必要一直導出當我數據更新在MySQL表中一次又一次。 :) –

0

首先,您應該將輸出放在緩衝區變量中,而不是一直打印或打印。

之後,您應該閱讀您自己的評論 - > // set_time_limit(60); //如果您有大量數據,則可以啓用此功能

+0

我很新的PHP中,所以不知道whw把輸出放入緩衝區變量會給我一個簡單的例子 –

+0

只要把'$ output ='' ;'在開頭用'$ output。='代替'echo'和'print',然後在最後加上'print $ output;'。 –

0

首先:告訴我們有關錯誤的信息!

我建議你做以下兩個步驟之一(或兩者)。

set_time_limit(60); 
// increase this value to 300 (means the server doesn't timeout for 5 minutes) 

ini_set('memory_limit','512M'); 
// increases the memory limit to 512 MB 
+0

感謝您的建議,但我仍然得到同樣的錯誤。我hv添加了兩個建議的行,但得到相同的錯誤 –

0

嘗試使用「\ t」 的列之間不是行,這讓Excel中的本地偏移/邊距。 也更好地使用與大括號循環作爲一個更好的標準,不要混淆自己。

0

從你的評論我猜會有東西搞亂了標記。

<th></th><tr></tr>之間的一切,儘量使用htmlentities

for ($i = 0; $i < mysql_num_fields($result); $i++) // show column names as names of MySQL fields 
    echo '<th>'.htmlentities(mysql_field_name($result, $i),ENT_COMPAT,"UTF-8").'</th>'; 

而且

if(is_null($row[$j])) 
    $output .= '<td>&nbsp;</td>'; 
else 
    $output .= "<td>".htmlentities($row[$j],ENT_COMPAT,"UTF-8")."</td>"; 

請注意,我假設你的編碼是UTF-8。

相關問題