2010-03-22 68 views
2

如何使用PHP從mySQL表創建一個.XLS文檔?mySQL to XLS using PHP?

我已經嘗試了幾乎所有的東西,沒有成功。

基本上,我需要採取表單數據,並將其輸入到我已經完成的數據庫中,然後我需要檢索該表數據並將其解析爲Microsoft Excel文件,該文件需要自動保存到Web服務器。

<?php 

// DB TABLE Exporter 
// 
// How to use: 
// 
// Place this file in a safe place, edit the info just below here 
// browse to the file, enjoy! 

// CHANGE THIS STUFF FOR WHAT YOU NEED TO DO 

    $dbhost = "-"; 
    $dbuser = "-"; 
    $dbpass = "-"; 
    $dbname = "-"; 
    $dbtable = "-"; 

// END CHANGING STUFF 

$cdate = date("Y-m-d"); // get current date 


// first thing that we are going to do is make some functions for writing out 
// and excel file. These functions do some hex writing and to be honest I got 
// them from some where else but hey it works so I am not going to question it 
// just reuse 


// This one makes the beginning of the xls file 
function xlsBOF() { 
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); 
    return; 
} 

// This one makes the end of the xls file 
function xlsEOF() { 
    echo pack("ss", 0x0A, 0x00); 
    return; 
} 

// this will write text in the cell you specify 
function xlsWriteLabel($Row, $Col, $Value) { 
    $L = strlen($Value); 
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); 
    echo $Value; 
    return; 
} 



// make the connection an DB query 
$dbc = mysql_connect($dbhost , $dbuser , $dbpass) or die(mysql_error()); 
mysql_select_db($dbname); 
$q = "SELECT * FROM ".$dbtable." WHERE date ='$cdate'"; 
$qr = mysql_query($q) or die(mysql_error()); 

// start the file 
xlsBOF(); 

// these will be used for keeping things in order. 
$col = 0; 
$row = 0; 

// This tells us that we are on the first row 
$first = true; 

while($qrow = mysql_fetch_assoc($qr)) 
{ 
    // Ok we are on the first row 
    // lets make some headers of sorts 
    if($first) 
    { 
     foreach($qrow as $k => $v) 
     { 
      // take the key and make label 
      // make it uppper case and replace _ with ' ' 
      xlsWriteLabel($row, $col, strtoupper(ereg_replace("_" , " " , $k))); 
      $col++; 
     } 

     // prepare for the first real data row 
     $col = 0; 
     $row++; 
     $first = false; 
    } 

    // go through the data 
    foreach($qrow as $k => $v) 
    { 
     // write it out 
     xlsWriteLabel($row, $col, $v); 
     $col++; 
    } 
    // reset col and goto next row 
    $col = 0; 
    $row++; 
} 

xlsEOF(); 
exit(); 
?> 

我只是似乎無法弄清楚如何FWRITE融入所有寫入生成的數據轉換爲.xls文件,我將如何去這樣做?

我需要得到這個相當緊迫的工作,所以任何幫助將不勝感激。 Thanx傢伙。

回答

1

如果您的數據庫有某種類型的前端(如phpMyAdmin或SQLyog),您可以將表格(或任何SELECT查詢的結果)導出爲CSV並在Excel中打開該表格。

評論後編輯: 我創建了一次XLS。這是一個有點不同,但我所做的就是把這個在我的PHP的頂部(產生任何輸出前):

header("Content-type: application/vnd.ms-excel"); 
header("Content-disposition: attachment; filename=\"name.xls\""); 

而在腳本的其餘部分我只是呼應了一個表(表, tr,td ...等) 該腳本的執行會給用戶下載。我認爲Content-disposition屬性有一些不同的選項(也許有一個讓腳本保存文件的選項)。

+0

它需要實現自動化,該腳本將通過cron作業每天在指定時間運行,我只需要弄清楚如何讓腳本保存生成的XLS數據到自動文件。 – Odyss3us 2010-03-22 09:02:42

+0

好的..在這種情況下,我的解決方案並不是那麼好..讓我編輯它.. – 2010-03-22 09:05:15

+0

在cron中,你也可以運行wget來保存php腳本的輸出。 – Krab 2010-03-22 09:13:06

0

