欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php 自定義函數(shù)實(shí)現(xiàn)將數(shù)據(jù) 以excel 表格形式導(dǎo)出示例

 更新時(shí)間:2019年11月13日 09:36:06   作者:傾聽歲月  
這篇文章主要介紹了php 自定義函數(shù)實(shí)現(xiàn)將數(shù)據(jù) 以excel 表格形式導(dǎo)出,結(jié)合實(shí)例形式分析了PHP操作二維數(shù)組的遍歷與Excel格式輸出相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了php 自定義函數(shù)實(shí)現(xiàn)將數(shù)據(jù) 以excel 表格形式導(dǎo)出。分享給大家供大家參考,具體如下:

/**
* 導(dǎo)出數(shù)據(jù)為excel表格
* @param
* array $data  一個(gè)二維數(shù)組,結(jié)構(gòu)如同從數(shù)據(jù)庫查出來的數(shù)組
* array $title  excel的第一行標(biāo)題,一個(gè)數(shù)組,如果為空則沒有標(biāo)題
* String $filename 下載的文件名
*/
function exportexcel($data=array(),$title=array(),$filename='report'){
    header("Content-type:application/octet-stream");
    header("Accept-Ranges:bytes");
    header("Content-type:application/vnd.ms-excel");
    header("Content-Disposition:attachment;filename=".$filename.".xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    //導(dǎo)出xls 開始
    if (!empty($title)){
      foreach ($title as $k => $v) {
        $title[$k]=iconv("UTF-8", "GB2312",$v);
      }
      $title= implode("\t", $title);
      echo "$title\n";
    }
    if (!empty($data)){
      foreach($data as $key=>$val){
        foreach ($val as $ck => $cv) {
          $data[$key][$ck]=mb_convert_encoding($cv,"GB2312","UTF-8");
        }
        $data[$key]=implode("\t", $data[$key]);
      }
      echo implode("\n",$data);
    }
}

php內(nèi)置函數(shù)講解

String mb_convert_encoding( $str, $encoding1,$encoding2 )

$str,要轉(zhuǎn)換編碼的字符串
$encoding1,目標(biāo)編碼,如utf-8,gbk,大小寫均可
$encoding2,原編碼,如utf-8,gbk,大小寫均可

demo

$title = array("title1","title2","title3");
$data = array("數(shù)據(jù)1","數(shù)據(jù)2","數(shù)據(jù)3");
$fileName = "demo";
exportexcel($data,$title,$fileName);

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php正則表達(dá)式用法總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論