2011-08-09 155 views
2

你好,我遇到麻煩數組出口值脫穎而出例如我將如何 導出以下:出口值,以Excel文件格式

echo "<table ' border='1' >" 
    echo "<th>ComputerName</th>")); 
    echo "<th>SerialNumber</th>"; 
    echo "<th>SystemModel</th>"; 
    echo "<th>DateTime</th>"; 
    echo "<th>Software</th>"; 
    echo "<th>Hardware</th>"; 
    echo "<th>Support</th>"; 
    echo "<th>Delete</th>"; 
    echo "</tr>"; 

$alt_color = 0; 
while($row = mysql_fetch_array($result)) 

    { 
    error_reporting (E_ALL^E_NOTICE); 
    //foreach($array_values as $value) 
$bgc = ($alt_color % 2 ? 'Dark' : 'Light'); 
    echo "<tr class=".$bgc.">"; 
    //echo "<td><a href=\"update.php?LSUserID=" .$row['LSUserID']."\">" .$row['LSUserID']."</a></td>"; 
    echo "<td><a href=\"update.php?LSUserID=" .$row['LSUserID']."\">" .$row['ComputerName']."</a></td>"; 
    echo "<td>" . $row['SerialNumber'] . "</td>"; 
    echo "<td>" . $row['SystemModel'] . "</td>"; 
    echo "<td>" . $row['DateTime'] . "</td>"; 
+1

將它寫入名爲spreadsheet.xls的文件?相信與否,Excel將以電子表格形式打開HTML表格。 –

+0

不會有更好的csv文件,更大的兼容性和內置的PHP工具。 – 2011-08-09 04:43:40

回答

4

你需要相應的頭文件(那些下面是從在PHP文檔的example

$export = "my_name.xls"; 

header('Pragma: public'); 
///////////////////////////////////////////////////////////// 
// prevent caching.... 
///////////////////////////////////////////////////////////// 
// Date in the past sets the value to already have been expired. 
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");  
header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT'); 
header('Cache-Control: no-store, no-cache, must-revalidate');  // HTTP/1.1 
header('Cache-Control: pre-check=0, post-check=0, max-age=0'); // HTTP/1.1 
header ("Pragma: no-cache"); 
header("Expires: 0"); 
///////////////////////////////////////////////////////////// 
// end of prevent caching.... 
///////////////////////////////////////////////////////////// 
header('Content-Transfer-Encoding: none'); 
// This should work for IE & Opera 
header('Content-Type: application/vnd.ms-excel;'); 
// This should work for the rest 
header("Content-type: application/x-msexcel");  
header('Content-Disposition: attachment; filename="'.basename($export).'"'); 

之後,您的代碼應與一對夫婦的修改工作(Excel可以讀取HTML結構):

error_reporting (E_ALL^E_NOTICE); // you shouldn't have that in the loop. 
            // you actually should put this first 
echo "<table >" 
    // the rest of your table opening code. 

$alt_color = 0; 
while($row = mysql_fetch_array($result)) 
{ 
    echo "<tr>"; 
    // the rest of your columns. 
    echo "</tr>"; 
} 
echo "</table>"; 

如果不工作,出於某種原因,那麼你就可以創建一個CSV,但你不必擔心逃避它:

// add all of the headers. 
echo '"ComputerName","SerialNumber","SystemModel",'; //etc for all columns. 

// row makes sure that there is only one set of values. 
$row = mysql_fetch_row($result); 
if(!$row) die(); // no value found. exit. 
echo getCSVRow($row); 
do // do...while lets us handle the \n more gracefully. 
{ 
    echo "\n". getCSVRow($row); 
} while ($row = mysql_fetch_row($result)); 

function getCSVRow(array $row) 
{ 
    $ret = ""; 
    foreach($row as $val) 
     $ret .= '"' . escapeCSV($val) . '",'; 
    return sustr($ret, 0, strlen($ret)); // clear the tailing comma 
} 

function escapeCSV($str) 
{ 
    return str_replace('"', '\"', $str); 
} 
1

我發現從谷歌的AppInventor大量靈感,創造出在PHP/MySQL的環境數據庫導出工具。 檢查站點:http://www.freegroup.de/software/phpBlocks/demo.html

+0

需要更清楚一點的解釋,但鏈接似乎去有效的軟件技術。 @Andreas通常你應該嘗試着寫一篇文章,而不僅僅是有一個鏈接,尤其是從堆棧溢出開始,由於鏈接機器人的流行。 – Kzqai

0

使用phpBlock,您可以在拖放環境中創建您的db-> excel導出,這受到來自MIT的OpenBlock或來自Google的AppInventor的啓發。 這意味着沒有必要在PHP中編寫任何代碼。

但是導出功能只是php-lib的一種可能性。您可以將此lib用作現有PHP應用程序的動態擴展。像dyn一樣。公式或計算。 我希望這比之前發佈的更清楚。