phpspreadsheet中文手册_php读取文件内容

phpspreadsheet中文手册_php读取文件内容由于phpexcel已经不再维护,phpspreadsheet是phpexcel的下一个版本。phpspreadsheet是一个用纯php编写的库,并引入了命名空间,psr规范等。这里简单介绍下phpspreadsheet的导入导出功能。1、安装使用composer安装:composerrequirephpoffice/phpspreadsheetgithub下载:2、excel文件导出/**…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

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

1、安装

使用composer安装:

composer require phpoffice/phpspreadsheet

github下载:

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;

}

结果:

2fc1b0a95487ef1d41780cb41caf3765.png

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;

}

结果:

98051ec76959d0fe71a05a594ef7097c.png

可能出现的问题:

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://javaforall.net/192499.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • scrapy安装步骤_scrapy安装失败

    scrapy安装步骤_scrapy安装失败scrapy安装指南

    2022年9月18日
    2
  • dota2连接服务器没有响应,win10系统dota2无法与任何服务器建立连接的解决方法

    dota2连接服务器没有响应,win10系统dota2无法与任何服务器建立连接的解决方法很多小伙伴都遇到过win10系统dota2无法与任何服务器建立连接的情况,想必大家都遇到过win10系统dota2无法与任何服务器建立连接的情况吧,那么应该怎么处理win10系统dota2无法与任何服务器建立连接呢?我们依照1、按下windows+Q组合键打开搜索框,在搜索框中搜索cmd,在搜索结果中我们可以看到命令提示符在命令提示符选项上单击右键,选择【以管理员身份运行】;2、在命令…

    2022年5月13日
    92
  • 【大话QT之十六】使用ctkPluginFramework插件系统构建项目实战「建议收藏」

    【大话QT之十六】使用ctkPluginFramework插件系统构建项目实战「建议收藏」”使用ctkPluginFramework插件系统构建项目实战”,这篇文章是写博客以来最纠结的一篇文章。倒不是因为技术都多么困难,而是想去描述一个项目架构采用ctkPluginFramework来构建总是未尽其意,描述的太少未免词不达意,描述的太多又显得太啰嗦。有些看过之前写的【大话QT之四】ctkPlugin插件系统实现项目插件式开发这篇文章的朋友也想了解一下到底如果从零开始架构一个项目。在写这

    2022年6月6日
    28
  • 访问远程MySQL数据库的方法

    访问远程MySQL数据库的方法

    2021年9月19日
    46
  • Java拖拽排序工具类「建议收藏」

    Java拖拽排序工具类「建议收藏」packagecom.ciih.jwt.util.sort;importjava.lang.reflect.Field;importjava.util.Collections;importjava.util.List;/***拖拽排序工具:此工具将传入的list重新排序后返回,使用者只需要将list重新存入数据库即可完成排序.*<>*拖拽排序必然牵扯到两个元素,被拖拽的元素和被挤压的元素.排序方式就存在两种,一种是两个元素进行交换位置,一种是一个元素拖到.

    2022年6月29日
    26
  • flashfxp注册码

    flashfxp注册码FlashFXP4.0注册码key(通用):——–FlashFXPRegistrationDataSTART——–FLASHFXPVENSVURFnQEAAAGGZJcQuuC6/Znb915ltgBNBmXkEQhOgVxpo/z4OJEIfnjjL/LLDCQbiZE9+N8EbDIQP/sQQf5D+faH6owMEG7/wINp3590f9jk462O98CWS

    2022年7月26日
    11

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号