PHPackages                             yuxiaobo/simple-excel - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. yuxiaobo/simple-excel

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

yuxiaobo/simple-excel
=====================

简单的 excel 导入导出工具包

0.1.4(3mo ago)11621MITPHPPHP ^7.3 || ^8CI failing

Since Jan 23Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/edk24/simple-excel)[ Packagist](https://packagist.org/packages/yuxiaobo/simple-excel)[ RSS](/packages/yuxiaobo-simple-excel/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependencies (4)Versions (15)Used By (0)

简单 Excel 导入导出工具库
================

[](#简单-excel-导入导出工具库)

🌈 简单使用， 方便快捷。 支持 `xls` `xlsx` `csv` 🌸

[![示例](./docs/export.png)](./docs/export.png)

🎉 使用方法
------

[](#-使用方法)

环境要求

- PHP &gt;= 7.3
- PhpSpreadsheet &gt;= 1.19

安装

```
$ composer require yuxiaobo/simple-excel
```

导入

```
$arr = SimpleExcel::import(dirname(__DIR__) . '/test/test.xlsx', 'xlsx', array(
    '姓名'      => 'name',
    '年龄'      => 'age',
    '性别'      => 'gender'
));
var_dump($arr);
```

导出到文件

```
SimpleExcel::export('/tmp/dump.xlsx', 'xlsx', [
    'name'      => '姓名',
    'idcard'    => '身份证',
    'mobile'    => '手机号'
], [
    ['name' => '张三', 'idcard' => '522131199703213342', 'mobile'=>'18311548011'],
    ['name' => '李四', 'idcard' => '522131199703213342', 'mobile' => '18311548011'],
    ['name' => '赵五', 'idcard' => '522131199703213342', 'mobile' => '18311548011'],
],'#ff0000', '#00ff00', '#333333');
```

导出并下载数据

```
SimpleExcel::setDownloadHeader('导出数据.xlsx'); // 设置下载文件响应头
SimpleExcel::export('php://output', 'xlsx', [
    'name'      => '姓名',
    'idcard'    => '身份证',
    'mobile'    => '手机号'
], [
    ['name' => '张三', 'idcard' => '522131199703213342', 'mobile'=>'18311548011'],
    ['name' => '李四', 'idcard' => '522131199703213342', 'mobile' => '18311548011'],
    ['name' => '赵五', 'idcard' => '522131199703213342', 'mobile' => '18311548011'],
],'#ff0000', '#00ff00', '#333333');
```

> 注意：当 `fileName` 为 `php://output` 且导出过程发生异常时，库会自动切换为 `text/html` 并输出友好的错误页面，不再继续下载 xlsx。
> 如果导出到本地文件路径（如 `/tmp/demo.xlsx`），异常仍会继续抛出。

性能测试

1. 此库的 `xls/xlsx` 基于的 `PhpSpreadsheet` 实现，且导出表格有着色处理。建议5w行100列以内的数据处理
2. CSV单独做了优化处理, 100000行100列导入导出测试结果如下
    - export: `0.9 sec, 1.6 gb`
    - import: `3.92 sec, 1.6gb`

默认单元测试会跳过上述压测用例，如需执行可使用：`./vendor/bin/phpunit -c phpunit.xml --group performance`

✅️ 常见问题
-------

[](#️-常见问题)

**1. 导入或导出几十万条数据失败**

- ① 游标分批查询数据导出
- ② 数据量较大建议做成异步队列，避免影响主业务
- ③ 少用模型，多用大 DB 查数据（以 thinkphp为例）能明显优化内存与任务耗时
- ④ 只查询需要的字段
- ⑤ 耗时任务强烈建议做成异步队列的方式！！！不要犟，避免影响主业务

**2. 内存超出使用限制**

`Allowed memory size of 8388608 bytes exhausted (tried to allocate 1298358 bytes)`

上述问题是由于内存不足导致的，可能是有死循环，或者代码对内存的使用量比较大。

临时解决办法：`ini_set('memory_limit', '256M');`

**3. 请求超时**

临时解决：`ini_set('max_execution_time', 600);`

> Tips: 也需要注意一下 nginx 的最大保持 http 请求时间

其他优化办法:

- (1). 可以临时通过增加 php 执行时间解决 `ini_set('max_execution_time', 600);`
- (2). 忘记说了，也需要对应增加 nginx/apache 的 http 请求保持时间，它在你的上层～
- (3). 耗时任务强烈建议做成异步队列的方式！！！不要犟，避免影响主业务

**4. 数字太长变成 E+**

导出时在前面加个单元号或者反单元号，如身份证：`522131199703253412` 改成 `'522131199703253412`

> `0.1.3起` 导出单元格数据强制转换字符串，避免出现科学计算法

**5. 大数据导出**

> 建议: 使用 csv 替代 xlsx，`xls 最大行 65536，xlsx 最大行 1048576（一百万）`, csv 没有限制

- ① 本库的 csv 做过优化，[100 000 rows and 100 columns测试](#%E6%80%A7%E8%83%BD%E6%B5%8B%E8%AF%95) 基本足够大部分使用场景了
- ② 如果你对内存和速度有更高的追求，我这边有几个推荐。
    - 1. 直接用 php 的 `fputcsv` 和 `fgetcsv`，使用 chunk 分批查询（如果你的框架支持）
    - 2. 使用 PHP 扩展 [xlswriter](https://xlswriter-docs.viest.me/zh-cn) [(感谢 @bugsnail @langziyang @QlanQ 推荐)](https://www.v2ex.com/t/1045263#reply5)
- ③ 导出几百万上千万的数据，怎么打开？用 `UltraEdit` 即可，很🐂🍺的

**6. 空行忽略**

遇到空行数据默认是跳过的

可以通过 simpleExcel::import() 函数的参数4 `ignoreEmptyRow` 设置

**7. 导入遇日期时间为小数问题**

自行通过 `\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject(0.375)->format('Y-m-d H:i:s')` 转换即可;

在 Excel 中，日期时间是一个小数，例如 `09:00:00` 对应的小数为 `0.375`;

- `09:00` 9 = 9/24 = 0.375
- `12:30` (12 + 30/60) = 12.5 = 12.5/24 = 0.5208333333333334

```
# php简单实现 (备用方案)
function excelTimeToTimeStringSimple(int $excelTime) {
    // Excel的基准日期是1900-01-00，加上天数得到时间戳
    // 86400 = 24 * 60 * 60 (一天的秒数)
    $timestamp = $excelTime * 86400;
    return gmdate("H:i:s", (int)round($timestamp));
}
// excelTimeToTimeStringSimple(0.375) => 09:00:00
```

💡意见反馈
-----

[](#意见反馈)

如果您在使用过程中遇到了问题，请先反馈 issue

着急使用的话请联系 绿泡泡：Base1024

或是 `fork` 一份自己改，项目遵循 MIT 开源协议

请务必反馈 issue

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance81

Actively maintained with recent releases

Popularity17

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity35

Early-stage or recently created project

 Bus Factor1

Top contributor holds 96.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~60 days

Recently: every ~165 days

Total

14

Last Release

102d ago

PHP version history (3 changes)0.0.4PHP ^7.4 || ^8.0

0.0.7PHP ^7 || ^8

0.0.8PHP ^7.3 || ^8

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/32519482?v=4)[余晓波](/maintainers/edk24)[@edk24](https://github.com/edk24)

---

Top Contributors

[![edk24](https://avatars.githubusercontent.com/u/32519482?v=4)](https://github.com/edk24 "edk24 (28 commits)")[![J2zl](https://avatars.githubusercontent.com/u/23160330?v=4)](https://github.com/J2zl "J2zl (1 commits)")

---

Tags

exportexcelxlsximportphpspreadsheet

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/yuxiaobo-simple-excel/health.svg)

```
[![Health](https://phpackages.com/badges/yuxiaobo-simple-excel/health.svg)](https://phpackages.com/packages/yuxiaobo-simple-excel)
```

###  Alternatives

[maatwebsite/excel

Supercharged Excel exports and imports in Laravel

12.9k157.3M894](/packages/maatwebsite-excel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
