色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

詳解如何實現(xiàn)phpoffice的excel導(dǎo)入功能解耦

瀏覽:5日期:2022-06-14 10:53:23
目錄前言:沖突:解決方法:編碼:前言:

在業(yè)務(wù)中開發(fā)中,表格的導(dǎo)入導(dǎo)出功能很常見。但是這里主要是使用PhpOffice類庫介紹實現(xiàn)導(dǎo)入表格數(shù)據(jù)的功能。

沖突:

大部分的導(dǎo)入功能,就是通過點擊按鈕上傳一張表格,然后后臺讀取表格數(shù)據(jù)根據(jù)業(yè)務(wù)整理后直接插入到數(shù)據(jù)庫,最后再返回給前端。但是如果表格數(shù)據(jù)龐大,業(yè)務(wù)邏輯復(fù)雜的時候,就會導(dǎo)致導(dǎo)入那一塊很臃腫不好維護(hù)。

解決方法:

處理方式是把導(dǎo)入與業(yè)務(wù)數(shù)據(jù)插入分離,所以在二者之間添加一個隊列就可以了。導(dǎo)入只負(fù)責(zé)將表格數(shù)據(jù)存入隊列。業(yè)務(wù)部分可以是單獨的系統(tǒng),最后就是消費隊列中的數(shù)據(jù)了。這樣一來,不但提升了導(dǎo)入速度,而且還讓導(dǎo)入與系統(tǒng)解耦,不會因為異常而影響到其他業(yè)務(wù)。

編碼:

1.下載PhpOffice。

composer repuire phpoffice/phpspreadsheet

2.導(dǎo)入導(dǎo)出代碼。