如果我理解正確,您發佈的腳本工作正常,並創建一個正確的xls文件,並且您只想保存輸出,請查看http://php.net/manual/en/book.outcontrol.php。使用ob_get_clean(),您可以獲取創建的xls文件並將其寫入服務器的某個位置。也許你可以考慮其他選項,例如將數據保存爲Excel可以讀取的其他格式(.csv,可能它也可以讀取一些html/xml表格)。

+0

我用ob_get_clean()試過了,但沒有奏效,下面是我現在正在處理的代碼: – Odyss3us 2010-03-22 12:32:46

0

我嘗試了與ob_get_clean(),但它沒有工作,這裏是我一直在努力代碼:

<?php 

// DB TABLE Exporter 
// 
// How to use: 
// 
// Place this file in a safe place, edit the info just below here 
// browse to the file, enjoy! 

// CHANGE THIS STUFF FOR WHAT YOU NEED TO DO 

    $dbhost = "-"; 
    $dbuser = "-"; 
    $dbpass = "-"; 
    $dbname = "-"; 
    $dbtable = "-"; 

// END CHANGING STUFF 

$cdate = date("Y-m-d"); // get current date 


// first thing that we are going to do is make some functions for writing out 
// and excel file. These functions do some hex writing and to be honest I got 
// them from some where else but hey it works so I am not going to question it 
// just reuse 


// This one makes the beginning of the xls file 
function xlsBOF() { 
    $output = pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); 
    return; 
} 

// This one makes the end of the xls file 
function xlsEOF() { 
    $output .= pack("ss", 0x0A, 0x00); 
    return; 
} 

// this will write text in the cell you specify 
function xlsWriteLabel($Row, $Col, $Value) { 
    $L = strlen($Value); 
    $output .= pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); 
    $file = fopen("exported.xls","w"); 
    fwrite($file, "$output"); 
    fclose($file); 
    return; 
} 



// make the connection an DB query 
$dbc = mysql_connect($dbhost , $dbuser , $dbpass) or die(mysql_error()); 
mysql_select_db($dbname); 
$q = "SELECT * FROM ".$dbtable." WHERE date ='$cdate'"; 
$qr = mysql_query($q) or die(mysql_error()); 


// Ok now we are going to send some headers so that this 
// thing that we are going make comes out of browser 
// as an xls file. 
// 
//header("Pragma: public"); 
//header("Expires: 0"); 
//header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
//header("Content-Type: application/force-download"); 
//header("Content-Type: application/octet-stream"); 
//header("Content-Type: application/download"); 

//this line is important its makes the file name 
//header("Content-Disposition: attachment;filename=export_".$dbtable.".xls "); 

//header("Content-Transfer-Encoding: binary "); 

// start the file 
xlsBOF(); 

// these will be used for keeping things in order. 
$col = 0; 
$row = 0; 

// This tells us that we are on the first row 
$first = true; 

while($qrow = mysql_fetch_assoc($qr)) 
{ 
    // Ok we are on the first row 
    // lets make some headers of sorts 
    if($first) 
    { 
     foreach($qrow as $k => $v) 
     { 
      // take the key and make label 
      // make it uppper case and replace _ with ' ' 
      xlsWriteLabel($row, $col, strtoupper(ereg_replace("_" , " " , $k))); 
      $col++; 
     } 

     // prepare for the first real data row 
     $col = 0; 
     $row++; 
     $first = false; 
    } 

    // go through the data 
    foreach($qrow as $k => $v) 
    { 
     // write it out 
     xlsWriteLabel($row, $col, $v); 
     $col++; 
    } 

    // reset col and goto next row 
    $col = 0; 
    $row++; 

} 

xlsEOF(); 
exit(); 



?> 

我甚至不知道是否有任何這是有道理的,但我將fwrite添加到xlsBOF,xlsEOF和xlsWriteLabel函數中,試圖將數據寫入exported.xls文件,它可以像那樣工作嗎?

+0

不應該打開&co。在xlsEOF?每個單元格都調用xlsWriteLabel。 – Krab 2010-03-23 07:26:59

+0

我會放棄它,thanx。我會保持你的進展。 – Odyss3us 2010-03-23 19:49:09

1

我在項目中使用了很多PEAR Spreadsheet_Excel_Writer,效果很好。它生成Excel 5.0級別的文件,所以如果你需要比這更高級的anyhing,它可能不足以滿足你的目的,但它會生成本地.xls,而不僅僅是.csv僞裝成.xls。