zoukankan      html  css  js  c++  java
  • PHP实现导出Excel文件

    实现代码:

    private function exportExcel($params)
        {
    
            $arr = $this->getExportData($params);   // 获取需要导出的信息(我这边是:二维数组)
    
    
            $fileName = "5月文化衫信息采集表";
            $xlsTitle = iconv('utf-8', 'gbk', "5月文化衫信息采集表");  //注意编码转换
            require LIB_PATH . "application/library/PHPExcel/PHPExcel.php";  
    
            $objPHPExcel = new PHPExcel();
            $objSheet = $objPHPExcel->getActiveSheet(); // 获取当前活动sheet
            $objSheet->setTitle('5月文化衫信息采集表'); // 给当前的活动sheet起个名称
    
            $objSheet->setCellValue('A1', '员工名');
            $objSheet->setCellValue('B1', '工号');
    
            $objSheet->setCellValue('C1', '一级部门');
            $objSheet->setCellValue('D1', '二级部门');
    
            $objSheet->setCellValue('E1', '衣服款式');
            $objSheet->setCellValue('F1', '尺码');
    
    
            $i = 2;
            foreach ($arr as $key => $val) {
                switch ($val['clothes_style']) {
                    case 1:
                        $val['clothes_style'] = '款式A';
                        break;
                    case 2:
                        $val['clothes_style'] = '款式B';
                        break;
    
                }
                switch ($val['clothes_size']) {
                    case 1:
                        $val['clothes_size'] = 'XS';
                        break;
                    case 2:
                        $val['clothes_size'] = 'S';
                        break;
                    case 3:
                        $val['clothes_size'] = 'M';
                        break;
                    case 4:
                        $val['clothes_size'] = 'L';
                        break;
                    case 5:
                        $val['clothes_size'] = 'XL';
                        break;
                    case 6:
                        $val['clothes_size'] = 'XXL';
                        break;
                    default:
                        $val['clothes_size'] = 'F';
                        break;
                }
    
    
                $objSheet->setCellValue('A' . $i, $val['user_id'])
                    ->setCellValue('B' . $i, "4399XM-" . $val['empno'])
                    ->setCellValue('C' . $i, $val['dept_id'])
                    ->setCellValue('D' . $i, $val['job_id'])
                    ->setCellValue('E' . $i, $val['clothes_style'])
                    ->setCellValue('F' . $i, $val['clothes_size']);
                $i++;
            }
    
    
            header('pragma:public');
            header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xlsx"');
            header("Content-Disposition:attachment;filename=$fileName.xlsx");
            header('Cache-Control: max-age=0');
            header("Content-Type: application/force-download");
            header("Content-Type: application/octet-stream");
            header("Content-Type: application/download");
    //        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');
            exit;
        }    
    
  • 相关阅读:
    bug的约束
    bug管理规范
    FTP小教程
    测试需求,产品需求,项目需求
    如何理解客户需求、市场需求、产品需求、业务需求、特性、功能需求 ?(转)
    什么是测试需求?(转)
    绑定与非绑定,反射,内置方法
    面向对象之组合、封装、多态、property装饰器
    面向对象之继承与派生,属性查找
    面向对象基础
  • 原文地址:https://www.cnblogs.com/xinxinmifan/p/php-export-excel.html
Copyright © 2011-2022 走看看