thinkphp用phpexcel导出excel2007

freedonn 发布于 2014/03/06 09:42
阅读 2K+
收藏 2

thinkphp用phpexcel导出excel2007,浏览器中下载的excel打不开,提示:

服务器中也是这个错误,但是可以打开,通过浏览器下载的里面没有内容。

代码:

public function conlist()
    {                 
        $con=M("con");
        $sql="select * from yyt_con ";            
                   
        $sql.=" order by id desc limit 50";
        $list = $con->query($sql);
        $this->export($list);
        
    }

 public function export($list){
           
        //导出excel开始
        //引入phpexcel扩展类(注意你的路劲)
        Vendor('PHPExcel.PHPExcel');
        Vendor('PHPExcel.PHPExcel.IOFactory');
        Vendor('PHPExcel.PHPExcel.Reader.Excel2007');
        
         $objPHPExcel = new PHPExcel();
        // Set properties
        $objPHPExcel->getProperties()->setCreator("ctos")
            ->setLastModifiedBy("ctos")
            ->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");

        //set width
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(30);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(50);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(50);
       
        //设置行高度
        $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(22);

        $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
   
        //set font size bold
        $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
        $objPHPExcel->getActiveSheet()->getStyle('A2:G2')->getFont()->setBold(true);

        $objPHPExcel->getActiveSheet()->getStyle('A2:G2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A2:G2')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

        //设置水平居中
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
       
        //合并cell
        $objPHPExcel->getActiveSheet()->mergeCells('A1:J1');

        // set table header content
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '数据统计:'.date('Y-m-d H:i:s'))
            ->setCellValue('A2', '词')
            ->setCellValue('B2', '数量')
            ->setCellValue('C2', '价格')
            ->setCellValue('D2', '用户名')
            ->setCellValue('E2', '网址')
            ->setCellValue('F2', '引擎')
            ->setCellValue('G2', '时间');      

        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<count($list)-1;$i++){
            $objPHPExcel->getActiveSheet(0)->setCellValue('A'.($i+3), $list[$i]['word']);
            $objPHPExcel->getActiveSheet(0)->setCellValue('B'.($i+3), $list[$i]['num']);
            $objPHPExcel->getActiveSheet(0)->setCellValue('C'.($i+3), $list[$i]['money']);
            $objPHPExcel->getActiveSheet(0)->setCellValue('D'.($i+3), $list[$i]['uname']);
            $objPHPExcel->getActiveSheet(0)->setCellValue('E'.($i+3), $list[$i]['site']);
            $objPHPExcel->getActiveSheet(0)->setCellValue('F'.($i+3), $list[$i]['yinqing']);
            $objPHPExcel->getActiveSheet(0)->setCellValue('G'.($i+3), $list[$i]['addtime']);
           
            $objPHPExcel->getActiveSheet()->getStyle('A'.($i+3).':G'.($i+3))->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle('A'.($i+3).':G'.($i+3))->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
            $objPHPExcel->getActiveSheet()->getRowDimension($i+3)->setRowHeight(16);
        }


        //  sheet命名
        $objPHPExcel->getActiveSheet()->setTitle('数据列表');


        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);


        // excel头参数
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="simple.xls"');  //日期为文件名后缀
        header('Cache-Control: max-age=0');
        
    
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');  //excel5为xls格式,excel2007为xlsx格式
        $objWriter->save('output');         
        //导出excel结束
    }
加载中
0
s
season886
跟我遇到的问题一模一样
0
s
season886
我也不知道怎么弄
0
哇咔咔咔
哇咔咔咔
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="simple.xls"');


$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');  //excel5为xls格式,excel2007为xlsx格式




你的header是xls格式的,导出模式却使用了xlsx格式的,所以

要么把Excel2007改成excel5,同时引用

Vendor('PHPExcel.PHPExcel.Reader.Excel5');



要么换下header

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');  
header('Content-Disposition: attachment;filename="simple.xlsx"');  
header('Cache-Control: max-age=0'); 





返回顶部
顶部