phpExcel实现excel文件导出

1. 下载PHPExcel的SDK,下载地址为:https://github.com/PHPOffice/PHPExcel,将下载的文件解压后,在自己项目common目录下新建phpexcel目录,将Classes目录下的文件拷贝到phpexcel下。

2. 导出excel文件,代码如下

        // 引入PHPExcel文件
        include_once ROOT_PATH.'/app/common/phpexcel/PHPExcel.php';
        //实例化并配置PHPExcel
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
            ->setLastModifiedBy("Maarten Balliauw")
            ->setTitle("Office 2007 XLSX Test Document")
            ->setSubject("Office 2007 XLSX Test Document")
            ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("Test result file");

        //为表格添加表头
        $objPHPExcel->setActiveSheetIndex(0)

            //Excel的第A列,uid是你查出数组的键值,下面以此类推
            ->setCellValue('A1',"活动ID")
            ->setCellValue('B1',"客户名称")
            ->setCellValue('C1',"交易号")
            ->setCellValue('D1',"金额")
            ->setCellValue('E1',"支付时间")
            ->setCellValue('F1',"活动名称")
            ->setCellValue('G1',"活动时间");

        /*以下就是对处理Excel里的数据, 横着取数据,主要是这一步,其他基本都不要改*/
        foreach($data as $k => $v)
        {
            $num=$k+2;
            $objPHPExcel->setActiveSheetIndex(0)
                ->setCellValue('A'.$num, $v->liveId)
                ->setCellValue('B'.$num, $v->nickName)
                ->setCellValue('C'.$num, "\t" . $v->out_trade_no . "\t")
                ->setCellValue('D'.$num, round($v->payedmoney/100,2))
                ->setCellValue('E'.$num, $v->payedTime)
                ->setCellValue('F'.$num, $v->locationTime);
        }
        
        // 为表格设置表格名称
        $objPHPExcel->getActiveSheet()->setTitle('User');

        // 设置表格大小自适应
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);

        $objPHPExcel->setActiveSheetIndex(0);
        
        //通过浏览器下载表格
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$name.'.xls"');
        header('Cache-Control: max-age=0');
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;

 

你可能感兴趣的:(PHP,Excel)