ExcelServer.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: xiaofeng
  5. * Date: 2018/8/20
  6. * Time: 下午8:32
  7. */
  8. namespace common\api;
  9. class ExcelServer{
  10. public $objExcel;
  11. public function __construct()
  12. {
  13. $this->objExcel = new \PHPExcel();
  14. }
  15. //设置文件名称
  16. public function SetTitle($name)
  17. {
  18. $objActSheet = $this->objExcel->getActiveSheet();
  19. $objActSheet->setTitle($name);
  20. }
  21. //设置表头
  22. public function SheetIndex()
  23. {
  24. $this->objExcel->setActiveSheetIndex()
  25. ->setCellValue('A1',"编号")
  26. ->setCellValue('B1',"楼盘名称")
  27. ->setCellValue('C1',"区域");
  28. // return $this->objExcel;
  29. }
  30. //谁知表格内容 从第二行开始
  31. public function SheetText($data,$num = 2)
  32. {
  33. $i = $num;
  34. if(!empty($data))
  35. {
  36. foreach ($data as $key=>$val)
  37. {
  38. $this->objExcel->getActiveSheet()
  39. ->setCellValue('A'.$i,$val['id'])
  40. ->setCellValue('B'.$i,$val['name'])
  41. ->setCellValue('C'.$i,$val['city_name']);
  42. $i++;
  43. }
  44. }
  45. }
  46. public function Export($fileName)
  47. {
  48. // $objWriter = \PHPExcel_IOFactory::createWriter($this->objExcel, 'Excel2007');
  49. // $objWriter->save('house.xlsx');
  50. header('Content-Type:application/vnd.ms-excel');
  51. header('Content-Disposition:attachment;filename="'.$fileName.'"');
  52. header('Cache-Control:max-age=0');
  53. $objWriter =\PHPExcel_IOFactory::createWriter($this->objExcel, 'Excel5');
  54. $objWriter->save('php://output');
  55. exit;
  56. unset($objWriter);
  57. }
  58. }