yakub/yxel

此类帮助读取和创建新的 csv/xlsx 文件。针对简单但大的电子表格进行了优化。

v1.2 2020-10-28 13:12 UTC

This package is auto-updated.

Last update: 2024-09-28 21:06:10 UTC


README

此类帮助读取和创建新的 csv/xlsx 文件。针对简单但大的电子表格进行了优化。

构建状态

master: Build Status Coverage Status

dev: Build Status Coverage Status

安装

推荐通过 Composer 安装

composer require yakub/yxel

设置

类使用临时目录创建新文件或读取 xlsx 文件。路径可以更改,但脚本必须具有在该文件夹中写入的权限。

\Yakub\Yxel\Main::setCreatingDir('/my/path/to/creating');

CSV

简单处理 csv 文件,其中自动检测单元格之间的分隔符

读取 csv 文件

$read = \Yakub\Yxel\Main::read('path/to/file.csv');

$read->getRows(function ($data, $row) {
	echo $row.' -> '.json_encode($data).'<br >';
});

创建 csv 文件

$write = \Yakub\Yxel\Main::write('yxel_test', \Yakub\Yxel\Main::CSV);

$write->addRow(['A1', 'B1', '']);
$write->addRow(['A2', '', 'C2']);

$write->close();

// Return full path to file
$patToFile = $write->getFilePath();

XLSX

简单处理 xlsx 文件,只读取第一个工作表

读取 xlsx 文件

$read = \Yakub\Yxel\Main::read('path/to/file.xlsx');

$read->getRows(function ($data, $row) {
	echo $row.' -> '.json_encode($data).'<br >';
});

创建 xlsx 文件

$write = \Yakub\Yxel\Main::write('yxel_test', \Yakub\Yxel\Main::XLSX);

$write->addRow(['A1', 'B1', '']);
$write->addRow(['A2', '', 'C2']);

$write->close();

// Return full path to file
$patToFile = $write->getFilePath();

批量使用

写入可以停止并稍后继续或在另一个进程中继续

$write = \Yakub\Yxel\Main::write('yxel_test', \Yakub\Yxel\Main::CSV);

$write->addRow(['A1', 'B1', '']);
$write->addRow(['A2', '', 'C2']);

// Instead of close use save. This function only save new data but file is still able to get new rows. Also this help clean memory.
// After save script can end and data will not be lost
$write->save();

在另一个进程中只需打开现有文件

// Name of file must be same
$write = \Yakub\Yxel\Main::write('yxel_test', \Yakub\Yxel\Main::CSV);

// Add new row to previous in this file
$write->addRow(['', 'B3', 'C3']);

// After close can't open this file again. If is used same name then file will be rewrited with new data
$write->close();

为了进程间的通信可以使用存储自定义数据

$write = \Yakub\Yxel\Main::write('yxel_test', \Yakub\Yxel\Main::CSV);

$write->addRow(['A1', 'B1', '']);
$write->addRow(['A2', '', 'C2']);

$write->settings('row_number', 3);
$write->save();

// ------- New process ------- //

$write = \Yakub\Yxel\Main::write('yxel_test', \Yakub\Yxel\Main::CSV);
$row = $write->settings('row_number');

$write->addRow(['', 'B'.$row, 'C'.$row]);

$write->close();