#yyds干货盘点#Hyperf结合PhpOffice/PhpSpreadsheet实现Excel&CSV文件导出导入

古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。这篇文章主要讲述#yyds干货盘点#Hyperf结合PhpOffice/PhpSpreadsheet实现Excel&CSV文件导出导入相关的知识,希望能为你提供帮助。

本文环境 Hyperf2.1,php7.3,mysql5.7
不懂的可以评论或联系我邮箱:owen@owenzhang.com
Hyperf & PhpSpreadsheet介绍Hyperf 介绍
Hyperf 是基于  ??Swoole 4.5+???  实现的高性能、高灵活性的 PHP 协程框架,内置协程服务器及大量常用的组件,性能较传统基于  ??PHP-FPM???  的框架有质的提升,提供超高性能的同时,也保持着极其灵活的可扩展性,标准组件均基于  ??PSR 标准???  实现,基于强大的依赖注入设计,保证了绝大部分组件或类都是  ??可替换???  与  ??可复用??  的。
PhpOffice/PhpSpreadsheet 介绍
PhpSpreadsheet是一个用纯PHP编写的库,它提供了一组类,允许您读取和写入各种电子表格文件格式,如Excel和LibreOffice Calc。
PhpSpreadsheet是PHPExcel的下一个版本。它破坏了兼容性,从而大大提高了代码库质量(命名空间、PSR 合规性、使用最新的 PHP 语言功能等)。
因为所有的努力都转移到了PhpSpreadsheet上,PHPExcel将不再被维护。所有对 PHPExcel 的贡献、补丁和新功能都应针对 PhpSpreadsheet 分支。??master??
  • GitHub  ??PHPOffice/PhpSpreadsheet:用于读取和写入电子表格文件的纯PHP库 (github.com)??
  • 官方使用文档    ??Welcome to PhpSpreadsheet’s documentation - PhpSpreadsheet Documentation??
  • api文档  ??Documentation (phpoffice.github.io)??
PhpOffice/PhpSpreadsheet 安装使用??composer??将 PhpSpreadsheet 安装到你的项目中:
1.composer require phpoffice/phpspreadsheet

或者,如果您计划使用它们,还可以下载文档和示例:
1.composer require phpoffice/phpspreadsheet --prefer-source


文件导出导入& 代码实例csv文件导出
导出实例类文件函数说明:使用hyperf框架的跨域中间件
  • ??-> withHeader??  添加浏览器响应头
  • ??-> withBody??  添加浏览器内容主体
  • ??Headers??  可以根据实际情况进行改写。
代码实例:
< ?php
/**
* Created by PhpStorm.
* Created by OwenZhang at 2021/11/8 14:39
*/

namespace App\\Common;

use Hyperf\\HttpMessage\\Stream\\SwooleStream;
use Hyperf\\HttpServer\\Response;

class Csv

/**
$head = [name=> 名字,score=> 得分];
$data = https://www.songbingjia.com/android/[
[name => 张三, score => 80],
[name => 李四, score => 90],
[name => 王五, score => 60],
];
$fileName = 测试
*/
/**
* Describe:导数数据 (csv 格式)
* @param array $head
* @param array $body
* @param string $fileName 测试.csv,测试.xlsx
* @return \\Psr\\Http\\Message\\ResponseInterface
* Created by owenzhang at 2021/11/8 14:47
*/
static function export(array $head, array $body, string $fileName)

$head_keys = array_keys($head);
$head_values = array_values($head);
$fileData = https://www.songbingjia.com/android/self::utfToGbk(implode(,, $head_values)) ."\\n";

foreach ($body as $value)
$temp_arr = [];
foreach ($head_keys as $key)
$temp_arr[] = $value[$key] ?? ;

$fileData .= self::utfToGbk(implode(,, $temp_arr)) . "\\n";

$response = new Response();
$contentType = text/csv;
return $response-> withHeader(content-description, File Transfer)
-> withHeader(content-type, $contentType)
-> withHeader(content-disposition, "attachment; filename=$fileName")
-> withHeader(content-transfer-encoding, binary)
-> withHeader(pragma, public)
-> withBody(new SwooleStream($fileData));


/**
* 字符转换(utf-8 => GBK)
* @param $data
* @return false|string
*/
static function utfToGbk($data)

return mb_convert_encoding($data,"GBK","UTF-8");
# return iconv(utf-8, GBK, $data);




调用导出实例函数方法调用上面的csv文件导出类,浏览器调整新页面直接下载导出。
代码实例:
< ?php
/**
* Describe: 列表导出
* Route: get /admin/badword_list_export
* Created by OwenZhang at 2021/12/13 10:14
*/
public function getBadwordListExport(): ResponseInterface

$page = (int)$this-> request-> input(page, 1);
$pageSize = (int)$this-> request-> input(page_size, 15);
$word = (string)$this-> request-> input(word, );
$type = (string)$this-> request-> input(type, );

$container = ApplicationContext::getContainer();
$exportArray = $container-> get(BadwordServiceInterface::class)-> getBadwordListExport($page, $pageSize, $word, $type);
//$exportArray 数组,每个里面键值和$header一样

set_time_limit(0);
ini_set(memory_limit, 5048M);
$filename = 敏感词数据 . date(ymdHis);

$header = [
badword_id => 敏感词id,
word => 敏感词,
type => 分类,
style_name => 应用区域,
replace_word => 替换词,
];

return Csv::export($header, $exportArray, $filename);


excel文件导出
导出实例类文件
  1. xls后缀excel文件导出的heard头:
    ???xls=application/vnd.ms-excel??
  2. xlsx后缀excel文件导出的heard头:
    ???xlsx=application/vnd.openxmlformats-officedocument.spreadsheetml.sheet??
函数说明:
  • 构造函数 创建一个PhpSpreadsheet实例  ??__construct()??
  • 设置表头  ??$title=[id,标题,内容] setHeader($title)??
  • 添加表内容??$data=https://www.songbingjia.com/android/[ [1,标题1,内容1], [2,标题2,内容2], ... ] addData($data)??
  • 保存到服务器本地  ??$fileName=文件名 saveToLocal($fileName)??
  • 直接从浏览器下载到本地,有问题,不使用,??php://output??  目前PhpSpreadsheet插件有问题,PhpSpreadsheet插件作者还在修复  ??saveToBrowser($fileName)??
  • 保存临时文件在从浏览器自动下载到本地  ??saveToBrowserByTmp($fileName)??
【#yyds干货盘点#Hyperf结合PhpOffice/PhpSpreadsheet实现Excel&CSV文件导出导入】代码实例:
< ?php
/**
* Created by PhpStorm.
* Created by OwenZhang at 2021/12/28 14:39
*/

namespace App\\Common;

use Hyperf\\HttpMessage\\Stream\\SwooleStream;
use Hyperf\\HttpServer\\Response;
use PhpOffice\\PhpSpreadsheet\\IOFactory;
use PhpOffice\\PhpSpreadsheet\\Spreadsheet;

class ExportExcelHandle

private $sheet;
private

    推荐阅读