PHP導(dǎo)出Excel實(shí)例講解
本次實(shí)現(xiàn)PHP導(dǎo)出Excel文件使用的是PHP開源程序PHPExcel,部分關(guān)鍵代碼分享給大家,具體內(nèi)容如下
<?php error_reporting(E_ALL); date_default_timezone_set('Asia/Shanghai'); require_once './Classes/PHPExcel.php'; $data=array( 0=>array( 'id'=>1001, 'username'=>'張飛', 'password'=>'123456', 'address'=>'三國(guó)時(shí)高老莊250巷101室' ), 1=>array( 'id'=>1002, 'username'=>'關(guān)羽', 'password'=>'123456', 'address'=>'三國(guó)時(shí)花果山' ), 2=>array( 'id'=>1003, 'username'=>'曹操', 'password'=>'123456', 'address'=>'延安西路2055弄3號(hào)' ), 3=>array( 'id'=>1004, 'username'=>'劉備', 'password'=>'654321', 'address'=>'愚園路188號(hào)3309室' ) ); $objPHPExcel=new PHPExcel(); $objPHPExcel->getProperties()->setCreator('http://www.dbjr.com.cn') ->setLastModifiedBy('http://www.dbjr.com.cn') ->setTitle('Office 2007 XLSX Document') ->setSubject('Office 2007 XLSX Document') ->setDescription('Document for Office 2007 XLSX, generated using PHP classes.') ->setKeywords('office 2007 openxml php') ->setCategory('Result file'); $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A1','ID') ->setCellValue('B1','用戶名') ->setCellValue('C1','密碼') ->setCellValue('D1','地址'); $i=2; foreach($data as $k=>$v){ $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A'.$i,$v['id']) ->setCellValue('B'.$i,$v['username']) ->setCellValue('C'.$i,$v['password']) ->setCellValue('D'.$i,$v['address']); $i++; } $objPHPExcel->getActiveSheet()->setTitle('三年級(jí)2班'); $objPHPExcel->setActiveSheetIndex(0); $filename=urlencode('學(xué)生信息統(tǒng)計(jì)表').'_'.date('Y-m-dHis'); //生成xlsx文件 /* header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"'); header('Cache-Control: max-age=0'); $objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007'); */ //生成xls文件 header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="'.$filename.'.xls"'); header('Cache-Control: max-age=0'); $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit;
注意:如果導(dǎo)出中文時(shí)出現(xiàn)亂碼,可以嘗試將字符串轉(zhuǎn)換成gb2312,代碼類似如下:
查看代碼打印
$str=mb_convert_encoding("gb2312","UTF-8",$str);
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
IIS+fastcgi下PHP運(yùn)行超時(shí)問(wèn)題的解決辦法詳解
本篇文章是對(duì)IIS+fastcgi下PHP運(yùn)行超時(shí)問(wèn)題的解決辦法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP中使用addslashes函數(shù)轉(zhuǎn)義的安全性原理分析
這篇文章主要介紹了PHP中使用addslashes函數(shù)轉(zhuǎn)義的安全性原理分析,較為深入的分析了addslashes函數(shù)的用法及ecshop自定義函數(shù)addslashes_deep的不足之處,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-11-11php cookie名使用點(diǎn)號(hào)(句號(hào))會(huì)被轉(zhuǎn)換
php cookie名不能使用點(diǎn)號(hào)(句號(hào)),應(yīng)該說(shuō)可以使用點(diǎn)號(hào)的cookie名,但會(huì)被轉(zhuǎn)換,要知道為什么,祥看本文2014-10-10php中foreach結(jié)合curl實(shí)現(xiàn)多線程的方法分析
這篇文章主要介紹了php中foreach結(jié)合curl實(shí)現(xiàn)多線程的方法,結(jié)合實(shí)例形式分析了foreach語(yǔ)句結(jié)合curl循環(huán)調(diào)用模擬多線程的原理與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-09-09最新用php獲取谷歌PR值算法,附上php查詢PR值代碼示例
用php程序怎么獲取谷歌PR值,已經(jīng)有好幾個(gè)人問(wèn)我php查詢PR值怎么實(shí)現(xiàn)的,于是現(xiàn)在就把php查詢PR值法算法附上,大家可以直接使用下面的代碼去測(cè)試php查詢PR值2011-12-12PHP使用標(biāo)準(zhǔn)庫(kù)spl實(shí)現(xiàn)的觀察者模式示例
這篇文章主要介紹了PHP使用標(biāo)準(zhǔn)庫(kù)spl實(shí)現(xiàn)的觀察者模式,結(jié)合實(shí)例形式分析了php基于spl標(biāo)準(zhǔn)庫(kù)的觀察者模式相關(guān)實(shí)現(xiàn)與使用操作技巧,需要的朋友可以參考下2018-08-08利用PHP實(shí)現(xiàn)開心消消樂(lè)的算法示例
開心消消樂(lè)應(yīng)該對(duì)大家來(lái)說(shuō)都不陌生吧,下面這篇文章主要給大家介紹了關(guān)于如何利用PHP實(shí)現(xiàn)開心消消樂(lè)算法的相關(guān)資料,文中將需求和示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10