PHP Spreadsheet 导出 excel 服务封装
概述
SpreadSheet Packagist 仓库地址
PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine
1. 文件代码如下:
点击查看代码
| <?php |
| |
| use PhpOffice\PhpSpreadsheet\Spreadsheet; |
| use PhpOffice\PhpSpreadsheet\Writer\Xlsx; |
| |
| class ExcelExportService |
| { |
| |
| |
| |
| public $excel; |
| public $fileName; |
| public $rootPath; |
| public $storeDir = '/Files/Export/Tmp/'; |
| public $rowNum; |
| public $header = array(); |
| public $body = array(); |
| |
| public $fields = array(); |
| public $values = array(); |
| |
| public function __construct($fileName) |
| { |
| $this->excel = new Spreadsheet(); |
| |
| $arrFile = explode('.', $fileName); |
| $ext = $arrFile[1]; |
| $baseName = $arrFile[0]; |
| |
| $this->fileName = $baseName . '_' . time() . random_int(1000, 9999) . '.' . $ext; |
| $this->rowNum = 1; |
| } |
| |
| |
| |
| |
| |
| public function setRootPath($path = null) |
| { |
| if (!empty($path)) { |
| $this->rootPath = rtrim($path, '/'); |
| } else { |
| $this->rootPath = rtrim(fcommon::FilePath(), '/'); |
| } |
| } |
| |
| |
| |
| |
| |
| public function setStorePath($pathName) |
| { |
| if (!empty($pathName)) { |
| $this->storeDir = rtrim($pathName, '/') . '/'; |
| } else { |
| $this->storeDir = $this->storeDir . date('Y') . '/' . date('m') . '/'; |
| } |
| } |
| |
| |
| |
| |
| |
| public function setHeader($header) |
| { |
| $this->header = $header; |
| } |
| |
| |
| |
| |
| |
| public function setBody($body) |
| { |
| $this->body = $body; |
| } |
| |
| |
| |
| |
| |
| |
| public function writeRow($row) |
| { |
| $sheet = $this->excel->getActiveSheet(); |
| $column = 0; |
| foreach ($row as $k => $value) { |
| $sheet->setCellValueByColumnAndRow(++$column, $this->rowNum, $value); |
| } |
| |
| $this->rowNum++; |
| } |
| |
| |
| |
| |
| |
| public function writeHeader() |
| { |
| $this->writeRow($this->header); |
| } |
| |
| |
| |
| |
| |
| public function writeBody() |
| { |
| foreach ($this->body as $k => $row) { |
| |
| $filterRow = []; |
| foreach($this->fields as $field) |
| { |
| $value = $row[$field]; |
| if(isset($this->values[$field]) && is_callable($this->values[$field])) |
| { |
| $value = call_user_func($this->values[$field], $value, $row); |
| } |
| $filterRow[] = $value; |
| } |
| |
| $this->writeRow($filterRow); |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function parseColumns($columns) |
| { |
| $header = []; |
| $fields = []; |
| $values = []; |
| foreach ($columns as $k => $item) { |
| $fields[] = $k; |
| |
| if (is_string($item)) { |
| $header[] = $item; |
| |
| } elseif (is_array($item)) { |
| $header[] = $item['label']; |
| if(isset($item['value']) && is_callable($item['value'])) |
| { |
| $values[$k] = $item['value']; |
| } |
| } |
| } |
| |
| $this->header = $header; |
| $this->fields = $fields; |
| $this->values = $values; |
| } |
| |
| |
| |
| |
| |
| |
| public function setWidths($widths) |
| { |
| if(count($widths) == count($this->header)) |
| { |
| for($i=1; $i<=count($this->header); $i++) |
| { |
| $this->excel->getActiveSheet()->getColumnDimensionByColumn($i)->setWidth($widths[$i-1]); |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| public function setFilePath($path) |
| { |
| $this->setRootPath(); |
| $this->setStorePath($path); |
| $absPath = $this->rootPath . $this->storeDir . $this->fileName; |
| |
| $arrPath = pathinfo($absPath); |
| if (!is_dir($arrPath['dirname'])) { |
| mkdir($arrPath['dirname'], 0777, true); |
| } |
| |
| return $absPath; |
| } |
| |
| |
| |
| |
| |
| |
| |
| public function save($path = null) |
| { |
| if (empty($this->body)) { |
| throw new Exception('没有待导出的数据', 501); |
| } |
| |
| |
| $absPath = $this->setFilePath($path); |
| |
| |
| $this->writeHeader(); |
| |
| |
| $this->writeBody(); |
| |
| $write = new Xlsx($this->excel); |
| $write->save(fcommon::linuxStringUtf($absPath)); |
| |
| $this->excel->disconnectWorksheets(); |
| unset($this->excel); |
| } |
| |
| public function getFullFileUrl() |
| { |
| $baseUrl = fcommon::FileUrl(); |
| |
| $fileUrl = $baseUrl . $this->storeDir . $this->fileName; |
| |
| return $fileUrl; |
| } |
| } |
2. 使用方式如下:
| ... |
| |
| $fileName = '教师信息导入错误记录.xlsx'; |
| $columns = [ |
| 'name'=>'姓名', |
| 'idNumber'=>'身份证号', |
| 'error'=>'错误信息' |
| ]; |
| $excel = new ExcelExportService($fileName); |
| $widths = [10, 20, 60]; |
| |
| $excel->parseColumns($columns); |
| $excel->setBody($data); |
| $excel->setWidths($widths); |
| |
| $excel->save(); |
| |
| return $excel->getFullFileUrl(); |
| |
| ... |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步