PHP-Xlswriter 替代php excel 提升性能
仓库地址:https://github.com/viest/php-ext-xlswriter-doc/tree/master/zh-cn
Install
Unix
C/C++ Code复制内容到剪贴板
- pecl install xlswriter
# 添加 extension = xlswriter.so 到 ini 配置
读取文件暂不支持 windows 系统;
扩展版本大于等于 1.2.7;
PECL 安装时将会提示是否开启读取功能,请键入 yes;
安装完成后,可以输入命令查看已安装的扩展
PHP Code复制内容到剪贴板
- php -m
Windows
Benchmark
Test environment: Macbook Pro 13 inch, Intel Core i5, 16GB 2133MHz LPDDR3 Memory, 128GB SSD Storage.
Export
Two memory modes export 1 million rows of data (27 columns, data is string)
- Normal mode: only 29S is needed, and the memory only needs 2083MB;
- Fixed memory mode: only need 52S, memory only needs <1MB;
Import
1 million rows of data (1 columns, data is inter)
- Full mode: Just 3S, the memory is only 558MB;
- Cursor mode: Just 2.8S, memory is only <1MB;
PHP Code复制内容到剪贴板
- composer require viest/php-ext-xlswriter-ide-helper:dev-master
宝塔面板安装php扩展,xlswriter
1、ssh进入,略过
2、下载包
C/C++ Code复制内容到剪贴板
- cd /www/server/php/[php版本]/bin
- sudo wget https://github.com/viest/php-ext-xlswriter/archive/master.zip
- sudo unzip master.zip
- rm -f master.zip
- cd php-ext-xlswriter-master/
读取excel文件示例:
PHP Code复制内容到剪贴板
- /**
- * 解析excel资源并入库解析结果(由C封装的php扩展,内存优化模式)
- * @param $attid
- * @param int $headerRow
- * @param int $dataStartRow
- * @return ExcelRead
- * @throws HttpException
- */
- protected function insertParsingRecordByC($attid, $headerRow = 1, $dataStartRow = 1)
- {
- // 资源文件
- $attModel = Attachment::findOne(["id" => $attid]);
- if (!$attModel) {
- throw new HttpException(400, "上传记录不存在");
- }
- $filePath = Yii::$app->storage->getFullPath($attModel->path);
- if (!file_exists($filePath)) {
- throw new HttpException(400, "资源文件不存在");
- }
- // path是指文件目录
- $config = ['path' => Yii::getAlias("@storagePath") . "/upload"];
- $excel = new \Vtiful\Kernel\Excel($config);
- // 读取文件
- $data = $excel->openFile($attModel->path)
- ->openSheet()
- ->getSheetData();
- // // 打开文件,获取工作表数量
- // $sheetList = $excel->openFile('tutorial.xlsx')
- // ->sheetList();
- // foreach ($sheetList as $sheetName) {
- // echo 'Sheet Name:' . $sheetName . PHP_EOL;
- // // 通过工作表名称获取工作表数据
- // $sheetData = $excel
- // ->openSheet($sheetName)
- // ->getSheetData();
- // //var_dump($sheetData);
- // }
- $headerRow -= 1; //从0开始
- $dataStartRow -= 1; // 从0开始,都会依次减掉一行
- $allRow = count($data); // 总行数
- foreach ($data[$headerRow] as $k => $h) {
- $getValue = $data[$dataStartRow][$k]; //取第一行数据,看数据类型是有无值
- if ($getValue) {
- // 从第一行数据行取,如果有值的话
- // 1代表 字符型 2代表 数值型
- $excelHeader[] = [
- "index" => $k,
- "dataType" => $getValue ? (is_numeric($getValue) ? 2 : 1) : "",
- "dataTypeName" => $getValue ? (is_numeric($getValue) ? "数值型" : "字符型") : "",
- "type" => $getValue ? (is_numeric($getValue) ? "integer" : "string") : "",
- "size" => $getValue ? (is_numeric($getValue) ? "11" : "255") : "",
- "dbType" => $getValue ? (is_numeric($getValue) ? "int(11)" : "varchar(255)") : "",
- "defaultValue" => null,
- "isPrimaryKey" => false,
- "name" => $h,
- "target" => $h, //别名
- ];
- } else {
- $excelHeader[] = [
- "index" => $k,
- "dataType" => 0,
- "dataTypeName" => "",
- "type" => "",
- "size" => "",
- "dbType" => "",
- "defaultValue" => null,
- "isPrimaryKey" => false,
- "name" => "",
- "target" => "", //别名
- ];
- }
- }
- // 暂只获取第一个工作表
- for ($currentRow = $dataStartRow; $currentRow <= $allRow; $currentRow++) {
- $excelDatas[] = $data[$currentRow];
- }
- $time = time();
- $params = [
- 'user_id' => Yii::$app->user ? Yii::$app->user->id : 0,
- 'attachment_id' => $attid,
- 'excel_head' => Json::encode($excelHeader),
- 'excel_data' => Json::encode($excelDatas),
- 'status' => '0',
- 'error' => '',
- 'mark' => '',
- 'created_at' => $time,
- 'updated_at' => $time,
- ];
- $model = new self();
- $model->setAttributes($params);
- if ($model->save()) {
- return $model;
- } else {
- throw new HttpException(400, current($model->firstErrors));
- }
- }
上一篇 根据父级ID查询出全部子级
下一篇 php7安装imagick扩展