|
- <?php
- /**
- * Created by PhpStorm.
- * User: wangxj
- * Date: 2017/5/31
- * Time: 10:50
- */
-
- /**
- * @property string $file_name
- */
-
- namespace common\components;
- require_once '../../backend/common/PHPExcel/PHPExcel.php';
-
- class zPhpExcel extends \PHPExcel
- {
- public $file_name;
-
- //输出到浏览器
- public function output()
- {
- $objWrite = \PHPExcel_IOFactory::createWriter($this, 'Excel5'); //按照指定格式生成excel文件
- $this->browserExport('07', $this->file_name . '.xls');
- $objWrite->save('php://output');
- }
-
- //head头
- private function browserExport($type, $fileName)
- {
- if ($type == '05') { //输出xls文件
- header('Content-Type: application/vnd.ms-excel');
- } else { //输出xlsx文件
- header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
- }
- header('Content-Disposition: attachment;filename="' . $fileName . '"');
- header('Cache-Control: max-age=0');//禁止缓存
- }
-
- //设置自动宽度,中文支持的不好,基本不能用
- public function setAutoSize($range)
- {
- foreach ($range as $columnID) {
- $this->getActiveSheet()->getColumnDimension($columnID)
- ->setAutoSize(true);
- }
- }
-
- //手动设置宽度 一个中文对应3 “出车日期” 4*3 12
- public function setColumnSize($range, $width)
- {
- foreach ($range as $key => $columnID) {
- $this->getActiveSheet()->getColumnDimension($columnID)
- ->setWidth($width[$key]);
- }
- }
- }
|