php页面数据excel文件,使用PhpSpreadsheet将数据导出为Excel文件

打开demo页面,我们会看到了一个学生成绩列表,现在我们要做的是:点击列表下方的“导出Excel表格”按钮,即可将数据下载并保存为Excel文件。

点击按钮后,请求了export.php,那么我们来看export.php文件:

1.设置表头

首先我们引入自动加载PhpSpreadsheet库,然后实例化,设置工作表标题名称为:学生成绩表,接着设置表头内容。表头分为两行,第一行是表格的名称,第二行数表格列名称。最后我们将第一行单元格进行合并,并设置表头内容样式:字体、对齐方式等。

require 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\Spreadsheet;

use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

include('conn.php'); //连接数据库

$spreadsheet = new Spreadsheet();

$worksheet = $spreadsheet->getActiveSheet();

//设置工作表标题名称

$worksheet->setTitle('学生成绩表');

//表头

//设置单元格内容

$worksheet->setCellValueByColumnAndRow(1, 1, '学生成绩表');

$worksheet->setCellValueByColumnAndRow(1, 2, '姓名');

$worksheet->setCellValueByColumnAndRow(2, 2, '语文');

$worksheet->setCellValueByColumnAndRow(3, 2, '数学');

$worksheet->setCellValueByColumnAndRow(4, 2, '外语');

$worksheet->setCellValueByColumnAndRow(5, 2, '总分');

//合并单元格

$worksheet->mergeCells('A1:E1');

$styleArray = [

'font' => [

'bold' => true

],

'alignment' => [

'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,

],

];

//设置单元格样式

$worksheet->getStyle('A1')->applyFromArray($styleArray)->getFont()->setSize(28);

$worksheet->getStyle('A2:E2')->applyFromArray($styleArray)->getFont()->setSize(14);

2.读取数据

我们连接数据库后,直接读取学生成绩表t_student,然后for循环,设置每个单元格对应的内容,计算总成绩。注意的是表格中的数据是从第3行开始,因为第1,2行是表头占用了。

然后,我们设置整个表格样式,给表格加上边框,并且居中对齐。

$sql = "SELECT id,name,chinese,maths,english FROM `t_student`";

$stmt = $db->query($sql);

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

$len = count($rows);

$j = 0;

for ($i=0; $i < $len; $i++) {

$j = $i + 3; //从表格第3行开始

$worksheet->setCellValueByColumnAndRow(1, $j, $rows[$i]['name']);

$worksheet->setCellValueByColumnAndRow(2, $j, $rows[$i]['chinese']);

$worksheet->setCellValueByColumnAndRow(3, $j, $rows[$i]['maths']);

$worksheet->setCellValueByColumnAndRow(4, $j, $rows[$i]['english']);

$worksheet->setCellValueByColumnAndRow(5, $j, $rows[$i]['chinese'] + $rows[$i]['maths'] + $rows[$i]['english']);

}

$styleArrayBody = [

'borders' => [

'allBorders' => [

'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,

'color' => ['argb' => '666666'],

],

],

'alignment' => [

'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,

],

];

$total_rows = $len + 2;

//添加所有边框/居中

$worksheet->getStyle('A1:E'.$total_rows)->applyFromArray($styleArrayBody);

如果仅是为了满足文章开头说的老板的需求,我们这个时候就可以将数据保存为Excel文件,当然这个Excel文件只保存在服务器上,然后再使用邮件等方式将Excel发送给老板就结了。

但是我们更多的应用场景是用户直接将数据下载导出为Excel表格文件,请接着看:

3.下载保存

最后,我们强制浏览器下载数据并保存为Excel文件。

$filename = '成绩表.xlsx';

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

header('Content-Disposition: attachment;filename="'.$filename.'"');

header('Cache-Control: max-age=0');

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');

$writer->save('php://output');

如果你想要保存为.xls文件格式的话,可以改下header代码:

$filename = '成绩表.xlsx';

header('Content-Type: application/vnd.ms-excel');

header('Content-Disposition: attachment;filename="'.$filename.'"');

header('Cache-Control: max-age=0');

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'xls');

$writer->save('php://output');

完整的代码请下载源码。

PhpSpreadsheet提供了很多选项设置,接下来的文章我会专门介绍有关生成Excel的设置,如样式:字体、对齐、颜色、行高于列宽,合并与拆分、图片、日期时间、换行、函数使用等等。敬请关注。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值