当前位置:首页 > PHP

PHP实现excel

2026-01-29 11:05:23PHP

PHP 实现 Excel 操作

读取 Excel 文件

使用 PHP 读取 Excel 文件可以通过 PhpSpreadsheet 库实现。安装该库可以通过 Composer:

composer require phpoffice/phpspreadsheet

读取 Excel 文件的基本代码示例:

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;

$inputFileName = 'example.xlsx';
$spreadsheet = IOFactory::load($inputFileName);
$sheet = $spreadsheet->getActiveSheet();

foreach ($sheet->getRowIterator() as $row) {
    $cellIterator = $row->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false);

    foreach ($cellIterator as $cell) {
        echo $cell->getValue() . "\t";
    }
    echo "\n";
}

写入 Excel 文件

使用 PhpSpreadsheet 创建和写入 Excel 文件的示例:

require 'vendor/autoload.php';

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

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$sheet->setCellValue('A1', 'Hello');
$sheet->setCellValue('B1', 'World');

$writer = new Xlsx($spreadsheet);
$writer->save('output.xlsx');

导出 Excel 文件

将数据导出为 Excel 文件并直接下载:

require 'vendor/autoload.php';

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

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

$sheet->setCellValue('A1', 'Name');
$sheet->setCellValue('B1', 'Email');

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="export.xlsx"');
header('Cache-Control: max-age=0');

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

处理 CSV 文件

如果需要处理 CSV 文件,可以使用 PHP 内置函数:

// 读取 CSV
if (($handle = fopen("input.csv", "r")) !== false) {
    while (($data = fgetcsv($handle, 1000, ",")) !== false) {
        print_r($data);
    }
    fclose($handle);
}

// 写入 CSV
$list = [['Name', 'Email'], ['John', 'john@example.com']];
$fp = fopen('output.csv', 'w');
foreach ($list as $fields) {
    fputcsv($fp, $fields);
}
fclose($fp);

使用其他库

除了 PhpSpreadsheet,还可以使用 PHPExcel(已弃用)或 SimpleExcel 等库。PhpSpreadsheet 是目前最推荐的解决方案。

PHP实现excel

注意事项

  • 确保服务器有足够的权限读写文件。
  • 处理大文件时,注意内存使用情况,可以分块读取或使用流式处理。
  • 导出文件时,确保设置正确的 HTTP 头以避免文件损坏。

以上方法涵盖了 PHP 中操作 Excel 文件的常见需求,包括读取、写入和导出。

标签: PHPexcel
分享给朋友:

相关文章

单例模式的实现PHP

单例模式的实现PHP

单例模式的基本概念 单例模式是一种创建型设计模式,确保一个类只有一个实例,并提供全局访问点。适用于需要全局唯一对象的场景,如数据库连接、日志管理等。 PHP 单例模式的实现步骤 私有化构造函数…

PHP http长连接的实现

PHP http长连接的实现

HTTP 长连接基础概念 HTTP长连接(Keep-Alive)允许在单个TCP连接上发送和接收多个HTTP请求/响应,减少重复建立和断开连接的开销。HTTP/1.1默认启用长连接,而HTTP/1.0…

PHP实现图文聊天

PHP实现图文聊天

PHP实现图文聊天的方法 数据库设计 创建数据库表存储聊天消息,包含字段如消息ID、发送者ID、接收者ID、消息内容、消息类型(文本/图片)、发送时间等。使用MySQL或其他数据库管理系统。 CRE…

PHP实现numpy

PHP实现numpy

在PHP中实现类似NumPy的功能,可以通过结合PHP的数组操作、扩展库或第三方工具来模拟部分功能。以下是几种常见方法: 使用PHP数组和内置函数 PHP的数组支持多维操作,结合内置数学函数可实现基…

mvc实现 PHP

mvc实现 PHP

MVC 模式概述 MVC(Model-View-Controller)是一种设计模式,用于将应用程序的逻辑分离为三个核心组件:模型(Model)处理数据,视图(View)负责展示,控制器(Contro…

PHP实现建站

PHP实现建站

PHP建站的基本步骤 安装PHP环境 使用XAMPP、WAMP或MAMP等集成环境快速搭建本地开发环境,这些工具包含Apache、MySQL和PHP。对于生产环境,推荐使用LNMP(Linux+Ngi…