由于PHPExcel已经不再维护,PhpSpreadsheet是PHPExcel的下一个版本。PhpSpreadsheet是一个用纯PHP编写的库,并引入了命名空间,PSR规范等。这里简单介绍下PhpSpreadsheet的导入导出功能。

1、安装

  • 使用composer安装:
composer require phpoffice/phpspreadsheet
  • GitHub下载:

https://github.com/PHPOffice/PhpSpreadsheet

2、excel文件导出

/**
* excel文件导出
*/
function export()
{
require_once __DIR__ . '/vendor/autoload.php'; $data = [
['title1' => '111', 'title2' => '222'],
['title1' => '111', 'title2' => '222'],
['title1' => '111', 'title2' => '222']
];
$title = ['第一行标题', '第二行标题']; // Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet(); // 方法一,使用 setCellValueByColumnAndRow
//表头
//设置单元格内容
foreach ($title as $key => $value) {
// 单元格内容写入
$sheet->setCellValueByColumnAndRow($key + 1, 1, $value);
}
$row = 2; // 从第二行开始
foreach ($data as $item) {
$column = 1;
foreach ($item as $value) {
// 单元格内容写入
$sheet->setCellValueByColumnAndRow($column, $row, $value);
$column++;
}
$row++;
} // 方法二,使用 setCellValue
//表头
//设置单元格内容
$titCol = 'A';
foreach ($title as $key => $value) {
// 单元格内容写入
$sheet->setCellValue($titCol . '1', $value);
$titCol++;
}
$row = 2; // 从第二行开始
foreach ($data as $item) {
$dataCol = 'A';
foreach ($item as $value) {
// 单元格内容写入
$sheet->setCellValue($dataCol . $row, $value);
$dataCol++;
}
$row++;
} // Redirect output to a client’s web browser (Xlsx)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0 $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
exit;
}

结果:

3、excel文件保存到本地

/**
* excel文件保存到本地
*/
function save()
{
require_once __DIR__ . '/vendor/autoload.php'; $data = [
['title1' => '111', 'title2' => '222'],
['title1' => '111', 'title2' => '222'],
['title1' => '111', 'title2' => '222']
];
$title = ['第一行标题', '第二行标题']; // Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet(); //表头
//设置单元格内容
$titCol = 'A';
foreach ($title as $key => $value) {
// 单元格内容写入
$sheet->setCellValue($titCol . '1', $value);
$titCol++;
}
$row = 2; // 从第二行开始
foreach ($data as $item) {
$dataCol = 'A';
foreach ($item as $value) {
// 单元格内容写入
$sheet->setCellValue($dataCol . $row, $value);
$dataCol++;
}
$row++;
} // Save
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('01simple.xlsx');
}

4、读取excel文件内容

/**
* 读取excel文件内容
*/
function read()
{
require_once __DIR__ . '/vendor/autoload.php';
$inputFileName = dirname(__FILE__) . '/01simple.xlsx';
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($inputFileName);
// 方法二
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
return $sheetData;
}

结果:

可能出现的问题:

1、Fatal error: Uncaught Error: Class 'PhpOffice\PhpSpreadsheet\Spreadsheet' not found

这是因为没有自动加载。可以手动引入加载文件。

require_once __DIR__ . '/vendor/autoload.php';

或者:

require_once __DIR__ . '/vendor/phpoffice/phpspreadsheet/src/Bootstrap.php';

2、Fatal error: Interface 'Psr\SimpleCache\CacheInterface' not found

这是因为没有psr文件,缺少simple-cache模块。如果使用composer安装的话会自动生成。没有的话可以手动下载。

GitHub下载地址:https://github.com/php-fig/simple-cache/releases

