久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      關(guān)于PHP導(dǎo)出Excel的優(yōu)化詳解

      背景

      針對(duì)PHP導(dǎo)出Excel的優(yōu)化,在我之前的一篇文章里已經(jīng)做過介紹:關(guān)于PHP內(nèi)存溢出的思考,本文主要是介紹一款高性能的導(dǎo)出組件–xlswriter,他是一個(gè)PHP C擴(kuò)展,官方文檔地址,請(qǐng)點(diǎn)擊。

      推薦:PHP視頻教程

      安裝

      安裝pecl

      當(dāng)我們發(fā)現(xiàn)pecl未安裝時(shí),則需要安裝pecl。一般情況下,是安裝在PHP的安裝目錄,示例命令如下:

      # 進(jìn)入PHP安裝目錄 cd /usr/local/php/bin curl -o go-pear.php http://pear.php.net/go-pear.phar php go-pear.php # 安裝完成后,軟連接到bin目錄下 ln -s /usr/local/php/bin/pecl /usr/bin/pecl

      安裝xlswriter

      pecl install xlswriter # 添加 extension = xlswriter.so 到 ini 配置

      使用

      具體使用可以參考官方文檔,會(huì)介紹的更加詳細(xì),我這就上一段我使用中的代碼:

      封裝的導(dǎo)出service

        /**      * 下載      * @param $header      * @param $data      * @param $fileName      * @param $type      * @return bool      * @throws      */     public function download($header, $data, $fileName)     {         $config     = [             'path' => $this->getTmpDir() . '/',         ];         $now        = date('YmdHis');         $fileName   = $fileName . $now . '.xlsx';         $xlsxObject = new VtifulKernelExcel($config);         // Init File         $fileObject = $xlsxObject->fileName($fileName);         // 設(shè)置樣式         $fileHandle = $fileObject->getHandle();         $format     = new VtifulKernelFormat($fileHandle);         $style      = $format->bold()->background(             VtifulKernelFormat::COLOR_YELLOW         )->align(Format::FORMAT_ALIGN_VERTICAL_CENTER)->toResource();         // Writing data to a file ......         $fileObject->header($header)             ->data($data)             ->freezePanes(1, 0)             ->setRow('A1', 20, $style);         // Outptu         $filePath = $fileObject->output(); // 下載  header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");         header('Content-Disposition: attachment;filename="' . $fileName . '"');         header('Cache-Control: max-age=0');         header('Content-Length: ' . filesize($filePath));         header('Content-Transfer-Encoding: binary');         header('Cache-Control: must-revalidate');         header('Pragma: public');         ob_clean();         flush();         if (copy($filePath, 'php://output') === false) {             throw new RuntimeException('導(dǎo)出失敗');         }                // Delete temporary file         @unlink($filePath);         return true;     }     /**      * 獲取臨時(shí)文件夾      * @return false|string      */     private function getTmpDir()     {       // 目錄可以自定義       // return Yii::$app->params['downloadPath'];                $tmp = ini_get('upload_tmp_dir');         if ($tmp !== False && file_exists($tmp)) {             return realpath($tmp);         }         return realpath(sys_get_temp_dir());     }     /**      * 讀取文件      * @param $path      * @param $fileName      * @return array      */     public function readFile($path,$fileName)     {         // 讀取測(cè)試文件         $config = ['path' => $path];         $excel  = new VtifulKernelExcel($config);         $data   = $excel->openFile($fileName)             ->openSheet()             ->getSheetData();         return $data;     }

      調(diào)用處代碼

      導(dǎo)出

          /**      * 導(dǎo)出      */     public function actionExport()     {         try {             /**              * @var $searchModel SkuBarCodeSearch              */             $searchModel                     = Yii::createObject(SkuBarCodeSearch::className());             $queryParams['SkuBarCodeSearch'] = [];             $result = $searchModel->search($queryParams, true);             $formatData = [];             if (!empty($result)) {                 foreach ($result as $key => &$value) {                     $tmpData      = [                         'sku_code'   => $value['sku_code'],                         'bar_code'   => $value['bar_code'],                         'created_at' => $value['created_at'],                         'updated_at' => $value['updated_at'],                     ];                     $formatData[] = array_values($tmpData);                 }                 unset($value);             }             $fields = [                 'sku_code'   => 'SKU編碼',                 'bar_code'   => '條形碼',                 'created_at' => '創(chuàng)建時(shí)間',                 'updated_at' => '更新時(shí)間',             ];             /**              * @var $utilService UtilService              */             $utilService = UtilService::getInstance();             $utilService->download(array_values($fields), $formatData, 'sku_single_code');         } catch (Exception $e) {             Yii::$app->getSession()->setFlash('error', '導(dǎo)出失敗');         }     }

      導(dǎo)入

      public function actionImportTmpSku()     {         try {             /**              * @var $utilService UtilService              */             $utilService = UtilService::getInstance();             $path        = '/tmp/'; // 文件目錄             $fileName    = 'sku_0320.xlsx';             $excelData   = $utilService->readFile($path, $fileName);             unset($excelData[0]);             $excelData = array_merge($excelData);            // ... ... 業(yè)務(wù)代碼         } catch (Exception $e) {             echo $e->getMessage();             exit;         }     }

      結(jié)論

      整體使用下來,在處理大數(shù)據(jù)量時(shí),性能相比于原來的PHPExcel確實(shí)高了很多。

      本文系轉(zhuǎn)載,原文地址是:

      https://tsmliyun.github.io/php/PHP%E5%AF%BC%E5%87%BAExcel%E7%9A%84%E4%BC%98%E5%8C%96/

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)