這篇文章主要介紹了PHP怎么使用xlswriter進(jìn)行大數(shù)據(jù)的導(dǎo)入導(dǎo)出的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇PHP怎么使用xlswriter進(jìn)行大數(shù)據(jù)的導(dǎo)入導(dǎo)出文章都會(huì)有所收獲,下面我們一起來看看吧。
站在用戶的角度思考問題,與客戶深入溝通,找到太倉(cāng)網(wǎng)站設(shè)計(jì)與太倉(cāng)網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、網(wǎng)頁(yè)空間、企業(yè)郵箱。業(yè)務(wù)覆蓋太倉(cāng)地區(qū)。
1、windows系統(tǒng):
到PECL網(wǎng)站下載符合自己本地PHP環(huán)境的ddl文件,并復(fù)制到PHP的擴(kuò)展目錄ext文件夾下,修改php.ini文件,
加上這行
extension=xlswriter
2、Linux系統(tǒng):
使用命令安裝
pecl install xlswriter
php配置文件添加
extension = xlswriter.so
重啟:php nginx 查看PHP安裝xlswriter拓展
$path ]; $this->xlsObj = (new \Vtiful\Kernel\Excel($config)); } /** * 設(shè)置文件名 * @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())); } /** * 設(shè)置表頭 * @param array $header * @param bool $filter * @throws \Exception * @author LWW */ public function setHeader(array $header, bool $filter = false) { if (empty($header)) { throw new \Exception('表頭數(shù)據(jù)不能為空'); } if (is_null($this->fileName)) { self::setFileName(time()); } // 獲取單元格合并需要的信息 $colManage = self::setHeaderNeedManage($header); // 完善單元格合并信息 $colManage = self::completeColMerge($colManage); // 合并單元格 self::queryMergeColumn($colManage, $filter); } /** * 填充文件數(shù)據(jù) * @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); } /** * 保存文件至服務(wù)器 * @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('表頭數(shù)據(jù)格式有誤'); } if (is_null($parent)) { // 循環(huán)初始化 $parentList = []; $col = 1; } else { // 遞歸進(jìn)入,高度和父級(jí)集合通過相同父級(jí)條件從已有數(shù)組中獲取,避免遞歸增加與實(shí)際數(shù)據(jù)不符 foreach ($colManage as $value) { if ($value['parent'] == $parent) { $parentList = $value['parentList']; $col = $value['height']; break; } } } // 單元格標(biāo)識(shí) $column = $this->getColumn($cursor) . $col; // 組裝單元格需要的各種信息 $colManage[$column] = [ 'title' => $head['title'], // 標(biāo)題 'cursor' => $cursor, // 游標(biāo) 'cursorEnd' => $cursor, // 結(jié)束游標(biāo) 'height' => $col, // 高度 'width' => $this->defaultWidth, // 寬度 'mergeStart' => $column, // 合并開始標(biāo)識(shí) 'hMergeEnd' => $column, // 橫向合并結(jié)束標(biāo)識(shí) 'zMergeEnd' => $column, // 縱向合并結(jié)束標(biāo)識(shí) 'parent' => $parent, // 父級(jí)標(biāo)識(shí) 'parentList' => $parentList, // 父級(jí)集合 ]; if (isset($head['children']) && !empty($head['children']) && is_array($head['children'])) { // 有下級(jí),高度加一 $col += 1; // 當(dāng)前標(biāo)識(shí)加入父級(jí)集合 $parentList[] = $column; $this->setHeaderNeedManage($head['children'], $col, $cursor, $colManage, $column, $parentList); } else { // 沒有下級(jí),游標(biāo)加一 $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) { // 設(shè)置橫向合并結(jié)束范圍:存在父級(jí)集合,把所有父級(jí)的橫向合并結(jié)束范圍設(shè)置為當(dāng)前單元格 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']; } } // 設(shè)置縱向合并結(jié)束范圍:當(dāng)前高度小于最大高度 且 不存在以當(dāng)前單元格標(biāo)識(shí)作為父級(jí)的項(xiàng) $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']); // 設(shè)置單元格需要的寬度 if ($value['cursor'] != $value['cursorEnd']) { $value['width'] = ($value['cursorEnd'] - $value['cursor'] + 1) * $this->defaultWidth; } // 設(shè)置列單元格樣式 $toColumnStart = self::getColumn($value['cursor']); $toColumnEnd = self::getColumn($value['cursorEnd']); $this->fileObject->setColumn("{$toColumnStart}:{$toColumnEnd}", $value['width']); } // 是否開啟過濾選項(xiàng) if ($filter) { // 獲取最后的單元格標(biāo)識(shí) $filterEndColumn = self::getColumn(end($colManage)['cursorEnd']) . $this->maxHeight; $this->fileObject->autoFilter("A1:{$filterEndColumn}"); } } /** * 獲取單元格列標(biāo)識(shí) * @param int $num * @return string * @author LWW */ private function getColumn(int $num) { return Excel::stringFromColumnIndex($num); } }
代碼如下
/** * 導(dǎo)出測(cè)試 * @author LWW */ public function export() { $header = [ [ 'title' => '一級(jí)表頭1', 'children' => [ [ 'title' => '二級(jí)表頭1', ], [ 'title' => '二級(jí)表頭2', ], [ 'title' => '二級(jí)表頭3', ], ] ], [ 'title' => '一級(jí)表頭2' ], [ 'title' => '一級(jí)表頭3', 'children' => [ [ 'title' => '二級(jí)表頭1', 'children' => [ [ 'title' => '三級(jí)表頭1', ], [ 'title' => '三級(jí)表頭2', ], ] ], [ 'title' => '二級(jí)表頭2', ], [ 'title' => '二級(jí)表頭3', 'children' => [ [ 'title' => '三級(jí)表頭1', 'children' => [ [ 'title' => '四級(jí)表頭1', 'children' => [ [ 'title' => '五級(jí)表頭1' ], [ 'title' => '五級(jí)表頭2' ] ] ], [ 'title' => '四級(jí)表頭2' ] ] ], [ 'title' => '三級(jí)表頭2', ], ] ] ] ], [ 'title' => '一級(jí)表頭4', ], [ 'title' => '一級(jí)表頭5', ], ]; $data= []; // header頭規(guī)則 title表示列標(biāo)題,children表示子列,沒有子列children可不寫或?yàn)榭? for ($i = 0; $i < 100; $i++) { $data[] = [ '這是第'. $i .'行測(cè)試', '這是第'. $i .'行測(cè)試', '這是第'. $i .'行測(cè)試', '這是第'. $i .'行測(cè)試', '這是第'. $i .'行測(cè)試', '這是第'. $i .'行測(cè)試', '這是第'. $i .'行測(cè)試', '這是第'. $i .'行測(cè)試', '這是第'. $i .'行測(cè)試', '這是第'. $i .'行測(cè)試', '這是第'. $i .'行測(cè)試', '這是第'. $i .'行測(cè)試', '這是第'. $i .'行測(cè)試', ]; } $fileName = '很厲害的文件導(dǎo)出類'; $xlsWriterServer = new MultiFloorXlsWriterService(); $xlsWriterServer->setFileName($fileName, '這是Sheet1別名'); $xlsWriterServer->setHeader($header, true); $xlsWriterServer->setData($data); $xlsWriterServer->addSheet('這是Sheet2別名'); $xlsWriterServer->setHeader($header); //這里可以使用新的header $xlsWriterServer->setData($data); // 這里也可以根據(jù)新的header定義數(shù)據(jù)格式 $filePath = $xlsWriterServer->output(); // 保存到服務(wù)器 $xlsWriterServer->excelDownload($filePath); // 輸出到瀏覽器 }
導(dǎo)出效果
關(guān)于“PHP怎么使用xlswriter進(jìn)行大數(shù)據(jù)的導(dǎo)入導(dǎo)出”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“PHP怎么使用xlswriter進(jìn)行大數(shù)據(jù)的導(dǎo)入導(dǎo)出”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。