您好,登錄后才能下訂單哦!
這篇文章給大家介紹使用ThinkPHP框架怎么導(dǎo)出excel數(shù)據(jù),內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
<?php header("Content-type: text/html; charset=utf-8"); class MesTestAction extends Action { //測(cè)試導(dǎo)出excel數(shù)據(jù) public function tpGetExcel() { //創(chuàng)建對(duì)象 import("ORG.Util.PHPExcel"); //從PHPExcel目錄導(dǎo)PHPExcel.php類(lèi)文件 $excel = new PHPExcel(); $data = M()->query('SELECT userid,username,stepgoal FROM tp_data_user LIMIT 2775'); //Excel表格式,這里簡(jiǎn)略寫(xiě)了3列 $letter = array('A','B','C'); //表頭數(shù)組 $tableheader = array('userid','用戶(hù)名','目標(biāo)步數(shù)'); $count= count($data);//總的數(shù)據(jù)行數(shù) $listNum = 500;//每個(gè)sheet頁(yè)最大數(shù)據(jù)行數(shù) $num = ceil($count/$listNum);//sheet頁(yè)個(gè)數(shù) $MuitData = array_chunk($data,$listNum,false);//分割總的數(shù)據(jù),每頁(yè)最多$listNum行有效數(shù)據(jù) //var_dump($MuitData);//die('as'); //缺省情況下,PHPExcel會(huì)自動(dòng)創(chuàng)建第一個(gè)SHEET,其索引SheetIndex=0 //設(shè)置 當(dāng)前處于活動(dòng)狀態(tài)的SHEET 為PHPExcel自動(dòng)創(chuàng)建的第一個(gè)SHEET $excel->setActiveSheetIndex(0); //objPHPExcel //設(shè)置sheet的title $excel->getActiveSheet()->setTitle('考核得分第'.'1'.'頁(yè)'); //設(shè)置sheet的列名稱(chēng) for($k = 0; $k < count($tableheader); ++$k) { $excel->getActiveSheet()->setCellValue("$letter[$k]".'1',"$tableheader[$k]");//第一行數(shù)據(jù) } //填充表格信息 處理第1塊數(shù)據(jù) $crrntSheetLineNo = count($MuitData[0]) + 1; for ( $j = 2; $j <= $crrntSheetLineNo; ++$j) { //遍歷每一行 $k = 0; foreach ( $MuitData[0][$j - 2] as $key => $value ) {//遍歷具體行的某一列 $excel->getActiveSheet()->setCellValue("$letter[$k]".$j,"$value");//第$k列 第$j行 $k++; } } //后續(xù)的sheet頁(yè)及數(shù)據(jù)塊 for ( $i = 1; $i <$num; ++$i) { //創(chuàng)建第$i個(gè)sheet $msgWorkSheet = new PHPExcel_Worksheet($excel, '考核得分第'.($i + 1).'頁(yè)'); //創(chuàng)建一個(gè)工作表 $excel->addSheet($msgWorkSheet); //插入工作表 $excel->setActiveSheetIndex($i); //切換到新創(chuàng)建的工作表 //設(shè)置sheet的列名稱(chēng) for($k = 0; $k < count($tableheader); ++$k) { $excel->getActiveSheet()->setCellValue("$letter[$k]1","$tableheader[$k]");//第一行數(shù)據(jù) } //填充表格信息 處理第$i塊數(shù)據(jù) $crrntSheetLineNo = count($MuitData[$i]) + 1; //var_dump($crrntSheetLineNo);var_dump($MuitData[$i-1]);die('as'); for ( $j = 2; $j <= $crrntSheetLineNo; ++$j) { //遍歷每一行 $k = 0; foreach ( $MuitData[$i-1][$j - 2] as $key => $value ) {//遍歷具體行的某一列 $excel->getActiveSheet()->setCellValue("$letter[$k]$j","$value");//第$k列 第$j行 ++$k; } } usleep(100); } //創(chuàng)建Excel輸出對(duì)象 $filename = "大獎(jiǎng)賽培訓(xùn)考核得分.xls"; $write = new PHPExcel_Writer_Excel5($excel); ob_end_clean();//清除緩沖區(qū),避免亂碼 /* //輸出到本地 $write->save( iconv('utf-8', 'gbk', $filename) ); */ //輸出到瀏覽器 header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-execl"); header("Content-Type:application/download"); header('Content-Type:application/octet-stream'); $encoded_filename = urlencode($filename); $encoded_filename = str_replace("+", "%20", $encoded_filename); $ua = $_SERVER["HTTP_USER_AGENT"]; if (preg_match("/MSIE/", $ua)) { header('Content-Disposition: attachment; filename="' . $encoded_filename . '"'); } else if (preg_match("/Firefox/", $ua)) { header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"'); } else { header('Content-Disposition: attachment; filename="' . $filename . '"'); } header("Content-Transfer-Encoding:binary"); $write->save('php://output'); } } ?>
關(guān)于使用ThinkPHP框架怎么導(dǎo)出excel數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。