1、PhpSpreadsheet 是什么
PhpSpreadsheet由来:
phpexcel 由于版本陈旧性能低下 官方放弃维护
转而开发PhpSpreadsheet 用了最新得psr标准因而 对php版本不向下兼容 需要注意!。
PhpSpreadsheet是一个用纯PHP编写的库,提供了一组类,使您可以读取和写入不同的电子表格文件格式
PhpSpreadsheet提供了丰富的API接口,可以设置诸多单元格以及文档属性,包括样式、图片、日期、函数等等诸多应用,总之你想要什么样的Excel表格,PhpSpreadsheet都能做到
使用 PhpSpreadsheet 开发的PHP要求 7.1或更高版本
PhpSpreadsheet 支持链式操作
composer require phpoffice/phpspreadsheet
4、封装类
ExcelSpeadsheet.php
<?php namespace app\common\library; use Exception; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use think\Log; /** * 导出Excel */ class ExcelSpeadsheet { /** * @param array $header 表头 * @param array $body 表内容 */ public function export($header, $body, $filename) { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); //表头 //设置单元格内容 $titCol = 'A'; //宽度 $width = [45, 15, 15, 40, 10, 20, 40, 40]; foreach ($header as $key => $value) { // 单元格内容写入 $sheet->setCellValue($titCol . '1', $value); $sheet->getColumnDimension($titCol)->setWidth($width[$key]); $titCol++; } $row = 2; // 从第二行开始 foreach ($body as $item) { $dataCol = 'A'; foreach ($item as $value) { // 单元格内容写入 try { $sheet->setCellValue($dataCol . $row, $value); } catch (Exception $e) { Log::write('excel生成错误--row:' . $row . '。value:' . json_encode($value) . "。错误信息:" . $e->getMessage()); } $dataCol++; } $row++; } $writer = new Xlsx($spreadsheet); $writer->save($filename); } /** * @param array $header 表头 * @param array $body 表内容 */ public function group_export($header, $body, $row_width = null, $filename) { $spreadsheet = new Spreadsheet(); foreach ($body as $u_key => $u_value) { if ($u_key == 0) { $sheet = $spreadsheet->getActiveSheet(); } else { $sheet = $spreadsheet->createSheet($u_key + 1); } $sheet->setTitle($this->set_sheet_title($u_value)); //表头 //设置单元格内容 $titCol = 'A'; //宽度 $width = $row_width; foreach ($header as $key => $value) { $this_width = 35; if (isset($width[$key])) { $this_width = $width[$key]; } // 单元格内容写入 $sheet->setCellValue($titCol . '1', $value); $sheet->getColumnDimension($titCol)->setWidth($this_width); $titCol++; } $row = 2; // 从第二行开始 foreach ($u_value['content'] as $item) { $dataCol = 'A'; foreach ($item as $value) { // 单元格内容写入 try { if( $value && strpos($value,'=') === 0 ){$value = "'".$value;} if( $value && strpos($value,'>') === 0 ){$value = "'".$value;} if( $value && strpos($value,'<') === 0 ){$value = "'".$value;} $sheet->setCellValue($dataCol . $row, $value); $sheet->getStyle($dataCol.$row)->getAlignment()->setWrapText(true); } catch (Exception $e) { Log::write('excel生成错误--row:' . $row . '。value:' . json_encode($value) . "。错误信息:" . $e->getMessage()); } $dataCol++; } $row++; } } $writer = new Xlsx($spreadsheet); $writer->save($filename); return true; } private function set_sheet_title($content) { $user_info = $content['user_info']; $user_name = $user_info['user_name']; $user_type = $user_info['user_type']; $user_type_string = ''; switch ($user_type) { case 1: $user_type_string = '员工'; break; case 2: $user_type_string = '客户'; break; case 99: $user_type_string = '群聊'; break; default: $user_type_string = '未知'; } return $user_type_string.'-'.$user_name; } /** * @param array $header 表头 * @param array $body 表内容 */ public function limit_word_export($header, $body, $row_width = null, $filename) { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); //表头 //设置单元格内容 $titCol = 'A'; //宽度 $width = [45, 15, 15, 40, 10, 20, 40, 40]; foreach ($header as $key => $value) { // 单元格内容写入 $sheet->setCellValue($titCol . '1', $value); $sheet->getColumnDimension($titCol)->setWidth($row_width[$key]); $titCol++; } $row = 2; // 从第二行开始 foreach ($body as $item) { $dataCol = 'A'; foreach ($item as $value) { // 单元格内容写入 try { $sheet->setCellValue($dataCol . $row, $value); } catch (Exception $e) { Log::write('excel生成错误--row:' . $row . '。value:' . json_encode($value) . "。错误信息:" . $e->getMessage()); } $dataCol++; } $row++; } $writer = new Xlsx($spreadsheet); $writer->save($filename); return true; } }
5、调用
$sp = new ExcelSpeadsheet(); //把数据整理成需要下载的文件格式 $export = new MsgFileExport(); $excel_data = $export->assembly_data($data); $header = [ '文件名称', '聊天类型', '状态', '发送者', '接收者', '文件大小', '下载时间' ]; $dir = 'upload/'.date("Ymd",time()); if (!is_dir($dir)) { mkdir($dir, 0777, true); } $filename = $dir.'/文件导出' . md5(date('Y-m-d H:i:s')) . '.xlsx'; $sp->export($header, $excel_data, $filename); $fullname = '/' . $filename; return output(200, '聊天文件', ['filepath' => $fullname]);
林外听秋风