Loading

phpmail发送phpexcel生成的附件,php导出的Excel 作为邮件附件发送

PHP发送邮件戳这里https://www.cnblogs.com/wangzhaobo/p/8931328.html

PHP导出excel戳这里https://www.cnblogs.com/wangzhaobo/p/10143054.html

看完这两篇文章就只剩下一个问题

怎么把PHP导出的excel直接保存到服务器上而不是弹框下载.

第一步是把那些设置的header代码去掉,

第二步是保存excel

以下是保存excel到服务器的PHP代码, 然后再发送邮件的代码里面这样写$mail->AddAttachment($this->exportListExcel($expTitle,$expCellName,$expTableData)); 完成 !

public function exportListExcel($expTitle,$expCellName,$expTableData){
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $xlsTitle;
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);
        Loader::import('PHPExcel.Classes.PHPExcel');
        Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
        Loader::import('PHPExcel.Classes.PHPExcel.Reader.Excel5');

        $objPHPExcel = new \PHPExcel();
        // 居中
        $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal('center');
        $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical('center');
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');

        // $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
        // $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
        for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'1', $expCellName[$i][1]);
            $objPHPExcel->getActiveSheet(0)->getColumnDimension($cellName[$i])->setWidth($expCellName[$i][2]);
        }
        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<$dataNum;$i++){
            for($j=0;$j<$cellNum;$j++){
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+2), $expTableData[$i][$expCellName[$j][0]]);
            }
        }

        ob_start();
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        $content = ob_get_contents();
        ob_end_clean();
        $file = $fileName.".xls" ;
        file_put_contents($file, $content);
        return $file;
    }

 

posted @ 2019-01-17 15:52  王召波  阅读(1688)  评论(0编辑  收藏  举报