本篇文章給大家介紹一下使用PhpSpreadsheet導入導出Excel的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。

PHP對Excel導入&導出操作
最近公司要做報表功能,各種財務(wù)報表、工資報表、考勤報表等,復雜程度讓人頭大,于是特地封裝適用各大場景的導入&導出操作,希望各界大神支出不足之處,以便小弟繼續(xù)完善。
phpspreadsheet 引入
由于PHPExcel早就停止更新維護,所以適用phpspreadsheet。不知道如何通過composer拉取項目包的同學,可以查看Composer學習一文。引入方法:
composer require phpoffice/phpspreadsheet
引入命名空間
由于本人項目中需要居中、背景、單元格格式等各種操作,所以引入較多,大家使用的時候,可以根據(jù)自己實際需要引入。
use PhpOfficePhpSpreadsheetReaderXlsx;use PhpOfficePhpSpreadsheetReaderXls;use PhpOfficePhpSpreadsheetIOFactory;use PhpOfficePhpSpreadsheetCellCoordinate;use PhpOfficePhpSpreadsheetSpreadsheet;use PhpOfficePhpSpreadsheetWorksheetPageSetup;use PhpOfficePhpSpreadsheetCellDataType;use PhpOfficePhpSpreadsheetStyleFill;use PhpOfficePhpSpreadsheetStyleColor;use PhpOfficePhpSpreadsheetStyleAlignment;use PhpOfficePhpSpreadsheetStyleBorder;use PhpOfficePhpSpreadsheetStyleNumberFormat;
Excel導入操作(importExcel)
除了單純的處理Excel數(shù)據(jù)外,還可以將Excel中的合并項、公式項、單元格格式提取,提取后可根據(jù)業(yè)務(wù)需求做對應(yīng)處理后存儲起來,以便后續(xù)的各種操作。
/** * 使用PHPEXECL導入 * * @param string $file 文件地址 * @param int $sheet 工作表sheet(傳0則獲取第一個sheet) * @param int $columnCnt 列數(shù)(傳0則自動獲取最大列) * @param array $options 操作選項 * array mergeCells 合并單元格數(shù)組 * array formula 公式數(shù)組 * array format 單元格格式數(shù)組 * * @return array * @throws Exception */function importExecl(string $file = '', int $sheet = 0, int $columnCnt = 0, &$options = []){ try { /* 轉(zhuǎn)碼 */ $file = iconv("utf-8", "gb2312", $file); if (empty($file) OR !file_exists($file)) { throw new Exception('文件不存在!'); } /** @var Xlsx $objRead */ $objRead = IOFactory::createReader('Xlsx'); if (!$objRead->canRead($file)) { /** @var Xls $objRead */ $objRead = IOFactory::createReader('Xls'); if (!$objRead->canRead($file)) { throw new Exception('只支持導入Excel文件!'); } } /* 如果不需要獲取特殊操作,則只讀內(nèi)容,可以大幅度提升讀取Excel效率 */ empty($options) && $objRead->setReadDataOnly(true); /* 建立excel對象 */ $obj = $objRead->load($file); /* 獲取指定的sheet表 */ $currSheet = $obj->getSheet($sheet); 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; } } /* 判斷是否整行數(shù)據(jù)為空,是的話刪除該行數(shù)據(jù) */ if ($isNull) { unset($data[$_row]); } } return $data; } catch (Exception $e) { throw $e; }}
將數(shù)據(jù)處理好后,可以通過額外配置,將導出的Excel做各種不同的配置,例如打印樣式、鎖定行、背景色、寬度等。
Excel導出操作(exportExcel)
/** * Excel導出,TODO 可繼續(xù)優(yōu)化 * * @param array $datas 導出數(shù)據(jù),格式['A1' => 'XXXX公司報表', 'B1' => '序號'] * @param string $fileName 導出文件名稱 * @param array $options 操作選項,例如: * bool print 設(shè)置打印格式 * string freezePane 鎖定行數(shù),例如表頭為第一行,則鎖定表頭輸入A2 * array setARGB 設(shè)置背景色,例如['A1', 'C1'] * array setWidth 設(shè)置寬度,例如['A' => 30, 'C' => 20] * bool setBorder 設(shè)置單元格邊框 * array mergeCells 設(shè)置合并單元格,例如['A1:J1' => 'A1:J1'] * array formula 設(shè)置公式,例如['F2' => '=IF(D2>0,E42/D2,0)'] * array format 設(shè)置格式,整列設(shè)置,例如['A' => 'General'] * array alignCenter 設(shè)置居中樣式,例如['A1', 'A2'] * array bold 設(shè)置加粗樣式,例如['A1', 'A2'] * string savePath 保存路徑,設(shè)置后則文件保存到服務(wù)器,不通過瀏覽器下載 */function exportExcel(array $datas, string $fileName = '', array $options = []): bool{ try { if (empty($datas)) { return false; } set_time_limit(0); /** @var Spreadsheet $objSpreadsheet */ $objSpreadsheet = app(Spreadsheet::class); /* 設(shè)置默認文字居左,上下居中 */ $styleArray = [ 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_LEFT, 'vertical' => Alignment::VERTICAL_CENTER, ], ]; $objSpreadsheet->getDefaultStyle()->applyFromArray($styleArray); /* 設(shè)置Excel Sheet */ $activeSheet = $objSpreadsheet->setActiveSheetIndex(0); /* 打印設(shè)置 */ if (isset($options['print']) && $options['print']) { /* 設(shè)置打印為A4效果 */ $activeSheet->getPageSetup()->setPaperSize(PageSetup:: PAPERSIZE_A4); /* 設(shè)置打印時邊距 */ $pValue = 1 / 2.54; $activeSheet->getPageMargins()->setTop($pValue / 2); $activeSheet->getPageMargins()->setBottom($pValue * 2); $activeSheet->getPageMargins()->setLeft($pValue / 2); $activeSheet->getPageMargins()->setRight($pValue / 2); } /* 行數(shù)據(jù)處理 */ foreach ($datas as $sKey => $sItem) { /* 默認文本格式 */ $pDataType = DataType::TYPE_STRING; /* 設(shè)置單元格格式 */ if (isset($options['format']) && !empty($options['format'])) { $colRow = Coordinate::coordinateFromString($sKey); /* 存在該列格式并且有特殊格式 */ if (isset($options['format'][$colRow[0]]) && NumberFormat::FORMAT_GENERAL != $options['format'][$colRow[0]]) { $activeSheet->getStyle($sKey)->getNumberFormat() ->setFormatCode($options['format'][$colRow[0]]); if (false !== strpos($options['format'][$colRow[0]], '0.00') && is_numeric(str_replace(['¥', ','], '', $sItem))) { /* 數(shù)字格式轉(zhuǎn)換為數(shù)字單元格 */ $pDataType = DataType::TYPE_NUMERIC; $sItem = str_replace(['¥', ','], '', $sItem); } } elseif (is_int($sItem)) { $pDataType = DataType::TYPE_NUMERIC; } } $activeSheet->setCellValueExplicit($sKey, $sItem, $pDataType); /* 存在:形式的合并行列,列入A1:B2,則對應(yīng)合并 */ if (false !== strstr($sKey, ":")) { $options['mergeCells'][$sKey] = $sKey; } } unset($datas); /* 設(shè)置鎖定行 */ if (isset($options['freezePane']) && !empty($options['freezePane'])) { $activeSheet->freezePane($options['freezePane']); unset($options['freezePane']); } /* 設(shè)置寬度 */ if (isset($options['setWidth']) && !empty($options['setWidth'])) { foreach ($options['setWidth'] as $swKey => $swItem) { $activeSheet->getColumnDimension($swKey)->setWidth($swItem); } unset($options['setWidth']); } /* 設(shè)置背景色 */ if (isset($options['setARGB']) && !empty($options['setARGB'])) { foreach ($options['setARGB'] as $sItem) { $activeSheet->getStyle($sItem) ->getFill()->setFillType(Fill::FILL_SOLID) ->getStartColor()->setARGB(Color::COLOR_YELLOW); } unset($options['setARGB']); } /* 設(shè)置公式 */ if (isset($options['formula']) && !empty($options['formula'])) { foreach ($options['formula'] as $fKey => $fItem) { $activeSheet->setCellValue($fKey, $fItem); } unset($options['formula']); } /* 合并行列處理 */ if (isset($options['mergeCells']) && !empty($options['mergeCells'])) { $activeSheet->setMergeCells($options['mergeCells']); unset($options['mergeCells']); } /* 設(shè)置居中 */ if (isset($options['alignCenter']) && !empty($options['alignCenter'])) { $styleArray = [ 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER, ], ]; foreach ($options['alignCenter'] as $acItem) { $activeSheet->getStyle($acItem)->applyFromArray($styleArray); } unset($options['alignCenter']); } /* 設(shè)置加粗 */ if (isset($options['bold']) && !empty($options['bold'])) { foreach ($options['bold'] as $bItem) { $activeSheet->getStyle($bItem)->getFont()->setBold(true); } unset($options['bold']); } /* 設(shè)置單元格邊框,整個表格設(shè)置即可,必須在數(shù)據(jù)填充后才可以獲取到最大行列 */ if (isset($options['setBorder']) && $options['setBorder']) { $border = [ 'borders' => [ 'allBorders' => [ 'borderStyle' => Border::BORDER_THIN, // 設(shè)置border樣式 'color' => ['argb' => 'FF000000'], // 設(shè)置border顏色 ], ], ]; $setBorder = 'A1:' . $activeSheet->getHighestColumn() . $activeSheet->getHighestRow(); $activeSheet->getStyle($setBorder)->applyFromArray($border); unset($options['setBorder']); } $fileName = !empty($fileName) ? $fileName : (date('YmdHis') . '.xlsx'); if (!isset($options['savePath'])) { /* 直接導出Excel,無需保存到本地,輸出07Excel文件 */ header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header( "Content-Disposition:attachment;filename=" . iconv( "utf-8", "GB2312//TRANSLIT", $fileName ) ); header('Cache-Control: max-age=0');//禁止緩存 $savePath = 'php://output'; } else { $savePath = $options['savePath']; } ob_clean(); ob_start(); $objWriter = IOFactory::createWriter($objSpreadsheet, 'Xlsx'); $objWriter->save($savePath); /* 釋放內(nèi)存 */ $objSpreadsheet->disconnectWorksheets(); unset($objSpreadsheet); ob_end_flush(); return true; } catch (Exception $e) { return false; }}
推薦學習:php視頻教程
站長資訊網(wǎng)