首先咱們須要去PHPExcel官網下載開源包php
下載好了之後只須要把裏面的classes文件夾解壓出來就能夠用了!nginx
固然,在處理的數據量龐大的時候就會報錯,提示你看nginx日誌什麼的,這個時候你須要檢查php.ini中的memory_limit,若是是128什麼的比較小的數字就改大一點,改爲512,就能夠解決了~sql
在ThinkPHP中,文件的命名空間仍是很重要的,我試着按照其餘的第三方命名寫namespace可是仍是不行,最後我是這麼作的數組
第一步:重命名classes爲Excel,而且將文件夾內的PHPExcel.php重命名成PHPExcel.class.php瀏覽器
而後放入到目錄文件緩存
而後在控制層導入,直接use的話是會報錯找不到類文件的。。。也許是內核裏面的路由問題,因而我是在實例化的時候寫的詳細地址竟然能夠出來了app
$objPHPExcel = new \Org\Excel\PHPExcel(); 就是這樣
可是這樣類文件的一些方法是不能夠用的,若是調用須要在方法前面加一個\
好比這樣:
$objSheet->getStyle("A1:Z1")->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('#00FF00');//給單元格填充背景顏色
以下是具體代碼:
//excel導出
public function excel(){
//查詢sql生成數組
$loan = M("loan");
$data = $loan->select();
$objPHPExcel = new \Org\Excel\PHPExcel();//實例化類文件 等同於在桌面新建一個excel表格
$objSheet = $objPHPExcel->getActiveSheet();//得到當前活動sheet操做對象
$objSheet->setTitle('loan_info');//給當前活動sheet修更名稱
$objSheet->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//設置單元格垂直居中、水平居中
$objSheet->getStyle("A1:Z1")->getFont()->setName("微軟雅黑")->setSize(10)->setBold(true);//設置單元格範圍的字體、字體大小、加粗
$objSheet->getStyle("A1:Z1")->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('#00FF00');//給單元格填充背景顏色
//添加邊框
$styleArray = array(
'borders' => array(
'outline' => array(
'style' => \PHPExcel_Style_Border::BORDER_THICK,
'color' => array('argb' => '#F0F8FF'),
),
),
);
$objSheet->getStyle("A1")->applyFromArray($styleArray);
$objSheet->getStyle("B1")->applyFromArray($styleArray);
$objSheet->getStyle("C1")->applyFromArray($styleArray);
$objSheet->getStyle("D1")->applyFromArray($styleArray);
$objSheet->getStyle("E1")->applyFromArray($styleArray);
$objSheet->getStyle("F1")->applyFromArray($styleArray);
$objSheet->getStyle("G1")->applyFromArray($styleArray);
$objSheet->getStyle("H1")->applyFromArray($styleArray);
$objSheet->getStyle("I1")->applyFromArray($styleArray);
$objSheet->getStyle("J1")->applyFromArray($styleArray);
$objSheet->getStyle("K1")->applyFromArray($styleArray);
$objSheet->getStyle("L1")->applyFromArray($styleArray);
$objSheet->getStyle("M1")->applyFromArray($styleArray);
$objSheet->getStyle("N1")->applyFromArray($styleArray);
$objSheet->getStyle("O1")->applyFromArray($styleArray);
$objSheet->getStyle("P1")->applyFromArray($styleArray);
$objSheet->getStyle("Q1")->applyFromArray($styleArray);
$objSheet->getStyle("R1")->applyFromArray($styleArray);
$objSheet->getStyle("S1")->applyFromArray($styleArray);
$objSheet->getStyle("T1")->applyFromArray($styleArray);
$objSheet->getStyle("U1")->applyFromArray($styleArray);
$objSheet->getStyle("V1")->applyFromArray($styleArray);
$objSheet->getStyle("W1")->applyFromArray($styleArray);
$objSheet->setCellValue("A1","ID")
->setCellValue("B1","公司名稱")
->setCellValue("C1","公司LOGO")
->setCellValue("D1","連接地址")
->setCellValue("E1","貸款對應人羣")
->setCellValue("F1","宣傳語")
->setCellValue("G1","最小額度範圍")
->setCellValue("H1","最大額度範圍")
->setCellValue("I1","最小期限範圍")
->setCellValue("J1","最大期限範圍")
->setCellValue("K1","利率")
->setCellValue("L1","最快放款時間")
->setCellValue("M1","計息方式")
->setCellValue("N1","是否定證")
->setCellValue("O1","申請條件")
->setCellValue("P1","申請流程")
->setCellValue("Q1","申請所需材料")
->setCellValue("R1","申請時間")
->setCellValue("S1","更新時間")
->setCellValue("T1","成功貸款人數")
->setCellValue("U1","按點擊收費金額")
->setCellValue("V1","是否上架")
->setCellValue("W1","顯示序號排列");//給單元格填充相應的值
//循環填充
$j = 2;
foreach($data as $key => $val){
$objSheet->setCellValue("A".$j,$val['id'])
->setCellValue("B".$j,$val['corporate'])
->setCellValue("C".$j,$val['img'])
->setCellValue("D".$j,$val['corporate_url'])
->setCellValue("E".$j,$val['person'])
->setCellValue("F".$j,$val['propaganda'])
->setCellValue("G".$j,$val['xquota'])
->setCellValue("H".$j,$val['dquota'])
->setCellValue("I".$j,$val['xterm'])
->setCellValue("J".$j,$val['dterm'])
->setCellValue("K".$j,$val['rate'])
->setCellValue("L".$j,$val['qtime'])
->setCellValue("M".$j,$val['type'])
->setCellValue("N".$j,$val['authentication'])
->setCellValue("O".$j,$val['condition'])
->setCellValue("P".$j,$val['process'])
->setCellValue("Q".$j,$val['science'])
->setCellValueExplicit("R".$j,$val['apply_time'],\PHPExcel_Cell_DataType::TYPE_STRING)//設定日期固定的爲數字合適不會成爲官方看不懂的格式
->setCellValueExplicit("S".$j,$val['up_time'],\PHPExcel_Cell_DataType::TYPE_STRING)//設定日期固定的爲數字合適不會成爲官方看不懂的格式
->setCellValue("T".$j,$val['succ_person'])
->setCellValue("U".$j,$val['click_money'])
->setCellValue("V".$j,$val['is_shelves'])
->setCellValue("W".$j,$val['show_number']);
$j++;
}
//設置文件保存的命名、編碼、以及開放保存路徑權限
$fn= "loan_".time().".xls";
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header("Content-Disposition: attachment;filename=$fn");//告訴瀏覽器將要輸出的名稱
header('Cache-Control: max-age=0');//禁止緩存
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');//生成excel文件
$objWriter->save('php://output');//完全開放保存路徑
exit;
}