PHP怎么使用xlswriter进行大数据的导入导出?
发布时间:2022-07-09 12:37:23 所属栏目:PHP教程 来源:互联网
导读:PHP如何使用xlswriter进行大数据的导入导出?下面本篇文章给大家介绍一下PHP大数据xlswriter导入导出(最优数据化)的方法,希望对大家有所帮助! 本文介绍基于PHP扩展xlswriter的VtifulKernelExcel类可以支持无限层级的复杂表头导出!后续也可能会持续更新优
PHP如何使用xlswriter进行大数据的导入导出?下面本篇文章给大家介绍一下PHP大数据xlswriter导入导出(最优数据化)的方法,希望对大家有所帮助! 本文介绍基于PHP扩展xlswriter的VtifulKernelExcel类可以支持无限层级的复杂表头导出!后续也可能会持续更新优化 一、准备xlswriter扩展 1、windows系统: 到PECL网站下载符合自己本地PHP环境的ddl文件下载地址:https://pecl.php.net/package/xlswriter,并复制到PHP的扩展目录ext文件夹下,修改php.ini文件, 加上这行 extension=xlswriter 2、Linux系统: 使用命令安装 pecl install xlswriter php配置文件添加 extension = xlswriter.so 重启:php nginx 查看PHP安装xlswriter拓展 二、封装导出类文件(重点来了) <?php namespace AppServices; use VtifulKernelExcel; class MultiFloorXlsWriterService { // 默认宽度 private $defaultWidth = 16; // 默认导出格式 private $exportType = '.xlsx'; // 表头最大层级 private $maxHeight = 1; // 文件名 private $fileName = null; private $xlsObj; private $fileObject; private $format; /** * MultiFloorXlsWriterService constructor. * @throws AppExceptionsApiException */ public function __construct() { // 文件默认输出地址 $path = base_path().'/public/uploads/excel'; $config = [ 'path' => $path ]; $this->xlsObj = (new VtifulKernelExcel($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 VtifulKernelFormat($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 */ (编辑:我爱故事小小网_铜陵站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |