当前位置:首页 > PHP

php 实现excel 导出

2026-03-13 06:04:48PHP

PHP 实现 Excel 导出方法

使用 PHPExcel 或 PhpSpreadsheet 库

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

安装 PhpSpreadsheet:

composer require phpoffice/phpspreadsheet

导出 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:

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 提供了内存优化选项:

php 实现excel 导出

$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实现树结构的方法 在PHP中实现树结构通常可以通过递归或迭代的方式完成。以下是几种常见的实现方法: 递归实现树结构 递归是处理树结构的自然方式,尤其适用于具有未知深度的树。 class Tr…

php队列的实现

php队列的实现

PHP队列的实现方式 PHP中实现队列功能可以通过多种方式,根据需求选择适合的方案: 使用数组实现基础队列 PHP数组本身支持队列操作,利用array_push和array_shift函数: $q…

php实现分类

php实现分类

PHP实现分类的方法 数据库设计 创建分类表时,通常需要包含id、名称、父级id等字段。父级id用于实现多级分类结构。 CREATE TABLE categories ( id INT AU…

php实现接口

php实现接口

PHP 实现接口的方法 在 PHP 中,接口(Interface)用于定义一组方法的规范,而不实现具体功能。类可以实现一个或多个接口,并必须实现接口中定义的所有方法。 定义接口 接口使用 inter…

php实现上传图片

php实现上传图片

上传图片的基本流程 PHP 实现图片上传功能需要处理文件接收、验证、保存等步骤。以下是具体实现方法。 创建 HTML 表单 在 HTML 中创建一个表单,设置 enctype="multipart/…

php搜索功能实现

php搜索功能实现

实现基本的PHP搜索功能 使用MySQL数据库和PHP实现简单的关键词搜索功能。需要确保数据库中有可供搜索的表和数据。 // 连接数据库 $conn = new mysqli('localhost'…