1. 程式人生 > >Yii2框架中新增擴充套件PHPExcel或PHPMailer等

Yii2框架中新增擴充套件PHPExcel或PHPMailer等

Yii2中所有檔案的引用都是通過名稱空間來的,這裡只主要解決名稱空間的問題即可

新增中間類LExcelHelper,假設phpexcel的資料夾的lib下,則類LExcelHelper應放在在lib下, 內容如下:

namespace XXX\lib;
include_once 'PHPExcel/Classes/PHPExcel.php';

class LExcelHelper extends \PHPExcel
{
    

}

使用PHPExcel的時候就直接new物件LExcelHelper

$objExcel = new LExcelHelper();
$objWriter = new \PHPExcel_Writer_Excel5($objExcel);
 //設定屬性 (這段程式碼無關緊要,其中的內容可以替換為你需要的)
$objExcel->getProperties()->setCreator("andy");
$objExcel->getProperties()->setLastModifiedBy("andy");
$objExcel->getProperties()->setTitle("Office 2003 XLS Test Document");
$objExcel->getProperties()->setSubject("Office 2003 XLS Test Document");
$objExcel->getProperties()->setDescription("Test document for Office 2003 XLS, generated using PHP classes.");
$objExcel->getProperties()->setKeywords("office 2003 openxml php");
$objExcel->getProperties()->setCategory("Test result file");

$objExcel->setActiveSheetIndex(0);

 $i=0;
 //表頭
 $k1="xxx";
$k2="xxxxx";
$k3="xxxxx";
$k4="xxxxx";
 $k5="xxxxx";

        $objExcel->getActiveSheet()->setCellValue('a1', "$k1");
        $objExcel->getActiveSheet()->setCellValue('b1', "$k2");
        $objExcel->getActiveSheet()->setCellValue('c1', "$k3");
        $objExcel->getActiveSheet()->setCellValue('d1', "$k4");
        $objExcel->getActiveSheet()->setCellValue('e1', "$k5");
        /** @var Tips  $v */
        foreach($data as $v)
        {
        
            $u1 = $i+2;
            /*----------寫入內容-------------*/
            $objExcel->getActiveSheet()->setCellValue('a'.$u1, $v->xxx);
            $objExcel->getActiveSheet()->setCellValue('b'.$u1, $v->xxx);
            $objExcel->getActiveSheet()->setCellValue('c'.$u1, $v->xxx);
            $objExcel->getActiveSheet()->setCellValue('d'.$u1, $v->xxx);
            $objExcel->getActiveSheet()->setCellValue('e'.$u1, $v->xxx);
            $i++;
        }

        // 高置列的寬度
        $objExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objExcel->getActiveSheet()->getColumnDimension('B')->setWidth(10);
        $objExcel->getActiveSheet()->getColumnDimension('C')->setWidth(70);
        $objExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15);
        $objExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15);

        $objExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BPersonal cash register&RPrinted on &D');
        $objExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objExcel->getProperties()->getTitle() . '&RPage &P of &N');

        // 設定頁方向和規模
        $objExcel->getActiveSheet()->getPageSetup()->setOrientation(\PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
        $objExcel->getActiveSheet()->getPageSetup()->setPaperSize(\PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
        $objExcel->setActiveSheetIndex(0);
        $timestamp = time();
        $ex = '2007';

        if($ex == '2007')

{

     //匯出excel2007文件

            $outputFileName = $timestamp."links_out.xls";
            $objWriter->save($outputFileName);
            exit;

}

else

{        //匯出excel2003文

            header('Content-Type: application/vnd.ms-excel');

            header('Content-Disposition: attachment;filename="links_out'.$timestamp.'.xls"');
            header('Cache-Control: max-age=0');
            $objWriter = \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
            $objWriter->save('php://output');
            exit;
        }