您好,登錄后才能下訂單哦!
這篇文章主要介紹了PHP怎么使用xlswriter進行大數據的導入導出的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇PHP怎么使用xlswriter進行大數據的導入導出文章都會有所收獲,下面我們一起來看看吧。
1、windows系統:
到PECL網站下載符合自己本地PHP環境的ddl文件,并復制到PHP的擴展目錄ext文件夾下,修改php.ini文件,
加上這行
extension=xlswriter
2、Linux系統:
使用命令安裝
pecl install xlswriter
php配置文件添加
extension = xlswriter.so
重啟:php nginx 查看PHP安裝xlswriter拓展
<?php namespace App\Services; use Vtiful\Kernel\Excel; class MultiFloorXlsWriterService { // 默認寬度 private $defaultWidth = 16; // 默認導出格式 private $exportType = '.xlsx'; // 表頭最大層級 private $maxHeight = 1; // 文件名 private $fileName = null; private $xlsObj; private $fileObject; private $format; /** * MultiFloorXlsWriterService constructor. * @throws \App\Exceptions\ApiException */ public function __construct() { // 文件默認輸出地址 $path = base_path().'/public/uploads/excel'; $config = [ 'path' => $path ]; $this->xlsObj = (new \Vtiful\Kernel\Excel($config)); } /** * 設置文件名 * @param string $fileName * @param string $sheetName * @author LWW */ public function setFileName(string $fileName = '', string $sheetName = 'Sheet1') { $fileName = empty($fileName) ? (string)time() : $fileName; $fileName .= $this->exportType; $this->fileName = $fileName; $this->fileObject = $this->xlsObj->fileName($fileName, $sheetName); $this->format = (new \Vtiful\Kernel\Format($this->fileObject->getHandle())); } /** * 設置表頭 * @param array $header * @param bool $filter * @throws \Exception * @author LWW */ public function setHeader(array $header, bool $filter = false) { if (empty($header)) { throw new \Exception('表頭數據不能為空'); } if (is_null($this->fileName)) { self::setFileName(time()); } // 獲取單元格合并需要的信息 $colManage = self::setHeaderNeedManage($header); // 完善單元格合并信息 $colManage = self::completeColMerge($colManage); // 合并單元格 self::queryMergeColumn($colManage, $filter); } /** * 填充文件數據 * @param array $data * @author LWW */ public function setData(array $data) { foreach ($data as $row => $datum) { foreach ($datum as $column => $value) { $this->fileObject->insertText($row + $this->maxHeight, $column, $value); } } } /** * 添加Sheet * @param string $sheetName * @author LWW */ public function addSheet(string $sheetName) { $this->fileObject->addSheet($sheetName); } /** * 保存文件至服務器 * @return mixed * @author LWW */ public function output() { return $this->fileObject->output(); } /** * 輸出到瀏覽器 * @param string $filePath * @throws \Exception * @author LWW */ public function excelDownload(string $filePath) { $fileName = $this->fileName; $userBrowser = $_SERVER['HTTP_USER_AGENT']; if (preg_match('/MSIE/i', $userBrowser)) { $fileName = urlencode($fileName); } else { $fileName = iconv('UTF-8', 'GBK//IGNORE', $fileName); } header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); header('Content-Disposition: attachment;filename="' . $fileName . '"'); header('Content-Length: ' . filesize($filePath)); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); header('Cache-Control: max-age=0'); header('Pragma: public'); if (ob_get_contents()) { ob_clean(); } flush(); if (copy($filePath, 'php://output') === false) { throw new \Exception($filePath . '地址出問題了'); } // 刪除本地文件 @unlink($filePath); exit(); } /** * 組裝單元格合并需要的信息 * @param array $header * @param int $col * @param int $cursor * @param array $colManage * @param null $parent * @param array $parentList * @return array * @throws \Exception * @author LWW */ private function setHeaderNeedManage(array $header,int $col = 1,int &$cursor = 0,array &$colManage = [], $parent = null,array $parentList = []) { foreach ($header as $head) { if (empty($head['title'])) { throw new \Exception('表頭數據格式有誤'); } if (is_null($parent)) { // 循環初始化 $parentList = []; $col = 1; } else { // 遞歸進入,高度和父級集合通過相同父級條件從已有數組中獲取,避免遞歸增加與實際數據不符 foreach ($colManage as $value) { if ($value['parent'] == $parent) { $parentList = $value['parentList']; $col = $value['height']; break; } } } // 單元格標識 $column = $this->getColumn($cursor) . $col; // 組裝單元格需要的各種信息 $colManage[$column] = [ 'title' => $head['title'], // 標題 'cursor' => $cursor, // 游標 'cursorEnd' => $cursor, // 結束游標 'height' => $col, // 高度 'width' => $this->defaultWidth, // 寬度 'mergeStart' => $column, // 合并開始標識 'hMergeEnd' => $column, // 橫向合并結束標識 'zMergeEnd' => $column, // 縱向合并結束標識 'parent' => $parent, // 父級標識 'parentList' => $parentList, // 父級集合 ]; if (isset($head['children']) && !empty($head['children']) && is_array($head['children'])) { // 有下級,高度加一 $col += 1; // 當前標識加入父級集合 $parentList[] = $column; $this->setHeaderNeedManage($head['children'], $col, $cursor, $colManage, $column, $parentList); } else { // 沒有下級,游標加一 $cursor += 1; } } return $colManage; } /** * 完善單元格合并信息 * @param array $colManage * @return mixed * @author LWW */ private function completeColMerge(array $colManage) { $this->maxHeight = max(array_column($colManage, 'height')); $parentManage = array_column($colManage, 'parent'); foreach ($colManage as $index => $value) { // 設置橫向合并結束范圍:存在父級集合,把所有父級的橫向合并結束范圍設置為當前單元格 if (!is_null($value['parent']) && !empty($value['parentList'])) { foreach ($value['parentList'] as $parent) { $colManage[$parent]['hMergeEnd'] = self::getColumn($value['cursor']) . $colManage[$parent]['height']; $colManage[$parent]['cursorEnd'] = $value['cursor']; } } // 設置縱向合并結束范圍:當前高度小于最大高度 且 不存在以當前單元格標識作為父級的項 $checkChildren = array_search($index, $parentManage); if ($value['height'] < $this->maxHeight && !$checkChildren) { $colManage[$index]['zMergeEnd'] = self::getColumn($value['cursor']) . $this->maxHeight; } } return $colManage; } /** * 合并單元格 * @param array $colManage * @param bool $filter * @author LWW */ private function queryMergeColumn(array $colManage,bool $filter) { foreach ($colManage as $value) { $this->fileObject->mergeCells("{$value['mergeStart']}:{$value['zMergeEnd']}", $value['title']); $this->fileObject->mergeCells("{$value['mergeStart']}:{$value['hMergeEnd']}", $value['title']); // 設置單元格需要的寬度 if ($value['cursor'] != $value['cursorEnd']) { $value['width'] = ($value['cursorEnd'] - $value['cursor'] + 1) * $this->defaultWidth; } // 設置列單元格樣式 $toColumnStart = self::getColumn($value['cursor']); $toColumnEnd = self::getColumn($value['cursorEnd']); $this->fileObject->setColumn("{$toColumnStart}:{$toColumnEnd}", $value['width']); } // 是否開啟過濾選項 if ($filter) { // 獲取最后的單元格標識 $filterEndColumn = self::getColumn(end($colManage)['cursorEnd']) . $this->maxHeight; $this->fileObject->autoFilter("A1:{$filterEndColumn}"); } } /** * 獲取單元格列標識 * @param int $num * @return string * @author LWW */ private function getColumn(int $num) { return Excel::stringFromColumnIndex($num); } }
代碼如下
/** * 導出測試 * @author LWW */ public function export() { $header = [ [ 'title' => '一級表頭1', 'children' => [ [ 'title' => '二級表頭1', ], [ 'title' => '二級表頭2', ], [ 'title' => '二級表頭3', ], ] ], [ 'title' => '一級表頭2' ], [ 'title' => '一級表頭3', 'children' => [ [ 'title' => '二級表頭1', 'children' => [ [ 'title' => '三級表頭1', ], [ 'title' => '三級表頭2', ], ] ], [ 'title' => '二級表頭2', ], [ 'title' => '二級表頭3', 'children' => [ [ 'title' => '三級表頭1', 'children' => [ [ 'title' => '四級表頭1', 'children' => [ [ 'title' => '五級表頭1' ], [ 'title' => '五級表頭2' ] ] ], [ 'title' => '四級表頭2' ] ] ], [ 'title' => '三級表頭2', ], ] ] ] ], [ 'title' => '一級表頭4', ], [ 'title' => '一級表頭5', ], ]; $data= []; // header頭規則 title表示列標題,children表示子列,沒有子列children可不寫或為空 for ($i = 0; $i < 100; $i++) { $data[] = [ '這是第'. $i .'行測試', '這是第'. $i .'行測試', '這是第'. $i .'行測試', '這是第'. $i .'行測試', '這是第'. $i .'行測試', '這是第'. $i .'行測試', '這是第'. $i .'行測試', '這是第'. $i .'行測試', '這是第'. $i .'行測試', '這是第'. $i .'行測試', '這是第'. $i .'行測試', '這是第'. $i .'行測試', '這是第'. $i .'行測試', ]; } $fileName = '很厲害的文件導出類'; $xlsWriterServer = new MultiFloorXlsWriterService(); $xlsWriterServer->setFileName($fileName, '這是Sheet1別名'); $xlsWriterServer->setHeader($header, true); $xlsWriterServer->setData($data); $xlsWriterServer->addSheet('這是Sheet2別名'); $xlsWriterServer->setHeader($header); //這里可以使用新的header $xlsWriterServer->setData($data); // 這里也可以根據新的header定義數據格式 $filePath = $xlsWriterServer->output(); // 保存到服務器 $xlsWriterServer->excelDownload($filePath); // 輸出到瀏覽器 }
導出效果
關于“PHP怎么使用xlswriter進行大數據的導入導出”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“PHP怎么使用xlswriter進行大數據的導入導出”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。