由于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;
}
结果:
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下载地址:
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!