2011-05-02 126 views
0

我試圖從表格中導出一些不是全部的自定義標題的特定列,但是當我從服務器下載它時,它說文件已損壞,無法打開。我正在使用以下代碼將Mysql表格中的特定列導出到Excel

// Functions for export to excel. 
function xlsBOF() { 
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); 
    return; 
} 
function xlsEOF() { 
    echo pack("ss", 0x0A, 0x00); 
    return; 
} 
function xlsWriteNumber($Row, $Col, $Value) { 
    echo pack("sssss", 0x203, 14, $Row, $Col, 0x0); 
    echo pack("d", $Value); 
    return; 
} 


function xlsWriteLabel($Row, $Col, $Value) { 
    $L = strlen($Value); 
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); 
    echo $Value; 
    return; 
} 
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");; 
header("Content-Disposition: attachment;filename=orderlist.xls "); 
header("Content-Transfer-Encoding: binary "); 

xlsBOF(); 
xlsWriteLabel(0,5,"LIST OF CLOSED LEADS."); 

// Make column labels. (at line 3) 
xlsWriteLabel(2,0,"INQUIRY NO."); 
xlsWriteLabel(2,1,"POSTED DATE"); 
xlsWriteLabel(2,2,"AGENT NAME"); 
xlsWriteLabel(2,3,"STATUS"); 
xlsWriteLabel(2,4,"CLIENT NAME"); 
xlsWriteLabel(2,5,"UNIT NUMBER"); 
xlsWriteLabel(2,6,"PROPERTY TYPE"); 
xlsWriteLabel(2,7,"AREA"); 
xlsWriteLabel(2,8,"COMMISSION RECEIVED"); 
xlsWriteLabel(2,9,"BREAK UP DETAIL"); 
xlsWriteLabel(2,10,"DESCRIPTION"); 
xlsWriteLabel(2,11,"ADMIN COMMENT"); 

$xlsRow = 4; 

while($row=mysql_fetch_array($result)){ 

    $agent_query = "select Name from pf_agents where ID =".$row['agent_id']; 
    $agent_result=mysql_query("$agent_query"); 
    $agent_row=mysql_fetch_array($agent_result); 
    $agent_name = $agent_row['Name']; 

    $prop_query = "select Title from pf_property_types where ID =".$row['prop_type']; 
    $prop_result = mysql_query("$prop_query"); 
    $prop_row = mysql_fetch_array($prop_result); 
    $prop_name = $prop_row['Title']; 

    $area_query = "select Name from pf_master_dev where ID =".$row['Area']; 
    $area_result = mysql_query("$area_query"); 
    $area_row = mysql_fetch_array($area_result); 
    $area_name = $area_row['Name']; 

    $break_up = strip_word_html($row['reason_closed']); 
    $comment = strip_word_html($row['comment']); 
    $admin_comment = strip_word_html($row['comment_admin']); 
    $date_closed = date('d-m-Y', strtotime($row['updated_date'])); 

    xlsWriteLabel($xlsRow,0,$row['ID']); 
    xlsWriteLabel($xlsRow,1,$date_closed); 
    xlsWriteLabel($xlsRow,2,$agent_name); 
    xlsWriteLabel($xlsRow,3,$row['status']); 
    xlsWriteLabel($xlsRow,4,$row['name']); 
    xlsWriteLabel($xlsRow,5,$row['closed_unit']); 
    xlsWriteLabel($xlsRow,6,$prop_name); 
    xlsWriteLabel($xlsRow,7,$area_name); 
    xlsWriteLabel($xlsRow,8,$row['closed_amt']); 
    xlsWriteLabel($xlsRow,9,$break_up); 
    xlsWriteLabel($xlsRow,10,$comment); 
    xlsWriteLabel($xlsRow,11,$admin_comment); 

    $xlsRow++; 
} 
xlsEOF(); 

有沒有什麼辦法可以解決這個問題,或者我可以使用其他解決方案。

感謝

+0

失蹤xlsEOF() – 2011-05-02 11:32:41

+1

爲什麼從MySQL到Excel文件寫入數據的例子你設置了三個* Content-Type標題? – 2011-05-02 11:34:26

+0

@Mark我已經在我的文件中添加了xlsEOF(),我只顯示我的代碼的第一部分 – kakaajee 2011-05-02 12:02:31

回答

0

有很多PHP庫可以寫入Excel文件。盡我自己的PHPExcel或響應列出的庫之一this SO question

編輯

有使用PHPExcel here

+0

是的,可以使用這些,但必須獲得來自多個表的數據並顯示在特定的自定義列下,就像在我的代碼中,我提到了列名,然後以相同的順序提供了wile循環中的數據。有沒有更簡單的解決方案呢? – kakaajee 2011-05-02 12:28:35

+0

這是一個非常簡單的解決方案......它都歸結於您的SQL查詢。我懷疑你是否會找到比單個包含和約20行代碼更簡單的東西。 – 2011-05-02 12:30:52

+0

馬克你是對的,但原因是,我無法找到任何使用多列標題的例子。如果您可以指出或提供任何代碼,那將會非常有幫助。 – kakaajee 2011-05-02 13:03:07