spreadsheet php,PhpSpreadsheet的简单使用
由于PHPExcel已经不再维护,PhpSpreadsheet是PHPExcel的下一个版本。PhpSpreadsheet是一个用纯PHP编写的库,并引入了命名空间,PSR规范等。这里简单介绍下PhpSpreadsheet的导入导出功能。
1、安装
使用composer安装:
composer require phpoffice/phpspreadsheet
GitHub下载:
2、excel文件导出
/**
* excel文件导出*/
functionexport()
{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文件保存到本地*/
functionsave()
{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文件内容*/
functionread()
{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安装的话会自动生成。没有的话可以手动下载。
spreadsheet php,PhpSpreadsheet的简单使用相关推荐
- php Spreadsheet 导出,PhpSpreadsheet 导出Excel
/** * Excel 助手 * sudo composer require phpoffice/phpspreadsheet */ namespace CommonUtil; use PhpOffi ...
- spreadsheet php,关于 PhpSpreadsheet 简单教程
今天遇到一个问题,涉及 php 与 excel 之间数据转换.之前一直用 PHPExcel,他们的开发组不更新了.但是找到了 PhpSpreadsheet. 一.介绍 用纯 php 编写的库,它提供了 ...
- php Spreadsheet Csv,使用 PhpSpreadsheet 实现读取写入 Execl
使用 PhpSpreadsheet 实现读取写入 Execl 由 吴亲库里 创建于1年前, 最后更新于 1年前 版本号 #1 9742 views 1 likes 0 collects 前言 这两天需 ...
- Phpspreadsheet 中文文档(六)读写文件+读取文件
读写文件 从体系结构您已经知道,使用基本PhpSpreadsheet类无法对持久性存储进行读写.为此,PhpSpreadsheet提供读者和作家,这是实现\PhpOffice\PhpSpreadshe ...
- phpspreadsheet 中文文档(七)技巧和诀窍
2019年10月11日14:08:35 以下页面为您提供了一些使用广泛的PhpSpreadsheet食谱.请注意,这些文件没有提供有关特定PhpSpreadsheet API函数的完整文档,而只是一个 ...
- PHPExcel替代方案PhpSpreadsheet
PHPExcel上一版本1.8.1于2015年发布.该项目已不再维护,可以使用,但是不建议再使用.所有用户都应该迁移到其直接后继者PhpSpreadsheet或其他替代方案.PhpSpreadshee ...
- PhpSpreadsheet常用操作
php用PhpSpreadsheet对Excel进行读取.写入.修改十分便捷,下面将我在工作中用到过的操作进行总结.文档参考百度搜索和原始文档:https://phpspreadsheet.readt ...
- PhpSpreadsheet读取单元格内容的坑
在利用php读取Excel时,当前(2019年)流行的做法是利用composer安装PhpSpreadsheet(composer require phpoffice/phpspreadsheet)来 ...
- php生成表格数据类型,phpspreadsheet 中文文档(四) 创建电子表格+档案格式
该Spreadsheet班 该Spreadsheet班是PhpSpreadsheet的核心.它包含对所包含工作表,文档安全性设置和文档元数据的引用. 为了简化PhpSpreadsheet概念:Spre ...
最新文章
- 使用intellij查看scala变量的具体类型
- SAP Customer Data Cloud(Gigya)登录的重定向问题
- rabbitmq-路由模式-routingkey
- win10获取注册表权限
- Python获取路径中的文件名
- Cinema 4d 软件介绍
- 旅游吧!我在这里 ——旅游相册POI搜索:找回你的足迹
- Play框架文件上传
- Android中常用的一些颜色色值color整理
- 利器!推荐一个记忆方法:间隔记忆法(利用艾宾浩斯遗忘曲线制定)
- 用模拟退火算法估价heston期权定价模型的五个参数
- vue下载后端返回的压缩包
- jetson nano 报错Illegal instruction(core dumped)
- java验证身份证合法性_Java安全性,第2部分:身份验证和授权
- eclipse jvm_一种与众不同的JVM语言:Eclipse Xtend(现在适用于Android)
- markdown文档管理工具
- 电商怎么起号做直播带货需要注意什么
- 简述网桥的特点_网桥的工作原理和特点是什么?
- 如何用计算机控制家带你开关,kg316t微电脑时控开关怎么调 微电脑时控开关设置方法【图文】...
- 【原创】从头开始,使用安卓系统WebView做一个功能强大的Epub阅读器(二)
热门文章
- A Game of Thrones(41)
- 中科院计算机网络信息中心是一种怎样的存在?
- Java学习-面向对象进阶二
- 五子棋棋谱16*16c语言,五子棋开局棋谱大全
- 联想服务器刀片机型号,ThinkSystem SN550刀片服务器
- 朋友圈投票活动-刷票案例实现与分析
- unity blend 笔记
- 玩转华为数据中心交换机系列 | 配置VLAN内协议报文透传示例
- 【Unity 框架】QFramework v1.0 使用指南 工具篇:06. UIKit 界面管理快速开发解决方案 | Unity 游戏框架 | Unity 游戏开发 | Unity 独立游戏
- Java使用MongoTemplate操作MangoDB,实现根据时间等条件组合查询,解决ISODate的问题