PHPackages                             moxuandi/yii2-phpexcel - 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. moxuandi/yii2-phpexcel

ActiveYii2-extension[PDF &amp; Document Generation](/categories/documents)

moxuandi/yii2-phpexcel
======================

PHPExcel 是一个用来读写 Excel 表格(OpenXML) 文件的 PHP 库。

v1.0(7y ago)037MIT

Since Oct 3Pushed 7y agoCompare

[ Source](https://github.com/moxuandi/yii2-phpexcel)[ Packagist](https://packagist.org/packages/moxuandi/yii2-phpexcel)[ RSS](/packages/moxuandi-yii2-phpexcel/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (2)Versions (3)Used By (0)

[PHPExcel for Yii2](http://phpexcel.codeplex.com/)
==================================================

[](#phpexcel-for-yii2)

基于 PHPExcel，用于将数据导出为 Excel 表格，或读取 Excel 表格的内容。

#### 错误提示: PHPExcel 自 2015-5-1 发布 v1.8.1 后，官方不再维护，请使用最新的[PhpSpreadsheet](https://github.com/moxuandi/yii2-phpspreadsheet)

[](#错误提示-phpexcel-自-2015-5-1-发布-v181-后官方不再维护请使用最新的phpspreadsheet)

安装:
---

[](#安装)

使用 [composer](http://getcomposer.org/download/) 下载:

```
# 1.x(非重要Bug, 不再更新):
composer require moxuandi/yii2-phpexcel:"~1.0"

# 开发版:
composer require moxuandi/yii2-phpexcel:"dev-master"

```

导出数据(export):
-------------

[](#导出数据export)

```
$data = [
    ['id' => 1, 'name' => '张三', 'sex' => '男', 'phone' => 15515181929, 'idc' => 211325659536289526],
    ['id' => 2, 'name' => '李四', 'sex' => '男'],
    ['id' => 5, 'name' => '王二', 'sex' => '女'],
    ['id' => 8, 'name' => '麻子', 'sex' => '女'],
];
ExportData::widget([
    'data' => $data,  // 必须
    'headers' => [],  // 第一行的标题栏
    'sheetTitle' => 'sheet',  // 工作表的标题
    'setFirstTitle' => false,  // 是否在第一行设置标题行
    'asAttachment' => false,  // 是否下载导出结果
    'fileName' => 'word.xls',  // 导出的 Excel 文件名
    'savePath' => 'uploads/excels/',  // 保存到服务器的路径
    'properties' => [],  // Excel 文件的属性列表
]);

// 在一个 Excel 中导出多个工作表:
$data = [
    'sheet1' => [
        ['id' => 1, 'name' => '张三', 'sex' => '男', 'phone' => 15515181929, 'idc' => 412728199201123271],
        ['id' => 2, 'name' => '李四', 'sex' => '男'],
        ['id' => 5, 'name' => '王二', 'sex' => '女'],
        ['id' => 8, 'name' => '麻子', 'sex' => '女'],
    ],
    'sheet2' => [
        ['id' => 11, 'name' => '张三', 'sex' => '男', 'phone' => 15515181929, 'idc' => 412728199201123271],
        ['id' => 12, 'name' => '李四', 'sex' => '男'],
        ['id' => 15, 'name' => '王二', 'sex' => '女'],
        ['id' => 18, 'name' => '麻子', 'sex' => '女'],
    ],
    'sheet3' => [
        ['id' => 21, 'name' => '张三', 'sex' => '男', 'phone' => 15515181929, 'idc' => 412728199201123271],
        ['id' => 22, 'name' => '李四', 'sex' => '男'],
        ['id' => 25, 'name' => '王二', 'sex' => '女'],
        ['id' => 28, 'name' => '麻子', 'sex' => '女'],
    ],
];
ExportData::widget([
    'data' => $data,  // 必须
    'isMultipleSheet' => true,  // 必须
    'sheetTitle' => ['sheet1' => 'sheet1', 'sheet2' => 'sheet2'],  // 工作表的标题
]);

```

导出数据模型(export):
---------------

[](#导出数据模型export)

```
// 导出单个表, 并下载导出的Excel文件
ExportModel::widget([
    'models' => Post::find()->all(),  // 必须
    'asAttachment' => true,  // 默认值, 可忽略
]);

// 导出单个表, 并将文件保存到服务器, 返回导出后的 Excel 文件路径
$url = ExportModel::widget([
    'models' => Post::find()->all(),  // 必须
    'asAttachment' => false,  // false 时保存到服务器
    'fileName' => time() . '.xls',  // 默认为:'excel.xls'
]);
// return: $url = 'uploads/excel/1500597563.xls';

// 导出单个表中指定的列
ExportModel::widget([
    'models' => Post::find()->all(),  // 必须
    'columns' => ['id', 'real_name', 'file_name', 'file_size'],
    'headers' => ['id' => 'ID', 'real_name' => '源文件名', 'file_name' => '新文件路径', 'file_size' => '大小(B)'],
    // 'headers'数组中的键名必须是'columns'数组的值, 否则无效
]);

// 导出多个表, 一个 Excel 文件多个表
ExportModel::widget([
    'isMultipleSheet' => true,  // 导出多个表时, 必须为 true
    'models' => [
        'sheet1' => Post::find()->all(),
        'sheet2' => Article::find()->all(),
        'sheet3' => Effect::find()->all(),
    ],
    //指定导出的列
    'columns' => [
        'sheet1' => ['id', 'real_name', 'file_name', 'file_size'],
        'sheet2' => ['id', 'title', 'sort'],
        'sheet3' => ['id', 'title', 'summary', 'method', 'demo_url'],
    ],
    // 设置每个表的标题
    'headers' => [
        'sheet1' => ['id' => 'ID', 'real_name' => '源文件名', 'file_name' => '新文件路径', 'file_size' => '大小(B)'],
        'sheet2' => ['id' => 'ID', 'title' => '文章标题', 'sort' => '排序值'],
        'sheet3' => ['id' => 'ID', 'title' => '插件标题', 'summary' => '插件介绍', 'demo_url' => '演示地址'],
    ],
]);

// 更强的导出功能: 自定义导出数据的格式
ExportModel::widget([
    'models' => Post::find()->all(),  // 必须
    'columns' => [
        'id',
        'real_name',
        'file_name',
        [
            'attribute' => 'file_size',
            'header' => '文件大小',
            'format' => 'text',
            'value' => function($model){
                return Helper::byteFormat($model->file_size);  //eg: '363.38KB'
            }
        ],
        'created_at:datetime',  //eg: '2017年5月4日 上午7:41:25'
        [
            'attribute' => 'updated_at',
            'format' => 'date'  //eg: '2017年5月4日'
        ],
        [
            'attribute' => 'updated_at',
            'header' => '最后修改时间',
            'format' => ['date', 'php:Y-m-d'],  //eg: '2017-05-04'
        ]
    ],
    'headers' => ['id' => 'ID', 'real_name' => '源文件名', 'file_name' => '新文件路径'],
]);

```

导入(import):
-----------

[](#导入import)

```
// 导入一个 Excel 文件
$data = ImportFile::widget([
    'file' => 'uploads/excel/excel.xls',
    'setFirstRecordAsKeys' => false,  // 是否将 Excel 文件中的第一行记录设置为每行数据的键
]);

// 导入一个多表 Excel 文件中指定的一个工作表
$data = ImportFile::widget([
    'file' => 'uploads/excel/excel2.xls',
    'setIndexSheetByName' => false,  // 如果 Excel 文件中有多个工作表, 是否以表名(eg:sheet1,sheet2)作为键名
    'getOnlySheet' => 'Worksheet',  // 指定一个工作表
    'getOnlySheet' => ['Worksheet', 'Worksheet (2)'],  // 指定多个工作表
]);

```

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~0 days

Total

2

Last Release

2779d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/38183123fe75b8fbe91f8cde1a806d5fd9d0913edbbc53a1ae7d89c7907bbb9a?d=identicon)[moxuandi](/maintainers/moxuandi)

---

Top Contributors

[![moxuandi](https://avatars.githubusercontent.com/u/22020977?v=4)](https://github.com/moxuandi "moxuandi (2 commits)")

---

Tags

excelyii2extension

### Embed Badge

![Health badge](/badges/moxuandi-yii2-phpexcel/health.svg)

```
[![Health](https://phpackages.com/badges/moxuandi-yii2-phpexcel/health.svg)](https://phpackages.com/packages/moxuandi-yii2-phpexcel)
```

###  Alternatives

[moonlandsoft/yii2-phpexcel

Exporting PHP to Excel or Importing Excel to PHP

1491.1M16](/packages/moonlandsoft-yii2-phpexcel)[arogachev/yii2-excel

ActiveRecord import and export based on PHPExcel for Yii 2 framework

6480.3k1](/packages/arogachev-yii2-excel)[phpnt/yii2-export

Yii2 It saves data in xls, csv, word, html, pdf files.

158.9k](/packages/phpnt-yii2-export)[ruskid/yii2-csv-importer

Helper to import csv files into database

37356.4k4](/packages/ruskid-yii2-csv-importer)[custom-it/yii2-excel-report

An extension for generate excel file from GridView content

381.9k](/packages/custom-it-yii2-excel-report)

PHPackages © 2026

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