1、composer安装:

composer require phpoffice/phpspreadsheet

2.点击导出按钮,触发控制器里面的方法 wdjzdc()

3. 在控制中引入

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

//wdjzdc数据导出
public function wdjzdc()
{
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
//设置工作表标题名称
$worksheet->setTitle('wdjz客户表'); //表头
//设置单元格内容
$worksheet->setCellValueByColumnAndRow(1, 1, 'wdjz无毒样板客户表');
$worksheet->setCellValueByColumnAndRow(1, 2, 'name');
$worksheet->setCellValueByColumnAndRow(2, 2, 'phone');
$worksheet->setCellValueByColumnAndRow(3, 2, 'radio1');
$worksheet->setCellValueByColumnAndRow(4, 2, 'style');
$worksheet->setCellValueByColumnAndRow(5, 2, 'time'); //合并单元格
$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);
;
$jzInfo = db('wdjz')->select();
$len = count($jzInfo);
$j = 0;
for ($i=0; $i < $len; $i++) {
$j = $i + 3; //从表格第3行开始 $worksheet->setCellValueByColumnAndRow(1, $j, $jzInfo[$i]['name']);
$worksheet->setCellValueByColumnAndRow(2, $j, $jzInfo[$i]['phone']);
$worksheet->setCellValueByColumnAndRow(3, $j, $jzInfo[$i]['radio1']);
$worksheet->setCellValueByColumnAndRow(4, $j, $jzInfo[$i]['style']);
$worksheet->setCellValueByColumnAndRow(5, $j, $jzInfo[$i]['time']);
} $styleArrayBody = [
'borders' => [
'allBorders' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN,
'color' => ['argb' => '666666'],
],
],
'alignment' => [
'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
],
];
$total_jzInfo = $len + 2;
//添加所有边框/居中
$worksheet->getStyle('A1:C'.$total_jzInfo)->applyFromArray($styleArrayBody); $filename = 'wdjz无毒样板客户表.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');
}

-----------------------------------摘取大神文档---------------------------------------------------------------------

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的设置,如样式:字体、对齐、颜色、行高于列宽,合并与拆分、图片、日期时间、换行、函数使用等等。敬请关注。


												

TP5使用Composer安装phpoffice/phpspreadsheet,导出Excel文件的更多相关文章

  1. tp5.0 composer下载phpexcel 实现导出excel 表格

    tp5.0 composer下载phpexcel 实现导出excel 表格 1.在下载好的tp5.0 框架里面使用 composer 下载phpexcel 的插件 composer require p ...

  2. c# listview导出excel文件

    首先在工程中需要添加对Microsoft Excel office 11.0 object的引用 生成excel的类代码如下 using System; using System.Collection ...

  3. HTML导出Excel文件(兼容IE及所有浏览器)

    注意:IE浏览器需要以下设置: 打开IE,在常用工具栏中选择“工具”--->Internet选项---->选择"安全"标签页--->选择"自定义级别&q ...

  4. EasyUI 如何结合JS导出Excel文件

    出处:http://blog.csdn.net/jumtre/article/details/41119991 EasyUI 如何结合JS导出Excel文件 分类: 技术 Javascript jQu ...

  5. 使用vue导出excel文件

    今天再开发中遇到一件事情,就是怎样用已有数据导出excel文件,网上有许多方法,有说用数据流的方式,https://www.cnblogs.com/yeqrblog/p/9758981.html,但是 ...

  6. vue+element 表格导出Excel文件

    https://www.cnblogs.com/bobodeboke/p/8867481.html  非常感谢 这个大佬 才让我搞到了Blob.js 和 Export2Excel.js 如果最后运行时 ...

  7. 纯前端实现数据导出excel文件

    一  安装依赖 npm install -S file-saver xlsx npm install -D script-loader 二 新建文件夹 在网上百度引入即可 三 在main.js中引入 ...

  8. ASP.NET Core导入导出Excel文件

    ASP.NET Core导入导出Excel文件 希望在ASP.NET Core中导入导出Excel文件,在网上搜了一遍,基本都是使用EPPlus插件,EPPlus挺好用,但商用需要授权,各位码友若有好 ...

  9. 前端必读3.0:如何在 Angular 中使用SpreadJS实现导入和导出 Excel 文件

    在之前的文章中,我们为大家分别详细介绍了在JavaScript.React中使用SpreadJS导入和导出Excel文件的方法,作为带给广大前端开发者的"三部曲",本文我们将为大家 ...

随机推荐

  1. JS如何获取PHP循环中的ID

    JS如何获取PHP循环中的ID  kaalrz 二路公交车    结帖率:83.33%   首先抱歉,因为昨天那帖图片几次都不能用,修改到不能再次修改,今天早上回帖又提示没有这个帖,只好重发一次. 如 ...

  2. Picard Tools

    Picard Tools - By Broad Institute http://broadinstitute.github.io/picard/command-line-overview.html ...

  3. java网络爬虫实现信息的抓取

    转载请注明出处:http://blog.csdn.NET/lmj623565791/article/details/23272657 今天公司有个需求,需要做一些指定网站查询后的数据的抓取,于是花了点 ...

  4. hdu-1176(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1176 思路:类似数塔问题, 从最后一秒开始考虑,每次这一秒的状态确定意味着前一秒的状态也已经确定,所以 ...

  5. APMServ—优秀的PHP集成环境工具

    经常折腾wordpress和各种php开发的cms,免不了要在本地测试这些程序,所以选择一款好的php集成环境就至关重要啦.之前在月光博客上看到有一篇“常见的WAMP集成环境”介绍,然后先后试用过XA ...

  6. Leed code 11. Container With Most Water

    public int maxArea(int[] height) { int left = 0, right = height.length - 1; int maxArea = 0; while ( ...

  7. 关于this对象

    1.在全局函数中this指的是window 2.当函数被当做方法调用时,this等于那个对象 3.匿名函数具有全局性,只要是匿名函数,this指向window 实例1: var name = 'the ...

  8. Navicat如何导出数据库的svg、pdf,png图片

    有时候各位可能有这么一种感觉,如果一个数据库中的表太多的话,查看起来不大方便,如果你习惯用navicat软件来查看er图的话,那也是更困难了,这里介绍一种方法,就是把这些关系结构导出一个可以用浏览器打 ...

  9. 零停重启程序工具Huptime研究

    目录 目录 1 1. 官网 1 2. 功能 1 3. 环境要求 2 4. 实现原理 2 5. SIGHUP信号处理 3 6. 重启线程 4 7. 重启目标程序 5 8. 系统调用钩子辅助 6 9. 被 ...

  10. Codeforces805 C. Find Amir 2017-05-05 08:41 140人阅读 评论(0) 收藏

    C. Find Amir time limit per test 1 second memory limit per test 256 megabytes input standard input o ...