2014-05-19 36 views
0

我在我的服務器中有一個名爲Admission.xls的文件 我想在該文件中寫入$ _POST數據&將它保存在服務器中,我不回到用戶,但它返回到用戶php從服務器返回excel文件,我不想

這裏是我的代碼

<?php 

// ----- begin of function library ----- 
// Excel begin of file header 
function xlsBOF() { 
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); 
    return; 
    } 
// Excel end of file footer 
function xlsEOF() { 
    echo pack("ss", 0x0A, 0x00); 
    return; 
} 
// Function to write a Number (double) into Row, Col 
function xlsWriteNumber($Row, $Col, $Value) { 
    echo pack("sssss", 0x203, 14, $Row, $Col, 0x0); 
    echo pack("d", $Value); 
    return; 
} 
// Function to write a label (text) into Row, Col 
function xlsWriteLabel($Row, $Col, $Value) { 
    $L = strlen($Value); 
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); 
    echo $Value; 
return; 
} 
// ----- end of function library ----- 

// ------ MS EXCEL START ------ 

header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT"); 
header ("Cache-Control: no-cache, must-revalidate");  
header ("Pragma: no-cache");  
header ('Content-type: application/x-msexcel'); 
header ("Content-Disposition: attachment; filename=Admission.xls"); 
header ("Content-Description: PHP/INTERBASE Generated Data"); 

// -----LOOP $_POST VALUES ----- 
$row = 2; 
$col = 1; 
xlsBOF(); 
foreach($_POST as $value) { 
    xlsWriteLabel($row, $col, $value); 
    $col++; 
} 
xlsEOF(); 

// ------ MS EXCEL END ----- 
?> 

如果可能的話,請告訴我怎麼打開,寫入和保存Excel文件在服務器 感謝您

+0

設置輸出緩衝,讀取輸出緩衝區給一個變量,並且這個變量寫的foreach後一個文件,不發送頭 –

回答

0

我認爲你可以使用PHPExcel,你將有很多的方法來實現這一任務。在foreach之前

Have look here

+0

郵政編碼或示例,不只是鏈接,因爲它可能會死了,你的答案變得毫無用處。 – Illidanek

+1

好的,我儘量不要再發布這樣的答案。 – Sandeep

0

你可以做這由帽子調整輸出緩衝區並將其寫入文件。

閱讀上此位置:http://www.php.net/manual/en/book.outcontrol.php


這你如何在你的腳本做到這一點。

刪除這些:

header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT"); 
header ("Cache-Control: no-cache, must-revalidate");  
header ("Pragma: no-cache");  
header ('Content-type: application/x-msexcel'); 
header ("Content-Disposition: attachment; filename=Admission.xls"); 
header ("Content-Description: PHP/INTERBASE Generated Data"); 

,改變你的foreach循環這樣的代碼:

// -----LOOP $_POST VALUES ----- 
ob_start(); 
$row = 2; 
$col = 1; 
xlsBOF(); 
foreach($_POST as $value) { 
    xlsWriteLabel($row, $col, $value); 
    $col++; 
} 
xlsEOF(); 
$xlsData = ob_get_contents(); 
ob_end_clean(); 

// -----WRITE EXCEL DATA TO FILE-- 
$h = @fopen('Admission.xls', 'w'); 
if ($h) { 
    fwrite($h, $xlsData); 
    fclose($h); 
} 

// ----- 
exit('Post data written to Admission.xls on server.'); 
+0

非常感謝凱恩,但是當我做它反覆在同一行替換,我只想追加/或添加在新的行 其實我正在做學校的在線登記表,學生的詳細信息應該保存在xls表,所以每個學生的數據應該保存在同一個文件中,但不能取代以前的學生數據 – Arjun

+0

爲什麼不將數據存儲在數據庫中? – Latheesan

+0

客戶要求excel表格 – Arjun