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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 论坛提问智慧

    论坛提问智慧本文转载自http://bbs.weblogicfans.net/thread-3628-1-1.html.一、确定你自己无法解决该问题 首先你至少应该解决问题花费1个小时以上的时间,并最终确定自己无力解决。问题的解决并不能够完全依靠他人。二、查看论坛精华文章 你遇到的问题很有可能已经有文章详细的论述如何解决了,精华文章往往是大家最需要留心关注的地方。 三、使用

    2022年7月26日
    5
  • 网页添加背景音乐

    网页添加背景音乐为网页添加背景音乐的方法一般有两种,第一种是通过普通的标签来添加,另一种是通过标签来添加。(一)使用标签用Dreamweaver打开需要添加背景音乐的页面,点击“代码”打开代码编辑视图,在之间输入“Dreamweaver自动输入“空格键,代码提示框会自动将bgsound标签的属性列出来供你选择使用。bgsound标签共有五个属性,其中balance是设置音乐的左右均衡,delay是

    2022年9月24日
    1
  • 自然常数e的由来(简单通俗易于理解自然常数e)「建议收藏」

    自然常数e的由来(简单通俗易于理解自然常数e)「建议收藏」自然常数e的由来 (该文章用于自学和分享) 开篇先讲两个例子苏格拉底的麦穗柏拉图问苏格拉底,什么是爱情。苏格拉底说,这样吧,你去麦田里,不要回头,一直往前走,把你遇到的、最大的那棵麦穗摘下来、拿给我。后面的事,大家都知道了:柏拉图瞻前顾后,总觉得后面还有更好的,结果两手空空、一棵麦穗也没有得到。 除此之外,梅里尔·弗勒德(MerrillFlood)【提出过博弈论中…

    2025年7月21日
    3
  • python删除文件中指定内容

    python删除文件中指定内容lines=[lforlinopen("file.txt","r")ifl.find("20150723",0,8)!=0]fd=open("file.txt","w")fd.writelines(lines)fd.close()开头是20150723的行删除

    2022年5月29日
    41
  • Platform device and platform driver

    Platform device and platform driverPlatformdevice是专门给嵌入式系统设计的设备类型,一般在移植内核到自己的开发板时,基本上注册的所有的设备的类型全是platformdevice。实际上,platform在Linux内核中是以一条总线的身份登场的,要想让这样的总线和设备一起完美的工作,必须首先在系统

    2022年7月24日
    10
  • 邮箱正则校验[通俗易懂]

    邮箱正则校验[通俗易懂]”^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$”这个是一个企业级的程序里copy出来的。合法E-mail地址:1.必须包含一个并且只有一个符号“@”2.第一个字符不得是“@”或者“.”3.不允许出现“@.”或者.@4.结尾不得是字符“@”或者“.”…

    2022年6月15日
    56

发表回复

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

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