PHPExcel 基本用法详解

我用的是PHPExcel-1.8大家可以到这里下载

下载地址:
地址

代码如下:

        require_once 'PHPExcel-1.8/Classes/PHPExcel.php';
        $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)
                ->setCellValue('A1', 'Name')
                ->setCellValue('B1', 'Username')
                ->setCellValue('C1', 'Last Visit Date')
                ->setCellValue('D1', 'RegisterDate')
                ->setCellValue('E1', 'ID');

        $BStyle = array(
            'borders' => array(
                'allborders' => array(
                    'style' => PHPExcel_Style_Border::BORDER_THIN
                )
            )
        );

        foreach(range('A','E') as $columnID) {
            $objPHPExcel->getActiveSheet()->getStyle($columnID.'1')->applyFromArray($BStyle);
        }
        //设置第一行颜色
        $objPHPExcel->getActiveSheet()->getStyle('A1:E1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB('8DB4E2');

        $key = 2;

        $data;//这里换成自己的数据

        foreach($data as $val){
            $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.$key, $val->name)
            ->setCellValue('B'.$key, $val->username)
            ->setCellValue('C'.$key, $val->lastvisitDate)
            ->setCellValue('D'.$key, $val->registerDate)
            ->setCellValue('E'.$key, $val->id);
            $key++;
        }

        //这里设置宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(40);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(40);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(40);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);
       //这里设置文件名字
        $fileName = "User Information".'-'.date('Y-m-d').'.xlsx';
        // Redirect output to a client’s web browser (Excel2007)
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $fileName . '"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        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
        ob_end_clean();
        $objWriter->save('php://output');
        exit;
标签:

发表评论