当前位置:首页 > PHP

php 实现excel 导出

2026-03-13 06:04:48PHP

PHP 实现 Excel 导出方法

使用 PHPExcel 或 PhpSpreadsheet 库

PhpSpreadsheet 是 PHPExcel 的继承者,推荐使用 PhpSpreadsheet 进行 Excel 导出。以下是一个基本示例:

安装 PhpSpreadsheet:

composer require phpoffice/phpspreadsheet

导出 Excel 代码示例:

php 实现excel 导出

<?php
require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

// 创建 Spreadsheet 对象
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// 设置表头
$sheet->setCellValue('A1', '姓名');
$sheet->setCellValue('B1', '年龄');
$sheet->setCellValue('C1', '邮箱');

// 填充数据
$sheet->setCellValue('A2', '张三');
$sheet->setCellValue('B2', '25');
$sheet->setCellValue('C2', 'zhangsan@example.com');

// 设置自动列宽
foreach(range('A','C') as $columnID) {
    $sheet->getColumnDimension($columnID)->setAutoSize(true);
}

// 设置文件名和输出
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="example.xlsx"');
header('Cache-Control: max-age=0');

$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
exit;

使用 CSV 格式导出

对于简单的数据导出,CSV 格式更轻量:

<?php
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');

$output = fopen('php://output', 'w');

// 写入表头
fputcsv($output, ['姓名', '年龄', '邮箱']);

// 写入数据
fputcsv($output, ['张三', 25, 'zhangsan@example.com']);
fputcsv($output, ['李四', 30, 'lisi@example.com']);

fclose($output);
exit;

使用 Laravel Excel 包(Laravel 框架)

安装 Laravel Excel:

php 实现excel 导出

composer require maatwebsite/excel

创建导出类:

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;

class UsersExport implements FromArray
{
    protected $data;

    public function __construct(array $data)
    {
        $this->data = $data;
    }

    public function array(): array
    {
        return $this->data;
    }
}

控制器中使用:

use App\Exports\UsersExport;
use Maatwebsite\Excel\Facades\Excel;

public function export() 
{
    $data = [
        ['姓名', '年龄', '邮箱'],
        ['张三', 25, 'zhangsan@example.com'],
        ['李四', 30, 'lisi@example.com']
    ];

    return Excel::download(new UsersExport($data), 'users.xlsx');
}

性能优化建议

大数据量导出时,考虑分块处理或使用生成器减少内存消耗。PhpSpreadsheet 提供了内存优化选项:

$spreadsheet = new Spreadsheet();
$spreadsheet->getActiveSheet()->fromArray($largeDataSet, null, 'A1', true);

// 使用缓存减少内存占用
$cacheSettings = [
    'cache' => [
        'dir' => '/path/to/cache',
        'gzip' => true
    ]
];
\PhpOffice\PhpSpreadsheet\Settings::setCache($cacheSettings);

以上方法涵盖了从简单到复杂的 Excel 导出需求,可根据项目实际情况选择合适方案。

标签: phpexcel
分享给朋友:

相关文章

php实现队列

php实现队列

PHP实现队列的方法 队列是一种先进先出(FIFO)的数据结构,PHP可以通过多种方式实现队列功能。 使用数组模拟队列 PHP数组可以模拟队列的基本操作,通过array_push和array_shi…

php 实现单链表

php 实现单链表

单链表的基本概念 单链表是一种线性数据结构,由节点组成,每个节点包含数据域和指向下一个节点的指针域。链表的头节点是访问整个链表的入口。 单链表的节点类实现 在PHP中,可以通过类来定义链表节…

php 实现mvc

php 实现mvc

PHP 实现 MVC 架构的方法 MVC(Model-View-Controller)是一种常见的软件设计模式,用于分离业务逻辑、用户界面和用户输入。以下是 PHP 实现 MVC 架构的关键步骤:…

php实现重定向

php实现重定向

使用header函数进行重定向 在PHP中,header()函数是实现重定向的常用方法。通过发送HTTP头信息Location,浏览器会自动跳转到指定URL。需确保在调用header()前没有输出任何…

php搜索功能的实现

php搜索功能的实现

实现PHP搜索功能的方法 数据库搜索 使用SQL的LIKE语句进行模糊匹配,适用于简单的搜索需求。 $searchTerm = $_GET['search']; $query = "SELECT *…

php array实现

php array实现

PHP 数组实现 PHP 数组是一种灵活的数据结构,可以存储多个值。以下是几种常见的实现方式: 索引数组 索引数组使用数字作为键名,默认从0开始递增。 $fruits = array("Apple…