phpExcel导出到Excel和pdf

来源:互联网 发布:淘宝开业牌匾 编辑:程序博客网 时间:2024/05/01 15:40

phpExcel的下载地址:

下载地址 (http://phpexcel.codeplex.com/releases/view/45412


一、导出到Excel

      require_once ("/lib/PHPExcel.php"); 
        //创建一个excel  
        $objPHPExcel = new PHPExcel();
        
        $objectSheet = $objPHPExcel -> getActiveSheet();
        $objectSheet -> setTitle("测试Excel");

       //$objectSheet ->setCellValueByColumnAndRow(1,1,“ddddddd”)这个用起来比较方便        (参数:$column,$row,$value

        $objectSheet -> setCellValue("A1","ddddddd");
        $objectSheet -> setCellValue("A2",26);
        $objectSheet -> setCellValue("A3",true);
        $objectSheet -> setCellValue("A4",'=SUM(A2:A2)');
        
        $objectSheet -> setCellValue("B1","ddddddd");
        $objectSheet -> setCellValue("B2","aaaaaaa");
        $objectSheet -> setCellValue("B3","ccccccccc");
        $objectSheet -> setCellValue("B4","忠厚仁义的放空间的立法的浪费");
        
        //显示指定内容的类型
        $objectSheet -> setCellValueExplicit('A5','444444444444444445',PHPExcel_Cell_DataType::TYPE_STRING);
        //设置宽度
        $objectSheet -> getColumnDimension('A')->setWidth(20);
        $objectSheet -> getColumnDimension('B')->setWidth(40);
        
         //合并单元格
        $objectSheet -> mergeCells("A6:B6");
        $objectSheet -> mergeCells("A7:B7");
        $objectSheet -> mergeCells("A6:A7");
        
         //分离单元格
         $objectSheet -> unmergeCells('A6:A7');
         
         //添加图片
        $objDrawing = new PHPExcel_Worksheet_Drawing();
        $objDrawing -> setName('搜索');
        $objDrawing -> setPath('/search_btn.png');
        $objDrawing -> setHeight(18);
        $objDrawing -> setCoordinates('D7');
        $objDrawing -> setOffsetX(10);
        $objDrawing -> setRotation(15);
        $objDrawing -> setWorksheet($objectSheet);
        
    //    $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);  
          
        //输出到硬盘
        //$objWriter->save("D://xxx.xls");  
        
        
        //输出到浏览器  
        $outputFileName = "x1.xls";
        header("Content-Type: application/force-download");  
        header("Content-Type: application/octet-stream");  
        header("Content-Type: application/download");  
        header('Content-Disposition:inline;filename="'.$outputFileName.'"');  
        header("Content-Transfer-Encoding: binary");  
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");  
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");  
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");  
        header("Pragma: no-cache");  
        $objWriter->save('php://output'); 


二、导出到pdf

      require_once ("/lib/PHPExcel.php");
        
        $objPHPExcel = new PHPExcel();
        
        $objectSheet = $objPHPExcel -> getActiveSheet();
        $objectSheet -> setTitle("测试Excel");
        $objectSheet -> setCellValue("A1","ddddddd");
        $objectSheet -> setCellValue("A2",26);
        $objectSheet -> setCellValue("A3",true);
        $objectSheet -> setCellValue("A4",'=SUM(A2:A2)');
        
        $objectSheet -> setCellValue("B1","ddddddd");
        $objectSheet -> setCellValue("B2","aaaaaaa");
        $objectSheet -> setCellValue("B3","ccccccccc");
        $objectSheet -> setCellValue("B4","忠厚仁义的放空间的立法的浪费");
        
        //显示指定内容的类型
        $objectSheet -> setCellValueExplicit('A5','444444444444444445',PHPExcel_Cell_DataType::TYPE_STRING);
        //设置宽度
        $objectSheet -> getColumnDimension('A')->setWidth(20);
        $objectSheet -> getColumnDimension('B')->setWidth(40);
        
         //合并单元格
        $objectSheet -> mergeCells("A6:B6");
        $objectSheet -> mergeCells("A7:B7");
        $objectSheet -> mergeCells("A6:A7");
        
         //分离单元格
         $objectSheet -> unmergeCells('A6:A7');
         
         header('Content-Type: application/pdf');
         header('Content-Disposition: attachment;filename="01simple.pdf"');
         header('Cache-Control: max-age=0');
            
         $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
         $objWriter->save('php://output');
         exit;

另:下载后在Tests文件夹下有好多实例,可以参考,




原创粉丝点击