由于phpexcel已经不再维护,phpspreadsheet是phpexcel的下一个版本。phpspreadsheet是一个用纯php编写的库,并引入了命名空间,psr规范等。这里简单介绍下phpspreadsheet的导入导出功能。
compor require phpoffice/phpspreadsheetgithub下载:
https://github.com/phpoffice/phpspreadsheet
/** * excel文件导出 */function export(){ require_once __dir__ . '/vendor/au四级分数占比toload.php'; $data = [ ['title1' => '111', 'title2' => '222'], ['title1' => '111', 'title2' => '222'], ['title1' => '111', 'title2' => '222'] ]; $title = ['第一行标题', '第二行标题']; // create new spreadsheet object $spre孕妇食谱adsheet = new \phpoffice\phpspreadsheet\spreadsheet(); $sheet = $spreadsheet->getactivesheet(); // 方法一,使用 tcellvaluebycolumnandrow //表头 //设置单元格内容 foreach ($title as $key => $value) { // 单元格内容写入 $sheet->tcellvaluebycolumnandrow($key + 1, 1, $value); } $row = 2; // 从第二行开始 foreach ($data as $item) { $column = 1; foreach ($item as $value) { // 单元格内容写入 $sheet->tcellvaluebycolumnandrow($column, $row, $value); $column++; } $row++; } // 方法二,使用 tcellvalue //表头 //设置单元格内容 $titcol = 'a'; foreach ($title as $key => $value) { // 单元格内容写入 $sheet->tcellvalue($titcol . '1', $value); $titcol++; } $row = 2; // 从第二行开始 foreach ($data as $item) { $datacol = 'a'; foreach ($item as $value) { // 单元格内容写入 $sheet->tcellvalue($datacol . $row, $value); $datacol++; } $row++; } // r山东大学全国排名edirect output to a client’s web browr (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 rving to ie 9, then the following may be needed 仓廪实而知礼节衣食足而知荣辱 header('cache-control: max-age=1'); // if you're rving 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;}
结果:
/** * 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->tcellvalue($titcol . '1', $value); $titcol++; } $row = 2; // 从第二行开始 foreach ($data as $item) { $datacol = 'a'; foreach ($item as $value) { // 单元格内容写入 $sheet->tcellvalue($datacol . $row, $value); $datacol++; } $row++; } // save $writer = \phpoffice\phpspreadsheet\iofactory::createwriter($spreadsheet, 'xlsx'); $writer->save('01simple.xlsx');}
/** * 读取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;}
结果:
这是因为没有自动加载。可以手动引入加载文件。
require_once __dir__ . '/vendor/autoload.php';
或者:
require_once __dir__ . '/vendor/phpoffice/phpspreadsheet/src/bootstrap.php';
这是因为没有psr文件,缺少simple-cache模块。如果使用compor安装的话会自动生成。没有的话可以手动下载。
github下载地址:
本文发布于:2023-04-07 12:56:49,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/90263abb14bd8857a948d62f04652150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:PhpSpreadsheet的简单使用.doc
本文 PDF 下载地址:PhpSpreadsheet的简单使用.pdf
留言与评论(共有 0 条评论) |