ThinkPHP5.0导出Excel表格

上周在项目中用tp5.0导出excel表格遇到一点问题,特此记录。
首先,不管是tp3.2.3还是tp5.0导出excel表格我都是基于phpexcel这个插件来做的。
然后,下载phpexcel放到vendor目录下。
以下是代码:

public function download(){
        $name = Request::instance()->param('ship');
        $start_time = Request::instance()->param('start_time');
        $end_time = Request::instance()->param('end_time');
        $data = getDatas($name,$start_time,$end_time);
        $excelName = "船舶轨迹信息表";
        Vendor('phpexcel.PHPExcel');//调用类库,路径是基于vendor文件夹的
        Vendor('phpexcel.PHPExcel.Worksheet.Drawing');
        Vendor('phpexcel.PHPExcel.Writer.Excel2007');

        //Vendor('PHPExcel179.PHPExcel');
        $objPHPExcel = new \PHPExcel();
        //设置宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(25);
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(20);
        //设置行高
        $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(30);
        $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
        //设置字体样式
        $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10); //默认字体大小
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(16)->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getFont()->setSize(12)->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A3:I3')->getFont()->setBold(true); //粗体
        //合并excel
        $objPHPExcel->getActiveSheet()->mergeCells('A1:I1');
        $objPHPExcel->getActiveSheet()->mergeCells('A2:I2');
        //设置垂直、水平居中
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()
            ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()
            ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
        $objPHPExcel->getActiveSheet()->getStyle('A3:I3')->getAlignment()
            ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //设置边框
        $objPHPExcel->getActiveSheet()->getStyle('A3:I3')->getBorders()->getAllBorders()
            ->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', $excelName)
            ->setCellValue('A2', '统计周期:' . $start_time . '--' . $end_time)
            ->setCellValue('A3', '序号')
            ->setCellValue('B3', '船舶名称')
            ->setCellValue('C3', '时间')
            ->setCellValue('D3', '东西经')
            ->setCellValue('E3', '南北纬')
            ->setCellValue('F3', '经度')
            ->setCellValue('G3', '纬度')
            ->setCellValue('H3', '报警信息')
            ->setCellValue('I3', '定位信息');
        $count = count($data);
        for ($i =0;  $i<$count;$i++) {
            $objPHPExcel->getActiveSheet()->setCellValue('A' . ($i+4), $i + 1);
            $objPHPExcel->getActiveSheet()->setCellValue('B' . ($i + 4), $data[$i]['name']);
            $objPHPExcel->getActiveSheet()->setCellValue('C' . ($i + 4), $data[$i]['time']);
            $objPHPExcel->getActiveSheet()->setCellValue('D' . ($i + 4), $data[$i]['ew']);
            $objPHPExcel->getActiveSheet()->setCellValue('E' . ($i + 4), $data[$i]['ns']);
            $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i + 4), $data[$i]['latitude']);
            $objPHPExcel->getActiveSheet()->setCellValue('G' . ($i + 4), $data[$i]['longitude']);
            $objPHPExcel->getActiveSheet()->setCellValue('H' . ($i + 4), $data[$i]['alarm']);
            $objPHPExcel->getActiveSheet()->setCellValue('I' . ($i + 4), $data[$i]['gps']);
            
            //设置垂直、水平居中
            $objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 4) . ':I' . ($i + 4))->getAlignment()
                ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
                ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            
            $objPHPExcel->getActiveSheet()->getRowDimension($i + 4)->setRowHeight(20);//行高
            
            //设置边框
            $objPHPExcel->getActiveSheet()->getStyle('A' . ($i + 4) . ':I' . ($i + 4))->getBorders()->getAllBorders()
                ->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
        }
        
        //sheet命名
        $objPHPExcel->getActiveSheet()->setTitle($excelName);
        //默认打开的sheet
        $objPHPExcel->setActiveSheetIndex(0);

        $outfile = "$excelName".date("Ymd").".xls";
        //excel头参数
        $outfile = iconv('utf-8', "gb2312", $outfile);  //解决文件名乱码
        header("Content-Type:application/vnd.ms-execl");
        header('Content-Disposition:attachment;filename="'.$outfile.'"');//日期文件名后缀
        header('Cache-Control:max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
    }

这里需要指出的是,我在测试过程中发现,导出excel表格,文件名会乱码,解决办法就是 iconv('utf-8', "gb2312", $outfile),用这个来进行转码。

你可能感兴趣的:(ThinkPHP5.0导出Excel表格)