当前位置:首页 > PHP

php实现对excel

2026-03-13 04:18:37PHP

PHP 实现 Excel 操作的方法

使用 PHPExcel 或 PhpSpreadsheet 库

PhpSpreadsheet 是 PHPExcel 的继任者,推荐使用 PhpSpreadsheet 进行 Excel 操作。以下是一个简单的示例,展示如何读取和写入 Excel 文件。

安装 PhpSpreadsheet:

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 文件

require 'vendor/autoload.php';

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

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World!');

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

导出 Excel 文件到浏览器

require 'vendor/autoload.php';

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

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World!');

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

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

使用 PHP 原生函数处理 CSV 文件

如果只需要处理简单的 CSV 文件,可以使用 PHP 的原生函数。

读取 CSV 文件:

$file = fopen('example.csv', 'r');
while (($line = fgetcsv($file)) !== false) {
    print_r($line);
}
fclose($file);

写入 CSV 文件:

$file = fopen('example.csv', 'w');
$data = [
    ['Name', 'Email'],
    ['John Doe', 'john@example.com'],
    ['Jane Doe', 'jane@example.com']
];

foreach ($data as $row) {
    fputcsv($file, $row);
}
fclose($file);

使用 SimpleExcel 库

SimpleExcel 是一个轻量级的 PHP 库,适合简单的 Excel 操作。

安装 SimpleExcel:

composer require faisalman/simple-excel-php

读取 Excel 文件:

require 'vendor/autoload.php';

use SimpleExcel\SimpleExcel;

$excel = new SimpleExcel('csv');
$excel->parser->loadFile('example.csv');
$rows = $excel->parser->getField();
print_r($rows);

写入 Excel 文件:

php实现对excel

require 'vendor/autoload.php';

use SimpleExcel\SimpleExcel;

$excel = new SimpleExcel('csv');
$data = [
    ['Name', 'Email'],
    ['John Doe', 'john@example.com'],
    ['Jane Doe', 'jane@example.com']
];
$excel->writer->setData($data);
$excel->writer->saveFile('example');

注意事项

  • PhpSpreadsheet 功能强大,但处理大文件时可能消耗较多内存。
  • 对于大型文件,考虑使用流式处理或分批处理。
  • CSV 文件处理简单,但不支持 Excel 的高级功能如样式、公式等。

标签: phpexcel
分享给朋友:

相关文章

php实现异步

php实现异步

PHP 实现异步的方法 PHP 本身是同步执行的脚本语言,但可以通过以下方法模拟异步操作或实现异步效果: 使用多进程(pcntl_fork) 通过 pcntl_fork 创建子进程实现异步,适用于…

php搜索功能实现

php搜索功能实现

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

php实现的进度条

php实现的进度条

PHP 实现的进度条方法 在 PHP 中实现进度条通常可以通过命令行脚本(CLI)或 Web 页面两种方式完成。以下是几种常见的实现方法: 命令行进度条 使用 PHP CLI 实现进度条,可以通过不…

无限分类实现 php

无限分类实现 php

无限分类的实现方法 无限分类通常用于构建多层级结构的数据,如商品分类、部门架构等。以下是几种常见的实现方式: 邻接列表模式(Adjacency List) 邻接列表是最简单的实现方式,通过在每个记录…

php session 实现购物车

php session 实现购物车

PHP Session 实现购物车的方法 初始化 Session 在 PHP 脚本的最开始调用 session_start() 函数,确保会话已启用。这通常在页面的顶部完成。 session_sta…

php实现单例模式

php实现单例模式

单例模式的概念 单例模式是一种创建型设计模式,确保一个类只有一个实例,并提供一个全局访问点。这种模式常用于数据库连接、日志记录器等需要全局唯一实例的场景。 PHP实现单例模式的关键步骤 私有化构造函…