PHPackages                             tp5er/think-csv - 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. [File &amp; Storage](/categories/file-storage)
4. /
5. tp5er/think-csv

ActiveLibrary[File &amp; Storage](/categories/file-storage)

tp5er/think-csv
===============

一个thinkphp包，可以轻松地从Eloquent模型生成CSV文件。

11.9kPHP

Since Aug 8Pushed 12mo agoCompare

[ Source](https://github.com/pkg6/think-csv)[ Packagist](https://packagist.org/packages/tp5er/think-csv)[ RSS](/packages/tp5er-think-csv/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (0)

基本用法
----

[](#基本用法)

```
$users = User::select(); // All users
$csvExporter = new \tp5er\thinkCsv\Export();
$csvExporter->build($users, ['email', 'name'])->download();

```

建立CSV
-----

[](#建立csv)

`$exporter->build($modelCollection, $fields)`需要三个参数。第一个是模型（模型的集合），第二个是要导出的字段名称，第三个是配置，这是可选的。

```
$csvExporter->build(User::select(), ['email', 'name', 'created_at']);

```

输出选项
----

[](#输出选项)

### 下载

[](#下载)

要将文件下载到浏览器:

```
$csvExporter->download();

```

如果您愿意，可以提供文件名：

```
$csvExporter->download('active_users.csv');

```

如果没有给出文件名，则将生成带有日期时间的文件名。

### 高级输出

[](#高级输出)

LaraCSV 使用[League CSV](http://csv.thephpleague.com/)。您可以做 League CSV 能做的事情。您可以通过调用获取底层 League CSV writer 和 reader 实例：

```
$csvWriter = $csvExporter->getWriter();
$csvReader = $csvExporter->getReader();

```

然后你可以做几件事，比如：

```
$csvString = $csvWriter->getContent(); // To get the CSV as string
$csvReader->jsonSerialize(); // To turn the CSV in to an array

```

有关更多信息，请查看[League CSV 文档](http://csv.thephpleague.com/)。

自定义标题
-----

[](#自定义标题)

上面的代码示例将生成一个带有标题电子邮件、名称、created\_at 和后面的相应行的 CSV。

如果要使用自定义标签更改标题，只需将其作为数组值传递：

```
$csvExporter->build(User::select(), ['email', 'name' => 'Full Name', 'created_at' => 'Joined']);

```

现在`name`列将显示标题，`Full Name`但它仍然会从`name`模型的字段中获取值。

### 无标题

[](#无标题)

您还可以取消 CSV 标头：

```
$csvExporter->build(User::select(), ['email', 'name', 'created_at'], [
    'header' => false,
]);

```

修改或添加值
------

[](#修改或添加值)

在处理数据库行之前会触发一个钩子。例如，如果您想更改日期格式，您可以这样做。

```
$csvExporter = new \Laracsv\Export();
$users = User::get();

// Register the hook before building
$csvExporter->beforeEach(function ($user) {
    $user->created_at = date('f', strtotime($user->created_at));
});

$csvExporter->build($users, ['email', 'name' => 'Full Name', 'created_at' => 'Joined']);

```

\*\*注意：\*\*如果`beforeEach`回调返回，`false`则整行将从 CSV 中排除。过滤一些行会很方便。

### 添加字段和值

[](#添加字段和值)

您还可以添加数据库表中不存在的字段并动态添加值：

```
// The notes field doesn't exist so values for this field will be blank by default
$csvExporter->beforeEach(function ($user) {
    // Now notes field will have this value
    $user->notes = 'Add your notes';
});

$csvExporter->build($users, ['email', 'notes']);

```

分块构建
----

[](#分块构建)

对于可能会消耗更多内存的较大数据集，可以使用构建器实例以块的形式处理结果。类似于行相关的钩子，在这种情况下可以使用块相关的钩子，例如急切加载或类似的基于块的操作。两个钩子之间的行为是相似的；它在每个块之前被调用，并将整个集合作为参数。**如果`false`返回，整个块被跳过，代码继续下一个。**

```
// Perform chunk related operations
$export->beforeEachChunk(function ($collection) {

});
$export->buildFromBuilder(User::newQuery(),['email', 'name']);
$export->buildFromBuilder(Db::table('user'),['email', 'name']);

```

默认块大小设置为 1000 个结果，但可以通过在`$config`传递给`buildFromBuilder`. 示例将块大小更改为 500。

```
$export->buildFromBuilder(User::newQuery(),['email', 'name'], ['chunk' => 500]);

```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance37

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity21

Early-stage or recently created project

 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/28b119a234a9963e2266b2b493c037b1d7f01d3e17a06a3ffe94481549efa47b?d=identicon)[1751212020](/maintainers/1751212020)

---

Top Contributors

[![chihqiang](https://avatars.githubusercontent.com/u/40115555?v=4)](https://github.com/chihqiang "chihqiang (1 commits)")

### Embed Badge

![Health badge](/badges/tp5er-think-csv/health.svg)

```
[![Health](https://phpackages.com/badges/tp5er-think-csv/health.svg)](https://phpackages.com/packages/tp5er-think-csv)
```

###  Alternatives

[knplabs/gaufrette

PHP library that provides a filesystem abstraction layer

2.5k39.8M123](/packages/knplabs-gaufrette)[google/cloud-storage

Cloud Storage Client for PHP

34390.8M125](/packages/google-cloud-storage)[illuminate/filesystem

The Illuminate Filesystem package.

15261.6M2.6k](/packages/illuminate-filesystem)[superbalist/flysystem-google-storage

Flysystem adapter for Google Cloud Storage

26320.6M30](/packages/superbalist-flysystem-google-storage)[creocoder/yii2-flysystem

The flysystem extension for the Yii framework

2931.7M62](/packages/creocoder-yii2-flysystem)[flowjs/flow-php-server

PHP library for handling chunk uploads. Works with flow.js html5 file uploads.

2451.6M15](/packages/flowjs-flow-php-server)

PHPackages © 2026

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