PhpSpreadsheet是什么?怎样对Excel操作?
Admin 2021-05-10 群英技术资讯 669 次浏览
PhpSpreadsheet是一个用纯PHP编写的库,提供了一组类,可以方便我们进行读取和写入不同的电子表格文件格式,因此PHP使用PhpSpreadsheet能很比较方便的对Excel进行操作,有些朋友可以能没有接触过PhpSpreadsheet,下面小编就给大家介绍一下PHP使用PhpSpreadsheet操作Excel的方法。
PhpSpreadsheet是一个用纯PHP编写的库,提供了一组类,使您可以读取和写入不同的电子表格文件格式。PhpSpreadsheet提供了丰富的API接口,可以设置诸多单元格以及文档属性,包括样式、图片、日期、函数等等诸多应用,总之你想要什么样的Excel表格,PhpSpreadsheet都能做到
<?php # 载入composer自动加载文件 require 瑞块儿 require 'vendor/autoload.php'; autoload 奥特老特 # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet();
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet();
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); # 获取单元格 $cell = $sheet->getCell('A1'); $cell = $sheet->getCellByColumnAndRow(1,1);
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); # 获取单元格 $cellA = $sheet->getCell('A1'); # 设置单元格值 $cellA->setValue('欧阳克'); # 获取单元格 $cellB = $sheet->getCellByColumnAndRow(1,2); # 设置单元格值 $cellB->setValue('黄蓉'); # 获取设置单元格,链式操作 $sheet->getCell('A3')->setValue('郭靖'); $sheet->getCellByColumnAndRow(1,4)->setValue('杨康');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); # 获取单元格 $cellA = $sheet->getCell('A1'); # 设置单元格值 $cellA->setValue('欧阳克'); echo '值: ', $cellA->getValue(),PHP_EOL; echo '坐标: ', $cellA->getCoordinate();
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); # 获取单元格 $cellA = $sheet->getCell('A1'); # 设置单元格值 $cellA->setValue('欧阳克'); # 获取单元格 $cellB = $sheet->getCellByColumnAndRow(1,2); # 设置单元格值 $cellB->setValue('黄蓉'); # 获取设置单元格,链式操作 $sheet->getCell('A3')->setValue('郭靖'); $sheet->getCellByColumnAndRow(1,4)->setValue('杨康'); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年龄'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '欧阳克'); $sheet->setCellValueByColumnAndRow(3, 2, '18岁'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); $sheet->setCellValueByColumnAndRow(1, 3, 2); $sheet->setCellValueByColumnAndRow(2, 3, '黄蓉'); $sheet->setCellValueByColumnAndRow(3, 3, '17岁'); $sheet->setCellValueByColumnAndRow(4, 3, '165cm'); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年龄'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '欧阳克'); $sheet->setCellValueByColumnAndRow(3, 2, '18岁'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); $sheet->setCellValueByColumnAndRow(1, 3, 2); $sheet->setCellValueByColumnAndRow(2, 3, '黄蓉'); $sheet->setCellValueByColumnAndRow(3, 3, '17岁'); $sheet->setCellValueByColumnAndRow(4, 3, '165cm'); $sheet->getStyle('B2')->getFont()->setBold(true)->setName('宋体')->setSize(20); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年龄'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '欧阳克'); $sheet->setCellValueByColumnAndRow(3, 2, '18岁'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); $sheet->setCellValueByColumnAndRow(1, 3, 2); $sheet->setCellValueByColumnAndRow(2, 3, '黄蓉'); $sheet->setCellValueByColumnAndRow(3, 3, '17岁'); $sheet->setCellValueByColumnAndRow(4, 3, '165cm'); $sheet->getStyle('B2')->getFont()->getColor()->setRGB('#AEEEEE'); echo $sheet->getStyle('B2')->getFont()->getColor()->getRGB(),PHP_EOL; $sheet->getStyle('B3')->getFont()->getColor()->setARGB('FFFF0000'); echo $sheet->getStyle('B3')->getFont()->getColor()->getARGB(); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','2019-10-10 10:10:10'); $sheet->setCellValue('A2','2019-10-10 10:10:10'); $sheet->getStyle('A2')->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_YYYYMMDD2); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1',"欧阳克\n黄蓉"); $sheet->getStyle('A1')->getAlignment()->setWrapText(true); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','www.php.cn'); $sheet->getCell('A1')->getHyperlink()->setUrl('http://www.php.cn'); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','10'); $sheet->setCellValue('B1','15'); $sheet->setCellValue('C1','20'); $sheet->setCellValue('D1','25'); $sheet->setCellValue('E1','30'); $sheet->setCellValue('G1','35'); $sheet->setCellValue('A2', '总数:'); $sheet->setCellValue('B2', '=SUM(A1:G1)'); $sheet->setCellValue('A3', '平均数:'); $sheet->setCellValue('B3', '=AVERAGE(A1:G1)'); $sheet->setCellValue('A4', '最小数:'); $sheet->setCellValue('B4', '=MIN(A1:G1)'); $sheet->setCellValue('A5', '最大数:'); $sheet->setCellValue('B5', '=MAX(A1:G1)'); $sheet->setCellValue('A6', '最大数:'); $sheet->setCellValue('B6', '\=MAX(A1:G1)'); // 使用转义字符 # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年龄'); $sheet->setCellValue('D1','身高'); $sheet->fromArray( [ [1,'欧阳克','18岁','188cm'], [2,'黄蓉','17岁','165cm'], [3,'郭靖','21岁','180cm'] ], 3, 'A2' ); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->mergeCells('A1:B5'); $sheet->getCell('A1')->setValue('欧阳克'); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
合并后,赋值只能给A1,开始的坐标。
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->mergeCells('A1:B5'); $sheet->unmergeCells('A1:B5'); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); echo $sheet->getColumnDimension('A')->getWidth(); $sheet->getColumnDimension('A')->setWidth(100); $sheet->getColumnDimension('B')->setAutoSize(true); $sheet->getDefaultColumnDimension()->setWidth(1); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); echo $sheet->getRowDimension(1)->getRowHeight(); $sheet->getRowDimension(1)->setRowHeight(100); $sheet->getDefaultRowDimension()->setRowHeight(1); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年龄'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '欧阳克'); $sheet->setCellValueByColumnAndRow(3, 2, '18岁'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); $sheet->setCellValueByColumnAndRow(1, 3, 2); $sheet->setCellValueByColumnAndRow(2, 3, '黄蓉'); $sheet->setCellValueByColumnAndRow(3, 3, '17岁'); $sheet->setCellValueByColumnAndRow(4, 3, '165cm'); echo $sheet->getHighestColumn(); echo $sheet->getHighestRow(); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年龄'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '欧阳克'); $sheet->setCellValueByColumnAndRow(3, 2, '18岁'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); $styleArray = [ // use PhpOffice\PhpSpreadsheet\Style\Alignment; 文件里常量,就是参数 // Alignment::HORIZONTAL_CENTER 水平居中 // Alignment::VERTICAL_CENTER 垂直居中 'alignment' => [ // 'horizontal' => Alignment::HORIZONTAL_CENTER, //水平居中 // 'vertical' => Alignment::VERTICAL_CENTER, //垂直居中 'horizontal' => 'center', //水平居中 'vertical' => 'center', //垂直居中 ], // use PhpOffice\PhpSpreadsheet\Style\Border; 文件里常量,就是参数 // Border::BORDER_THICK 边框样式 'borders' => [ 'outline' => [ // 'borderStyle' => '\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK', 'borderStyle' => 'thick', 'color' => ['argb' => 'FFFF0000'], ], ], 'font' => [ 'name' => '黑体', 'bold' => true, 'size' => 22 ] ]; $sheet->getStyle('A1')->applyFromArray($styleArray); # Xlsx类 将电子表格保存到文件 use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $writer = new Xlsx($spreadsheet); $writer->save('1.xlsx');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年龄'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '欧阳克'); $sheet->setCellValueByColumnAndRow(3, 2, '18岁'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); // MIME 协议,文件的类型,不设置,会默认html header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // MIME 协议的扩展 header('Content-Disposition:attachment;filename=1.xlsx'); // 缓存控制 header('Cache-Control:max-age=0'); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); // php://output 它是一个只写数据流, 允许你以 print 和 echo一样的方式写入到输出缓冲区。 $writer->save('php://output');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年龄'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '欧阳克'); $sheet->setCellValueByColumnAndRow(3, 2, '18岁'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); $filename = '1.xls'; header('Content-Type:application/vnd.ms-excel'); header('Content-Disposition:attachment;filename=1.xls'); header('Cache-Control:max-age=0'); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls'); $writer->save('php://output');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','姓名'); $sheet->setCellValue('C1','年龄'); $sheet->setCellValue('D1','身高'); $sheet->setCellValueByColumnAndRow(1, 2, 1); $sheet->setCellValueByColumnAndRow(2, 2, '欧阳克'); $sheet->setCellValueByColumnAndRow(3, 2, '18岁'); $sheet->setCellValueByColumnAndRow(4, 2, '188cm'); $sheet->setTitle('欧阳克'); // MIME 协议,文件的类型,不设置,会默认html header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // MIME 协议的扩展 header('Content-Disposition:attachment;filename=1.xlsx'); // 缓存控制 header('Cache-Control:max-age=0'); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); // php://output 它是一个只写数据流, 允许你以 print 和 echo一样的方式写入到输出缓冲区。 $writer->save('php://output');
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 创建读操作 $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx'); # 打开文件、载入excel表格 $spreadsheet = $reader->load('1.xlsx'); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); # 获取 单元格值 和 坐标 $cellC1 = $sheet->getCell('B2'); echo '值: ', $cellC1->getValue(),PHP_EOL; echo '坐标: ', $cellC1->getCoordinate(),PHP_EOL; $sheet->setCellValue('B2','欧阳锋'); # 获取 单元格值 和 坐标 $cellC2 = $sheet->getCell('B2'); echo '值: ', $cellC2->getValue(),PHP_EOL; echo '坐标: ', $cellC2->getCoordinate();
后缀 | MIME Type |
---|---|
.doc | application/msword |
.dot | application/msword |
.docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
.dotx | application/vnd.openxmlformats-officedocument.wordprocessingml.template |
.docm | application/vnd.ms-word.document.macroEnabled.12 |
.dotm | application/vnd.ms-word.template.macroEnabled.12 |
.xls | application/vnd.ms-excel |
.xlt | application/vnd.ms-excel |
.xla | application/vnd.ms-excel |
.xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
.xltx | application/vnd.openxmlformats-officedocument.spreadsheetml.template |
.xlsm | application/vnd.ms-excel.sheet.macroEnabled.12 |
.xltm | application/vnd.ms-excel.template.macroEnabled.12 |
.xlam | application/vnd.ms-excel.addin.macroEnabled.12 |
.xlsb | application/vnd.ms-excel.sheet.binary.macroEnabled.12 |
.ppt | application/vnd.ms-powerpoint |
.pot | application/vnd.ms-powerpoint |
.pps | application/vnd.ms-powerpoint |
.ppa | application/vnd.ms-powerpoint |
.pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation |
.potx | application/vnd.openxmlformats-officedocument.presentationml.template |
.ppsx | application/vnd.openxmlformats-officedocument.presentationml.slideshow |
.ppam | application/vnd.ms-powerpoint.addin.macroEnabled.12 |
.pptm | application/vnd.ms-powerpoint.presentation.macroEnabled.12 |
.potm | application/vnd.ms-powerpoint.presentation.macroEnabled.12 |
.ppsm | application/vnd.ms-powerpoint.slideshow.macroEnabled.12 |
CREATE TABLE `login_log` ( `id` int(11) NOT NULL AUTO_INCREMENT, `uid` int(11) DEFAULT NULL COMMENT '管理员ID', `client` tinyint(4) unsigned DEFAULT '0' COMMENT '0-PC 1-ios 2-android', `add_time` int(11) DEFAULT '0' COMMENT '创建时间', `ip` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '登录IP', PRIMARY KEY (`id`) USING BTREE ) ENGINE=MyISAM AUTO_INCREMENT=1122 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='登录日志';
<?php # 载入方法库 require 'function.php'; $select = select('login_log','*'); if(empty($select)){ exit; }else{ foreach($select as &$v){ switch ($v['client']) { case 0: $v['client'] = 'PC电脑'; break; case 1: $v['client'] = '苹果手机'; break; case 2: $v['client'] = '安卓手机'; break; } $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>导出数据</title> <link rel="stylesheet" href="layui/css/layui.css" rel="external nofollow" rel="external nofollow" > </head> <body> <div style="text-align:center;"> <a href="download.php" rel="external nofollow" rel="external nofollow"
class="layui-btn layui-btn-radius layui-btn-danger">导出数据</a> </div> <table class="layui-table"> <thead> <tr> <th>ID</th> <th>用户ID</th> <th>登陆设备</th> <th>登陆时间</th> <th>登陆ip</th> </tr> </thead> <tbody> <?php foreach($select as $v){ ?> <tr> <td><?php echo $v['id'] ?></td> <td><?php echo $v['uid'] ?></td> <td><?php echo $v['client'] ?></td> <td><?php echo $v['add_time'] ?></td> <td><?php echo $v['ip'] ?></td> </tr> <?php } ?> </tbody> </table> </body> </html>
<?php # 载入方法库 require 'function.php'; $select = select('login_log','*'); if(empty($select)){ exit; }else{ foreach($select as &$v){ switch ($v['client']) { case 0: $v['client'] = 'PC电脑'; break; case 1: $v['client'] = '苹果手机'; break; case 2: $v['client'] = '安卓手机'; break; } $v['add_time'] = date('Y-m-d H:i:s',$v['add_time']); } } # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use \PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','ID'); $sheet->setCellValue('B1','用户ID'); $sheet->setCellValue('C1','登陆设备'); $sheet->setCellValue('D1','登陆时间'); $sheet->setCellValue('E1','登陆ip'); $sheet->fromArray( $select, null, 'A2' ); // MIME 协议,文件的类型,不设置,会默认html header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // MIME 协议的扩展 header('Content-Disposition:attachment;filename=1.xlsx'); // 缓存控制 header('Cache-Control:max-age=0'); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); // php://output 它是一个只写数据流, 允许你以 print 和 echo一样的方式写入到输出缓冲区。 $writer->save('php://output'); ?>
# 商品分类表 CREATE TABLE `shop_cat` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', `pid` int(10) unsigned DEFAULT '0' COMMENT '父ID', `name` varchar(50) DEFAULT NULL COMMENT '分类名', `status` tinyint(1) unsigned DEFAULT '1' COMMENT '状态 1开启 0关闭', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COMMENT='分类表'; INSERT INTO `shop_cat` VALUES (1, 0, '女装', 1); INSERT INTO `shop_cat` VALUES (2, 0, '男装', 1); INSERT INTO `shop_cat` VALUES (3, 0, '孕产', 1); INSERT INTO `shop_cat` VALUES (4, 1, '连衣裙', 1); INSERT INTO `shop_cat` VALUES (5, 1, '牛仔裤', 1); INSERT INTO `shop_cat` VALUES (6, 2, '衬衫', 1); INSERT INTO `shop_cat` VALUES (7, 3, '睡衣', 1); # 商品表 CREATE TABLE `shop_list` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `cat_id` int(10) unsigned DEFAULT NULL COMMENT '分类ID', `cat_fid` int(10) unsigned DEFAULT NULL COMMENT '分类父ID', `title` varchar(200) NOT NULL COMMENT '商品标题', `price` double(10,2) unsigned NOT NULL COMMENT '价格', `img` varchar(200) NOT NULL COMMENT '商品图片', `add_time` int(10) unsigned NOT NULL COMMENT '添加时间', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='商品表';
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>导出数据</title> <link rel="stylesheet" href="layui/css/layui.css" rel="external nofollow" rel="external nofollow" > </head> <body> <div style="text-align:center;"> <button type="button" class="layui-btn" id="up"><i class="layui-icon">�v</i>上传文件</button> <a href="download.php" rel="external nofollow" rel="external nofollow"
class="layui-btn layui-btn-danger"><i class="layui-icon">�v</i>示例下载</a> </div> <div id="log" style="text-align:center;"> </div> </body> </html> <script src="layui/layui.js" charset="utf-8"></script> <script> layui.use('upload', function(){ var $ = layui.jquery ,upload = layui.upload; upload.render({ elem: '#up' ,url: 'data.php' ,accept: 'file' //普通文件 ,done: function(res){ if(res.code == 0){ for(var i=0;i<res.data.length;i++){ $("#log").append('<div>'+res.data[i]+'</div>'); } } } }); }) </script>
<?php $file = $_FILES['file']['tmp_name']; # 载入composer自动加载文件 require 'vendor/autoload.php'; # 载入方法库 require 'function.php'; # 创建读操作 $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx'); # 打开文件、载入excel表格 $spreadsheet = $reader->load($file); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); # 获取总列数 $highestColumn = $sheet->getHighestColumn(); # 获取总行数 $highestRow = $sheet->getHighestRow(); # 列数 改为数字显示 $highestColumnIndex = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString($highestColumn); $log = []; for($a=2;$a<$highestRow;$a++){ $title = $sheet->getCellByColumnAndRow(1,$a)->getValue(); $cat_fname = $sheet->getCellByColumnAndRow(2,$a)->getValue(); $cat_name = $sheet->getCellByColumnAndRow(3,$a)->getValue(); $price = $sheet->getCellByColumnAndRow(4,$a)->getValue(); $img = $sheet->getCellByColumnAndRow(5,$a)->getValue(); $cat_fid = find('shop_cat','id','name="'.$cat_fname.'"'); $cat_id = find('shop_cat','id','name="'.$cat_name.'"'); $data = [ 'title' => $title, 'cat_fid' => $cat_fid['id'], 'cat_id' => $cat_id['id'], 'price' => $price, 'img' => $img, 'add_time' => time(), ]; $ins = insert('shop_list',$data); if($ins){ $log[] = '第'.$a.'条,插入成功'; }else{ $log[] = '第'.$a.'条,插入失败'; } } echo json_encode(['code'=>0,'msg'=>'成功','data'=>$log]);
<?php # 载入composer自动加载文件 require 'vendor/autoload.php'; # 给类文件的命名空间起个别名 use \PhpOffice\PhpSpreadsheet\Spreadsheet; # 实例化 Spreadsheet 对象 $spreadsheet = new Spreadsheet(); # 获取活动工作薄 $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1','商品标题'); $sheet->setCellValue('B1','一级分类'); $sheet->setCellValue('C1','二级分类'); $sheet->setCellValue('D1','进货价'); $sheet->setCellValue('E1','图片'); $data = [ '云朵般轻盈的仙女裙 高级钉珠收腰长裙 气质无袖连衣裙', '女装', '连衣裙', 279.99, 'https://gd3.alicdn.com/imgextra/i3/266969832/O1CN01PWUBBB2MV6ekBKtb6_!!266969832.jpg_400x400.jpg', ]; $sheet->fromArray( $data, null, 'A2' ); // MIME 协议,文件的类型,不设置,会默认html header('Content-Type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // MIME 协议的扩展 header('Content-Disposition:attachment;filename=商品列表示例.xlsx'); // 缓存控制 header('Cache-Control:max-age=0'); $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx'); // php://output 它是一个只写数据流, 允许你以 print 和 echo一样的方式写入到输出缓冲区。 $writer->save('php://output');
以上就是关于PHP使用PhpSpreadsheet操作Excel的介绍,上述操作技巧有一定的参考价值,有需要的朋友可以看一下,希望文本对大家学习有帮助,想要了解更多PHP操作Excel的内容大家可以继续关注其他文章。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
在做开发时,我们可能会遇到需要上传多张图片的需求,那么我具体要如何来实现呢?下面小编就给大家分享一下如何使用TP5框架实现上传多张图片的方法,感兴趣的朋友可以看一看。
在thinkphp中,distinct()方法用于去除重复的值,能够返回唯一不同的值,distinct方法的参数是一个布尔值,语法为“$Model->distinct(布尔值参数)->field('name')->select();”。
今天小编就为大家分享一篇解决tp5在nginx下修改配置访问的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
首次安装phpstorm是运行不了PHP文件的,因为环境没配置好。下面我将以PHPstorm和wampserver来配置PHP运行环境。
PHP日期和时间函数怎么用?常用的时间函数有哪些?对于PHP初学者来说,可能对于日期与时间函数不是很清楚,因此,这篇文章就给大家介绍常用的时间函数及用法,有需要的朋友可以参考学习。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008