兩款php匯出excel執行個體

來源:互聯網
上載者:User
 代碼如下 複製代碼

<?php
header("content-type:application/vnd.ms-excel; charset=gbk");
    header("content-disposition:attachment;filename=test_data.xls");


$link=mysql教程_connect('localhost','root','hhhkkk');
if($link){

    mysql_select_db('dataui',$link);
    mysql_query("set names 'gbk'");
    echo "資料庫教程串連已經成功!";
}else{
     echo "資料庫連接失敗!";
}

echo "項目名稱"." ";
echo "項目詳情"." ";
$sql="select * from php168_item_content where fid='11'";
$query=mysql_query($sql);
while($rs=mysql_fetch_array($query)){
          echo $rs[title]." ";
     $sql2="select * from php168_item_content_1 where id='$rs[id]'";
   $query2=mysql_query($sql2);
   while($rs2=mysql_fetch_array($query2)){
   echo $rs2[content]." ";
   echo " ";
     
   }
                       
}


?>

匯出excel時,如果某列匯出的是社會安全號碼的話,開啟excel檔案以後會發現,社會安全號碼自動採用科學計數法,無論如何修改該列屬性,都無法實現自己的要求。網上有人說,先把該列屬性改為文本以後,再輸入就沒有問題,實際操作excel確實如此,但是,php程式匯出就無法做到了

 

 代碼如下 複製代碼

<?php
        // 實驗資料,實際作業中,這裡應該是從資料庫取得資料
        $emps教程[0]['id'] = '00001';
        $emps[0]['name'] = 'abc';
        $emps[0]['sexual'] = '男';
        $emps[0]['age'] = 28;

        $emps[1]['id'] = '00002';
        $emps[1]['name'] = 'bbc';
        $emps[1]['sexual'] = '男';
        $emps[1]['age'] = 23;

        $emps[2]['id'] = '00003';
        $emps[2]['name'] = 'cba';
        $emps[2]['sexual'] = '女';
        $emps[2]['age'] = 20;
     
        ini_set('include_path', '/data/website/htdocs/includes');
        require_once('smarty.php');
        $smarty = new smarty();

        $smarty->assign('emps', $emps);

        // 輸出檔案頭,表明是要輸出 excel 檔案
        header("content-type: application/vnd.ms-excel");
        header("content-disposition: attachment; filename=test.xls");
        $smarty->display('excel-xml.tpl');
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.