新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用PHP怎么導(dǎo)出excel數(shù)據(jù)-創(chuàng)新互聯(lián)
這篇文章將為大家詳細講解有關(guān)使用PHP怎么導(dǎo)出excel數(shù)據(jù),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
class Excel_XML { //定于私有變量,頂部標(biāo)簽 private $header = "\n"; //底部標(biāo)簽 private $footer = " "; //定于行數(shù)組 private $lines = array(); //設(shè)置編碼 private $sEncoding; //設(shè)置類型 private $bConvertTypes; //設(shè)置sheet名稱 private $sWorksheetTitle; //構(gòu)造函數(shù) public function __construct( $sEncoding = 'UTF-8',$bConvertTypes = false,$sWorksheetTitle = 'Table1') { $this->bConvertTypes = $bConvertTypes; $this->setEncoding($sEncoding); $this->setWorksheetTitle($sWorksheetTitle); } //設(shè)置編碼,在構(gòu)造函數(shù)里面默認的事UTF-8格式 public function setEncoding($sEncoding) { $this->sEncoding = $sEncoding; } //設(shè)置excel的頭 public function setWorksheetTitle ($title) { $title = preg_replace ("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title); $title = substr ($title, 0, 31); $this->sWorksheetTitle = $title; } //增加行函數(shù)(關(guān)鍵函數(shù)) private function addRow ($array) { $cells = ""; //設(shè)置每個單元為空 foreach ($array as $k => $v) { $type = 'String'; //默認類型是字符串 if ($this->bConvertTypes === true && is_numeric($v)): //判斷類型 { $type = 'Number'; } $v = htmlentities($v, ENT_COMPAT, $this->sEncoding); $cells .= "" . $v . " | \n"; } $this->lines[] = "\n" . $cells . "
\n"; //寫入數(shù)組 } //增加數(shù)組 public function addArray ($array) { foreach ($array as $k => $v) {$this->addRow ($v);} } //導(dǎo)出xml public function generateXML ($filename = 'excel-export') { $filename = preg_replace('/[^aA-zZ0-9\_\-]/', '', $filename); header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding); header("Content-Disposition: inline; filename=\"" . $filename . ".xls\""); echo stripslashes (sprintf($this->header, $this->sEncoding)); echo "\nsWorksheetTitle . "\">\n \n"; echo $this->footer; } }\n"; foreach ($this->lines as $line) echo $line; echo "
\n
原理很簡單,就是把數(shù)據(jù)數(shù)組,讀出來,再用XML的標(biāo)簽封上,在用php自帶的header()函數(shù)告訴游覽器,就可以了。
調(diào)用:
public function import() { $data = array( 1 => array ('學(xué)校名稱',"隊伍名稱") ); foreach($this->team as $key=>$value) { array_push($data,array($key, $value)); } $xls = new Excel_XML('UTF-8', false, 'My Test Sheet'); //實例化函數(shù) $xls->addArray($data); $xls->generateXML('school'); //導(dǎo)出并設(shè)置名稱 }
關(guān)于使用PHP怎么導(dǎo)出excel數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
當(dāng)前名稱:使用PHP怎么導(dǎo)出excel數(shù)據(jù)-創(chuàng)新互聯(lián)
本文路徑:http://fisionsoft.com.cn/article/dhcdic.html