12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- /**
- * Created by PhpStorm.
- * User: xiaofeng
- * Date: 2018/8/20
- * Time: 下午8:32
- */
- namespace common\api;
- class ExcelServer{
- public $objExcel;
- public function __construct()
- {
- $this->objExcel = new \PHPExcel();
- }
- //设置文件名称
- public function SetTitle($name)
- {
- $objActSheet = $this->objExcel->getActiveSheet();
- $objActSheet->setTitle($name);
- }
- //设置表头
- public function SheetIndex()
- {
- $this->objExcel->setActiveSheetIndex()
- ->setCellValue('A1',"编号")
- ->setCellValue('B1',"楼盘名称")
- ->setCellValue('C1',"区域");
- // return $this->objExcel;
- }
- //谁知表格内容 从第二行开始
- public function SheetText($data,$num = 2)
- {
- $i = $num;
- if(!empty($data))
- {
- foreach ($data as $key=>$val)
- {
- $this->objExcel->getActiveSheet()
- ->setCellValue('A'.$i,$val['id'])
- ->setCellValue('B'.$i,$val['name'])
- ->setCellValue('C'.$i,$val['city_name']);
- $i++;
- }
- }
- }
- public function Export($fileName)
- {
- // $objWriter = \PHPExcel_IOFactory::createWriter($this->objExcel, 'Excel2007');
- // $objWriter->save('house.xlsx');
- header('Content-Type:application/vnd.ms-excel');
- header('Content-Disposition:attachment;filename="'.$fileName.'"');
- header('Cache-Control:max-age=0');
- $objWriter =\PHPExcel_IOFactory::createWriter($this->objExcel, 'Excel5');
- $objWriter->save('php://output');
- exit;
- unset($objWriter);
- }
- }
|