這篇文章將為大家詳細講解有關使用PHP怎么導出excel數(shù)據(jù),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
在堯都等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供成都做網(wǎng)站、成都網(wǎng)站設計 網(wǎng)站設計制作定制網(wǎng)站開發(fā),公司網(wǎng)站建設,企業(yè)網(wǎng)站建設,高端網(wǎng)站設計,成都全網(wǎng)營銷,成都外貿(mào)網(wǎng)站建設公司,堯都網(wǎng)站建設費用合理。class Excel_XML { //定于私有變量,頂部標簽 private $header = "<?xml version=\"1.0\" encoding=\"%s\"?\>\n<Workbook xmlns=\"urn:schemas-microsoft-com: office:spreadsheet\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com: office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">"; //底部標簽 private $footer = "</Workbook>"; //定于行數(shù)組 private $lines = array(); //設置編碼 private $sEncoding; //設置類型 private $bConvertTypes; //設置sheet名稱 private $sWorksheetTitle; //構造函數(shù) public function __construct( $sEncoding = 'UTF-8',$bConvertTypes = false,$sWorksheetTitle = 'Table1') { $this->bConvertTypes = $bConvertTypes; $this->setEncoding($sEncoding); $this->setWorksheetTitle($sWorksheetTitle); } //設置編碼,在構造函數(shù)里面默認的事UTF-8格式 public function setEncoding($sEncoding) { $this->sEncoding = $sEncoding; } //設置excel的頭 public function setWorksheetTitle ($title) { $title = preg_replace ("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title); $title = substr ($title, 0, 31); $this->sWorksheetTitle = $title; } //增加行函數(shù)(關鍵函數(shù)) private function addRow ($array) { $cells = ""; //設置每個單元為空 foreach ($array as $k => $v) { $type = 'String'; //默認類型是字符串 if ($this->bConvertTypes === true && is_numeric($v)): //判斷類型 { $type = 'Number'; } $v = htmlentities($v, ENT_COMPAT, $this->sEncoding); $cells .= "<Cell><Data ss:Type=\"$type\">" . $v . "</Data></Cell>\n"; } $this->lines[] = "<Row>\n" . $cells . "</Row>\n"; //寫入數(shù)組 } //增加數(shù)組 public function addArray ($array) { foreach ($array as $k => $v) {$this->addRow ($v);} } //導出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 "\n<Worksheet ss:Name=\"" . $this->sWorksheetTitle . "\">\n<Table>\n"; foreach ($this->lines as $line) echo $line; echo "</Table>\n</Worksheet>\n"; echo $this->footer; } }
原理很簡單,就是把數(shù)據(jù)數(shù)組,讀出來,再用XML的標簽封上,在用php自帶的header()函數(shù)告訴游覽器,就可以了。
調(diào)用:
public function import() { $data = array( 1 => array ('學校名稱',"隊伍名稱") ); 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'); //導出并設置名稱 }
關于使用PHP怎么導出excel數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
當前名稱:使用PHP怎么導出excel數(shù)據(jù)-創(chuàng)新互聯(lián)
本文地址:http://jinyejixie.com/article2/dhcdic.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導航、App設計、網(wǎng)站維護、網(wǎng)站設計公司、做網(wǎng)站、營銷型網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)