PhpSpreadsheet的简单使用的更多相关文章

  1. 使用PhpSpreadsheet将Excel导入到MySQL数据库

    本文以导入学生成绩表为例,给大家讲解使用PhpSpreadsheet将Excel导入的MySQL数据库. 准备 首先我们需要准备一张MySQL表,表名t_student,表结构如下: CREATE T ...

  2. phpspreadsheet开发手记

    坑安装简单示例通过模板来生成文件释放内存单元格根据索引获取英文列设置值合并单元格居中显示宽度设置批量设置单元格格式直接输出下载自动计算列宽函数formula单元格变可点击的超链 PhpSpreadsh ...

  3. PhpSpreadsheet如何读取excel文件

    PhpSpreadsheet如何读取excel文件 一.总结 一句话总结:万能的百度,直接搜代码就好,绝对有,毕竟github上面4000+的关注,说明很多人用了这个,使用照着demo倒是异常简单 二 ...

  4. [thinkphp使用phpspreadsheet时出现]Cannot redeclare xxxxxx() (previously declared in C:\WWW\xxx.xxx:xxx)

    [thinkphp使用phpspreadsheet时出现]Cannot redeclare xxxxxx() (previously declared in C:\WWW\xxx.xxx:xxx) 一 ...

  5. Laravel Excel安装及最简单使用

    官网:https://docs.laravel-excel.com/ 1.安装 1.1.安装要求: ​ PHP: ^7.0 ​ Laravel: ^5.5 ​ PhpSpreadsheet: ^1.6 ...

  6. php读写excel —— PhpSpreadsheet组件

    前言 PhpSpreadsheet是一个纯PHP类库,它提供了一组类,允许您从不同的电子表格文件格式(如Excel和LibreOffice Calc)读取和写入.用PHP读取Excel.CSV文件 还 ...

  7. PhpSpreadsheet 导出特定格式 — 广告请款单

    需求说明 最近需要实现一个导出这种格式的Excel表单,之前都有用过导出Excel的功能,但大都是表头+数据的形式,只用于获取数据,没有太多样式要求,不用合并单元格.合并居中等,也不用对每一行数据特异 ...

  8. phpspreadsheet

    2019-5-9 8:20:07 星期四 昨天在看PHPExcel的时候, github上作者说已经停止更新了, 推荐使用phpspreadsheet, 查看了一下官方文档, 功能还挺强大的, 可以读 ...

  9. phpspreadsheet 中文文档(七)技巧和诀窍

    2019年10月11日14:08:35 以下页面为您提供了一些使用广泛的PhpSpreadsheet食谱.请注意,这些文件没有提供有关特定PhpSpreadsheet API函数的完整文档,而只是一个 ...

随机推荐

  1. rails gem ransack 之模糊搜索

    gem 'ransack' eq: "等于" eq_any: "等于任意值" eq_all: "等于所有值" not_eq: "不 ...

  2. git 中的 merge 和 rebase

    示例分支:master . dev 把 dev 分支上的新内容合并到 master 上 先切换分支到master git checkout master 合并操作 git merge dev 或者 g ...

  3. 【Activiti】使用学习

    [Activiti]使用学习 转载: ================================================== 1.下载安装 2.清空表 3.开启sql打印 4. 5. = ...

  4. AES-加密解密工具类

    注意:AES加密算法对于不同系统之间的tokens可能会不同,需要在相同的系统间使用 如果设置为private,在其它类调用AES访问不了这个tokens常量,需要自主定义一个tokens或者在配置文 ...

  5. 深度强化学习(DQN-Deep Q Network)之应用-Flappy Bird

    深度强化学习(DQN-Deep Q Network)之应用-Flappy Bird 本文系作者原创,转载请注明出处:https://www.cnblogs.com/further-further-fu ...

  6. C# -- DateTime与TimeSpan

    1. DateTime的使用 DateTime dt = DateTime.Now; StringBuilder sb = new StringBuilder(); sb.AppendLine(str ...

  7. python通过人脸识别全面分析好友,一起看透你的“朋友圈”

    微信:一个提供即时通讯服务的应用程序,更是一种生活方式,超过数十亿的使用者,越来越多的人选择使用它来沟通交流. 不知从何时起,我们的生活离不开微信,每天睁开眼的第一件事就是打开微信,关注着朋友圈里好友 ...

  8. python基础之字符串讲解(上)

    字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或者")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可.For example: 为str输入一个变量,p ...

  9. Linux 实验 [Day 01]

    目录 1. Linux 简介(略过) 2. Linux 基本概念及操作:命令.快捷键与通配符 2.1 基础命令 2.2 终端快捷键 2.3 通配符 2.4 帮助命令 3. 用户及文件权限管理 3.1 ...

  10. C++之下载Visual Studio Installer缓慢问题

    将IPv4中设置DNS首选项为8.8.8.8即可.