由于PHPExcel已经不再维护,PhpSpreadsheet是PHPExcel的下一个版本。PhpSpreadsheet是一个用纯PHP编写的库,并引入了命名空间,PSR规范等。这里简单介绍下PhpSpreadsheet的导入导出功能。
1、PhpSpreadsheet 介绍
一、PhpSpreadsheet 是什么
PhpSpreadsheet是一个用纯PHP编写的库,提供了一组类,使您可以读取和写入不同的电子表格文件格式
PhpSpreadsheet提供了丰富的API接口,可以设置诸多单元格以及文档属性,包括样式、图片、日期、函数等等诸多应用,总之你想要什么样的Excel表格,PhpSpreadsheet都能做到
使用 PhpSpreadsheet 开发的PHP要求 7.1或更高版本
PhpSpreadsheet 支持链式操作
二、PhpSpreadsheet 支持的文件格式
三、PhpSpreadsheet 官方网址
1、安装
用composer安装:
composer require phpoffice/phpspreadsheet
GitHub下载:
https://github.com/PHPOffice/PhpSpreadsheet
微云下载1.15:
https://share.weiyun.com/7sHuccke
2、excel文件导出
<?php
/**
* excel文件导出
*/
require_once __DIR__ . '/vendor/autoload.php';
$data = [
['title1' => '111', 'title2' => '222'],
['title1' => '111', 'title2' => '222'],
['title1' => '111', 'title2' => '222']
];
$title = ['第一行标题', '第二行标题'];
// 创建新的电子表格对象
$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++;
}
// 将输出重定向到客户端的网络浏览器(Xlsx)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="01simple.xlsx"');//文件名
header('Cache-Control: max-age=0');
// 如果你服务于IE 9,那么以下可能是需要的
header('Cache-Control: max-age=1');
// 如果您通过SSL为工业工程服务,那么可能需要以下内容
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;
?>
// 方法二(可选),使用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++;
}
3、读取excel文件内容并存到数据库示例
<?php
/**
* excel读取文件内容
*/
require './vendor/autoload.php';
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$reader->setReadDataOnly(true);
$spreadsheet = $reader->load('demo.xlsx');
$sheet = $spreadsheet->getActiveSheet();
$highestRow = $sheet->getHighestRow(); // 最大行数
$highestColumn = $sheet->getHighestColumn(); // 最大列数
$highestColumn = "AB";
// 把下标变成数字,如A-Z分别对应1-26,AA对应27,AB对应28,以此类推
$highestColumnIndex = PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn);
if($highestRow <= 2){ // 因为students.xlsx表格数据是从第三行开始的
exit('Excel没有任何数据');
}
$data = array();
for($row = 2;$row <= $highestRow;$row++){ //从第二行开始(第一行为标题)
$tempData['name'] = $sheet->getCellByColumnAndRow(1,$row)->getValue();
$tempData['parentid'] = $sheet->getCellByColumnAndRow(2,$row)->getValue();
$data[] = $tempData; //输出
}
/**********************截止此处,上面的代码为获取文件内容并存到$data数组中了*************************/
// 连接数据库,判断数据库错误这一步省略了
$con=mysqli_connect("localhost","root","root","ajax");
// 使用了mysql事务,关闭自动提交
mysqli_autocommit($con,false);
// 循环遍历
foreach ($data as $k=>$v){
$rt{$k} = mysqli_query($con,"insert into type (name,parentid) values ('".$v['name']."',".$v['parentid'].")");
}
$autocom = in_array("",$rt); //判断数组中是否存在空(如果有的会返回1)
// 开始执行数据并判断是否成功
if($autocom==1){
mysqli_query($con,"rollback");
echo "执行失败,并且回滚了";
}else{
mysqli_commit($con);
echo "成功执行";
}
?>