?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類文件
$excel = new PHPExcel();
$data = M()->query('SELECT userid,username,stepgoal FROM tp_data_user LIMIT 2775');
//Excel表格式,這里簡(jiǎn)略寫了3列
$letter = array('A','B','C');
//表頭數(shù)組
$tableheader = array('userid','用戶名','目標(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的列名稱
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的列名稱
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相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。