CI 框架整合 PHPExcel

joker丶牧羊人發表於2018-10-22
(本地開發環境下進行: CI 3.x + PHPExcel 1.8)
1、下載 PHPExcel 1.8

下載地址:archive.codeplex.com/?p=phpexcel

2、解壓,拷貝有效檔案及資料夾
2-1、將解壓後的資料夾裡的Classes資料夾裡面的所有檔案和資料夾放到application\libraries\目錄下,目錄結構如下:

application\libraries\PHPExcel.php application\libraries\PHPExcel\

2-2、修改application\libraries\PHPExcel\IOFactory.php 檔案

1)將其類名從PHPExcel_IOFactory改為IOFactory,遵從CI類命名規則 2)將其建構函式改為public

3、程式碼整合範例
3-1、匯出

先看下效果截圖:

phpexcel_export.png
控制器程式碼:

public function ExcelExport(){
    $this->load->library('PHPExcel');
    $this->load->library('PHPExcel/IOFactory');

    $objPHPExcel = new PHPExcel();
    // 設定sheet標籤
    $objPHPExcel->setActiveSheetIndex(0);
    // 設定sheet的name
    $objPHPExcel->getActiveSheet()->setTitle('測試sheet');

    // 合併單元格
    $objPHPExcel->getActiveSheet()->mergeCells('A1:E1');
    // 設定行高
    $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(40);

    // 自動行高
    $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(-1);

    // 設定水平居中
    $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
    //設定垂直居中
    $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

    // 設定加粗
    $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);

    //設定當前的sheet
    $objPHPExcel->getActiveSheet()->setCellValue('A1', '匯出測試excel樣式及內容');
    $objPHPExcel->getActiveSheet()->setCellValue('A2', 'IMG');
    $objPHPExcel->getActiveSheet()->setCellValue('B2', '專案');
    $objPHPExcel->getActiveSheet()->setCellValue('C2', '結果');
    $objPHPExcel->getActiveSheet()->setCellValue('D2', '數量');
    $objPHPExcel->getActiveSheet()->setCellValue('E2', '統計');

    $i = 3;
    $m_data = ['12', '5445', '457', '63'];
    foreach($m_data as $key => $value){
        // 設定行高
        $objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(45);
        $objPHPExcel->getActiveSheet()->getDefaultColumnDimension('A'.$i)->setWidth(8);

        // 圖片生成
        $imgPath = PUBLIC_IMG_RESOURCE_PATH . 'default-user.jpg';
        $objDrawing = new PHPExcel_Worksheet_Drawing();
        $objDrawing->setPath($imgPath);
        // 設定寬度高度
        $objDrawing->setHeight(40);//照片高度
        $objDrawing->setWidth(40); //照片寬度
        /*設定圖片要插入的單元格*/
        $objDrawing->setCoordinates('A'.$i);
        // 圖片偏移距離
        $objDrawing->setOffsetX(10);
        $objDrawing->setOffsetY(10);
        $objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

        $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $value);
        $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $value);
        $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $value);
        $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, '=SUM(B'.$i.':D'.$i.')');
        $i ++;
    }

    $outputFileName = time().".xls";
    $xlsWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
    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");
    $xlsWriter->save( "php://output" );
}
複製程式碼
3-2、匯入

控制器程式碼:

public function ExcelImport(){
    //載入工廠類
    $this->load->library('PHPExcel/IOFactory');

    //要讀取的xls檔案路徑
    $inputFileName = PUBLIC_FILE_RESOURCE_PATH . 'listTest/userListTest.xlsx';

    /** 用PHPExcel_IOFactory的load方法得到excel操作物件  **/
    $objPHPExcel = IOFactory::load($inputFileName);

    //得到當前活動表格,呼叫toArray方法,得到表格的二維陣列
    $sheetData = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);

    $sheet = $objPHPExcel->getActiveSheet(); // 當前活動表格
    $highestRow = $sheet -> getHighestRow(); // 取得總行數
    $highestColumn = $sheet->getHighestColumn(); // 取得總列數(格式:'AA')
    $columns = PHPExcel_Cell::columnIndexFromString($highestColumn); // 將資料總列數(格式:'AA')轉換成數字列數(格式:27)表示

    // 處理轉換成可以存入資料庫表中的資料
    $newData = [];
    for($currentRow = 2; $currentRow <= $highestRow; $currentRow++){
        $rowData = [];
        foreach($sheetData[1] as $key => $currentColumn){
            $rowData[$currentColumn] = $sheetData[$currentRow][$key]; //$objPHPExcel->getActiveSheet()->getCell($key.$currentRow)->getValue();
        }

        $newData[] = $rowData;
    }

    // 插入資料
    $res = $this->userModel->import_user($newData);

    if(!$res){
        $this->ajax_return('500', 'Import Failed!');
    }
    $this->ajax_return('200', 'Import Success!');
}
複製程式碼

至此,CI框架整合PHPExcel完畢。

【如若文件有錯誤,歡迎大家不吝賜教。本文件是集網上各位大神的資源進行整合的,具體資源來源已經忘記了,如果發現有侵權等行為,請聯絡我,我將對應處理,謝謝~~~】

相關文章