PhpSpreadsheet的简单使用
由于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的简单使用的更多相关文章
- 使用PhpSpreadsheet将Excel导入到MySQL数据库
本文以导入学生成绩表为例,给大家讲解使用PhpSpreadsheet将Excel导入的MySQL数据库. 准备 首先我们需要准备一张MySQL表,表名t_student,表结构如下: CREATE T ...
- phpspreadsheet开发手记
坑安装简单示例通过模板来生成文件释放内存单元格根据索引获取英文列设置值合并单元格居中显示宽度设置批量设置单元格格式直接输出下载自动计算列宽函数formula单元格变可点击的超链 PhpSpreadsh ...
- PhpSpreadsheet如何读取excel文件
PhpSpreadsheet如何读取excel文件 一.总结 一句话总结:万能的百度,直接搜代码就好,绝对有,毕竟github上面4000+的关注,说明很多人用了这个,使用照着demo倒是异常简单 二 ...
- [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) 一 ...
- Laravel Excel安装及最简单使用
官网:https://docs.laravel-excel.com/ 1.安装 1.1.安装要求: PHP: ^7.0 Laravel: ^5.5 PhpSpreadsheet: ^1.6 ...
- php读写excel —— PhpSpreadsheet组件
前言 PhpSpreadsheet是一个纯PHP类库,它提供了一组类,允许您从不同的电子表格文件格式(如Excel和LibreOffice Calc)读取和写入.用PHP读取Excel.CSV文件 还 ...
- PhpSpreadsheet 导出特定格式 — 广告请款单
需求说明 最近需要实现一个导出这种格式的Excel表单,之前都有用过导出Excel的功能,但大都是表头+数据的形式,只用于获取数据,没有太多样式要求,不用合并单元格.合并居中等,也不用对每一行数据特异 ...
- phpspreadsheet
2019-5-9 8:20:07 星期四 昨天在看PHPExcel的时候, github上作者说已经停止更新了, 推荐使用phpspreadsheet, 查看了一下官方文档, 功能还挺强大的, 可以读 ...
- phpspreadsheet 中文文档(七)技巧和诀窍
2019年10月11日14:08:35 以下页面为您提供了一些使用广泛的PhpSpreadsheet食谱.请注意,这些文件没有提供有关特定PhpSpreadsheet API函数的完整文档,而只是一个 ...
随机推荐
- 【Hybrid】288- Hybrid App 应用开发中 9 个必备知识点复习
前言 我们大前端团队内部 ?每周一练 的知识复习计划继续加油,本篇文章是 <Hybrid APP 混合应用专题> 主题的第二期和第三期的合集. 这一期共整理了 10 个问题,和相应的参考答 ...
- 一线大厂Java面试必问的2大类Tomcat调优
一.前言 最近整理了 Tomcat 调优这块,基本上面试必问,于是就花了点时间去搜集一下 Tomcat 调优都调了些什么,先记录一下调优手段,更多详细的原理和实现以后用到时候再来补充记录,下面就来介绍 ...
- Socket与系统调用深度分析
学习一下对Socket与系统调用的分析分析 一.介绍 我们都知道高级语言的网络编程最终的实现都是调用了系统的Socket API编程接口,在操作系统提供的socket系统接口之上可以建立不同端口之间的 ...
- JVM系列四(内存分配策略).
一.概要 前面的文章介绍了对象的创建过程,其中第三步 -- 分配内存,只是简单的介绍了分配的方式 -- 指针碰撞.空闲列表,其实内存在堆上分配还大有文章嘞. 对象的内存分配,往大方向上讲,就是在堆上分 ...
- 用js写九九乘法表格,附带背景颜色
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- 使用Power BI API 向流数据集推送实时数据并在仪表板可视化
使用Power BI 实现实时数据的可视化是大家比较关心的一个话题,在仪表盘上实现推送数据的展示,可以在诸如指挥大屏等场景下使用. 本视频实战内容如下: https://v.qq.com/x/page ...
- DC8: Vulnhub Walkthrough
镜像下载链接: https://www.vulnhub.com/entry/dc-8,367/#download 主机扫描: http://10.10.202.131/?nid=2%27 http:/ ...
- System.out.println高并发下导致应用暂停
今天在进行序列号压测的时候,通过调用genSeqId方法来获取序列号,100并发,16台压测机器,发现无论怎么压测,调用量达到140w左右的时候,此方法的ops直接掉到了0,具体图示如下: 从上图可以 ...
- Jupyter修改设置
下载完anaconda后Jupyter默认目录是用户目录,默认浏览器是IE,让有强迫症的我有点难受,所以把它的默认目录和浏览器修改一下. 首先运行一下jupyter notebook --genera ...
- Splash 学习笔记
一.介绍 Splash 跟之前我们介绍的 Selenium ( 参考 Selenium 与自动化测试 -- <Selenium 2 自动化测试实战>读书笔记) 很类似,都可以理解成一个浏览 ...