<?phpnamespace app\common\helper;use PhpOffice\PhpSpreadsheet\Spreadsheet;use PhpOffice\PhpSpreadsheet\Writer\Xlsx;use PhpOffice\PhpSpreadsheet\IOFactory;use PhpOffice\PhpSpreadsheet\Cell\Coordinate;use think\Exception;class Excel{ // 導(dǎo)出 public function outPut($data, $columns, $table = '導(dǎo)出文件') {$spreadsheet = new Spreadsheet();$sheet = $spreadsheet->getActiveSheet();// 設(shè)置第一欄的標(biāo)題foreach ($columns as $k => $v) { $sheet->setCellValue($k . '1', $v['title']);}//第二行起 設(shè)置內(nèi)容$baseRow = 2; //數(shù)據(jù)從N-1行開始往下輸出 這里是避免頭信息被覆蓋foreach ($data as $key => $value) { foreach ($columns as $k1 => $v1) {$i = $key + $baseRow;$sheet->setCellValue($k1 . $i, $value[$v1['field']]); }}$writer = new Xlsx($spreadsheet);$filename = $table . date('Y-m-d', time()) . '_' . time() . '.xlsx';$writer->save('./excel/' . $filename);return '/excel/' . $filename; } // 導(dǎo)入 public function importExcel($file = '', $sheet = 0, $columnCnt = 0, &$options = []) {try { $file = iconv('utf-8', 'gb2312', $file); if (empty($file) OR !file_exists($file)) {throw new \Exception('文件不存在!'); } $objRead = IOFactory::createReader('Xlsx'); if (!$objRead->canRead($file)) {$objRead = IOFactory::createReader('Xls');if (!$objRead->canRead($file)) { throw new \Exception('只支持導(dǎo)入Excel文件!');} } /* 如果不需要獲取特殊操作,則只讀內(nèi)容,可以大幅度提升讀取Excel效率 */ empty($options) && $objRead->setReadDataOnly(true); /* 建立excel對象 */ $obj = $objRead->load($file); /* 獲取指定的sheet表 */ $currSheet = $obj->getSheet($sheet); //$currSheet = $obj->getSheetByName($sheet); // 根據(jù)名字 if (isset($options['mergeCells'])) {/* 讀取合并行列 */$options['mergeCells'] = $currSheet->getMergeCells(); } if (0 == $columnCnt) {/* 取得最大的列號 */$columnH = $currSheet->getHighestColumn();/* 兼容原邏輯,循環(huán)時使用的是小于等于 */$columnCnt = Coordinate::columnIndexFromString($columnH); } /* 獲取總行數(shù) */ $rowCnt = $currSheet->getHighestRow(); $data = []; /* 讀取內(nèi)容 */ for ($_row = 1; $_row <= $rowCnt; $_row++) {$isNull = true;for ($_column = 1; $_column <= $columnCnt; $_column++) { $cellName = Coordinate::stringFromColumnIndex($_column); $cellId = $cellName . $_row; $cell = $currSheet->getCell($cellId); if (isset($options['format'])) {/* 獲取格式 */$format = $cell->getStyle()->getNumberFormat()->getFormatCode();/* 記錄格式 */$options['format'][$_row][$cellName] = $format; } if (isset($options['formula'])) {/* 獲取公式,公式均為=號開頭數(shù)據(jù) */$formula = $currSheet->getCell($cellId)->getValue();if (0 === strpos($formula, '=')) { $options['formula'][$cellName . $_row] = $formula;} } if (isset($format) && 'm/d/yyyy' == $format) {/* 日期格式翻轉(zhuǎn)處理 */$cell->getStyle()->getNumberFormat()->setFormatCode('yyyy/mm/dd'); } $data[$_row][$cellName] = trim($currSheet->getCell($cellId)->getFormattedValue()); if (!empty($data[$_row][$cellName])) {$isNull = false; }}if ($isNull) { unset($data[$_row]);} } return $data;} catch (\Exception $e) { throw $e;} }}

3.抽取指定的字段格式化Excel數(shù)據(jù)。

return [ // 導(dǎo)入的表格標(biāo)題 'bidding' => ['stock_no' => '編號','price' => '價格','mobile' => '手機(jī)','nickname' => '姓名' ]];// 格式化指定列數(shù)據(jù)(默認(rèn)第一行表頭)public static function formattingCells(array $data, array $cellConfig){ $res = array_values($data); // 表頭 $header = $res[0]; $cellKeys = []; foreach ($header as $key => $value) {foreach ($cellConfig as $k => $v) { if ($value == $v) {$cellKeys[$key] = $k; }} } if (count($cellKeys) != count($cellConfig)) {throw new Exception('表格不完整'); } // 需要添加過濾 $temp = []; for ($i = 1; $i <= count($res) - 1; $i++) {foreach ($cellKeys as $m => $n) { $temp[$i][$n] = $res[$i][$m];} } return array_values($temp);}

4.導(dǎo)入部分,上傳接口。

// 導(dǎo)入表格,上傳接口public function importExcel(){ $upload_file = $_FILES['files']['tmp_name']; $input = $this->input; // ID $id = isset($input['id']) ? $input['id'] : 0; // 默認(rèn)取第一工作表 $excelData = (new Excel())->importExcel($upload_file, 0); // 取Excel字段 $config = config('excel_export.bidding'); $price_offer = Excel::formattingCells($excelData, $config); // 判斷每條記錄的手機(jī)和價格格式 // …… $jsonList = json_encode(compact('id', 'price_offer')); //$jsonList = json_encode($price_offer); // 入MQ $host = config('mq.host'); $options = config('mq.price_offer_import'); try {$mq = new ProductMQ($host, $options);$mq->publish($jsonList);$mq->close(); } catch (\Exception $e) {return $this->jsonData(200, $e->getMessage()); } // 入MQ return $this->jsonData(200, '導(dǎo)入成功');}

5.消費業(yè)務(wù)邏輯。

以上就是詳解如何實現(xiàn)phpoffice的excel導(dǎo)入功能解耦的詳細(xì)內(nèi)容,更多關(guān)于phpoffice excel導(dǎo)入解耦的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: PHP
主站蜘蛛池模板: 老司机亚洲精品影院在线 | 波野多结衣在线观看 | 欧美大屁股精品毛片视频 | 欧美xxxxx毛片| 亚洲高清国产品国语在线观看 | 一品道一本香蕉视频 | 免费人成在线观看网站视频 | 亚洲rct中文字幕在线 | 日本不卡一区在线 | 成人影院欧美大片免费看 | 8050网午夜一级毛片免费不卡 | 99久久精品国产亚洲 | 综合久久99久久99播放 | 欧美人成在线观看 | 孕妇xxxx视频在线 | 久久久久久久久久久9精品视频 | 99在线精品免费视频九九视 | 欧美一级毛片在线一看 | 三级网站在线 | 久久中文字幕免费视频 | 亚洲欧美卡通成人制服动漫 | 操亚洲| 成人看片黄a免费看视频 | 久久精品高清视频 | 一本久综合久久爱 | 在线aaa| 日韩国产欧美在线观看 | 中文字幕视频在线观看 | 国产一区二区三区在线免费 | 日本暖暖视频在线播放 | 国产在线美女 | 欧美性视频一区二区三区 | 亚洲人成影院在线高清 | 精品日韩欧美一区二区三区在线播放 | 欧美在线亚洲国产免m观看 欧美在线一级精品 | 在线精品亚洲 | 欧美成人看片一区二区三区 | 国产精品亚洲玖玖玖在线靠爱 | 乱子伦xxxx | 久草免费在线观看视频 | 日本一极毛片兔费看 |