- <?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 "數(shù)據(jù)庫連接已經(jīng)成功!";
- }else{
- echo "數(shù)據(jù)庫連接失敗!";
- }
- echo "項(xiàng)目名稱"."t";
- echo "項(xiàng)目詳情"."t";
- $sql="select * from php168_item_content where fid='11'";
- $query=mysql_query($sql);
- while($rs=mysql_fetch_array($query)){
- echo $rs[title]."t";
- $sql2="select * from php168_item_content_1 where id='$rs[id]'";
- $query2=mysql_query($sql2);
- while($rs2=mysql_fetch_array($query2)){
- echo $rs2[content]."t";
- echo "n";
- }
- }
- ?>
導(dǎo)出excel時,如果某列導(dǎo)出的是身份證號的話,打開excel文件以后會發(fā)現(xiàn),身份證號自動采用科學(xué)計(jì)數(shù)法,無論如何修改該列屬性,都無法實(shí)現(xiàn)自己的要求。網(wǎng)上有人說,先把該列屬性改為文本以后,再輸入就沒有問題,實(shí)際操作excel確實(shí)如此,但是,php程序?qū)С鼍蜔o法做到了。
- <?php
- // 實(shí)驗(yàn)資料,實(shí)際作業(yè)中,這里應(yīng)該是從數(shù)據(jù)庫取得資料
- $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');
- ?>
新聞熱點(diǎn)
疑難解答