php匯出中文內容excel檔案類執行個體_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php匯出中文內容excel檔案類。分享給大家供大家參考。具體如下:

<?php class toExcel{  public $link = null;  function __construct(){  }   /***************************************************************************  * $mapping:數組格式頭資訊$map=array('No','Name','Email','Age');  * $datalist:資料庫查出來的結果集  * $fileName:Excel檔案名稱  * return:Excel格式檔案  **************************************************************************/  public function toExcel($mapping,$datalist,$fileName) {   header("Content-type:application/vnd.ms-excel");   header("Content-Disposition:filename=".iconv('utf-8', 'gb2312', $fileName).".xls");   echo'<html xmlns:o="urn:schemas-microsoft-com:office:office"   xmlns:x="urn:schemas-microsoft-com:office:excel"   xmlns="[url=http://www.w3.org/TR/REC-html40]http://www.w3.org/TR/REC-html40[/url]">   <head>   <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">   <meta http-equiv=Content-Type content="text/html; charset=UTF-8">   <!--[if gte mso 9]><xml>   <x:ExcelWorkbook>   <x:ExcelWorksheets>   <x:ExcelWorksheet>   <x:Name></x:Name>   <x:WorksheetOptions>   <x:DisplayGridlines/>   </x:WorksheetOptions>   </x:ExcelWorksheet>   </x:ExcelWorksheets>   </x:ExcelWorkbook>   </xml><![endif]-->   </head>   <body link=blue vlink=purple leftmargin=0 topmargin=0>';   echo'<table border="0" cellspacing="0" cellpadding="0">';   echo'<tr>';   if(is_array($mapping)) {    foreach($mapping as $key=>$val)    echo"<td style='background-color:#09F;font-weight:bold;'>".$val."</td>";   }   echo'</tr>';   foreach($datalist as $k=>$v){    echo'<tr>';    foreach($v as $key=>$val){     if(is_numeric($val) && strlen($val)>=14){      echo"<td style='vnd.ms-excel.numberformat:@'>".$val."</td>"; //大於14位的數字轉換成字串輸出(如身份證)     }else{      echo"<td>".$val."</td>";     }    }    echo'</tr>';   }   echo'</table>';   echo'</body>';   echo'</html>';  } } $map=array('No','Name','Email'); $datal=array(array(1, '管理員', 'admin@163.com'), array(2, 'member', 'member@163.com'));; $csv=new toExcel; $csv->toExcel($map,$datal,"dataexport"); ?> 

方法二

<?php header("Content-Type: application/vnd.ms-execl"); header("Content-Disposition: attachment; filename=myExcel.xls"); header("Pragma: no-cache"); header("Expires: 0"); /*first line*/ $data1= "中文測試"; $data1=mb_convert_encoding($data1,"GB2312","UTF-8"); echo $data1."\t"; echo "world"."\t"; echo "\t\n"; /*start of second line*/ echo "this is second line"."\t"; echo "Hi,pretty girl"."\t"; echo "\t\n"; ?>

希望本文所述對大家的php程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.