thinkphp中如何使用phpspreadsheet插件

一、总结

一句话总结:多百度,百度什么都有

1、thinkphp中用composer安装的插件的命名空间是什么?

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

2、插件的代码实例哪里找?

插件自带代码实例

\vendor\phpoffice\phpspreadsheet\samples\里面有例程,仔细看下

3、在thinkphp中安装好了插件,我们需要在入口文件中require包么?

不需要,thinkphp已经给我们做了

注意:不需要自己在入口文件中再require包了

第十八行代码不需要

 1 <?php
2 // +----------------------------------------------------------------------
3 // | ThinkPHP [ WE CAN DO IT JUST THINK ]
4 // +----------------------------------------------------------------------
5 // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
6 // +----------------------------------------------------------------------
7 // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8 // +----------------------------------------------------------------------
9 // | Author: liu21st <liu21st@gmail.com>
10 // +----------------------------------------------------------------------
11
12 // [ 应用入口文件 ]
13
14 // 定义应用目录
15 define('APP_PATH', __DIR__ . '/../application/');
16 // 加载框架引导文件
17 require __DIR__ . '/../thinkphp/start.php';
18 //require __DIR__ . '/../vendor/autoload.php';

4、我们要调用自己安装的插件的类,use的格式是怎样的?

其实use就是永远和文件的命名空间是相对应的

直接看需要文件定义的命名空间:比如说下面第一个文件Spreadsheet的命名空间就是这个:namespace PhpOffice\PhpSpreadsheet;

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

二、TP5使用Composer安装PhpSpreadsheet类库实现导出Excel表并封装

一、背景介绍:

PhpSpreadsheet是PHPExcel的下一个版本。它打破了兼容性,大大提高了代码库质量(命名空间,PSR合规性,最新PHP语言功能的使用等)。

由于所有努力都转移到了PhpSpreadsheet,因此将不再维护PHPExcel。PHPExcel,补丁和新功能的所有贡献都应该针对PhpSpreadsheet开发分支。

前提:TP5项目中已经安装配置好Composer 管理工具包。

二、Composer 中文网 / Packagist 中国全量镜像https://www.phpcomposer.com/ 打开安装包列表搜索phpspreadsheet

资源链接:https://packagist.org/packages/phpoffice/phpspreadsheet 复制命令composer require phpoffice/phpspreadsheet,

在开发工具(比如:PHPSTORM)命令行(terminal)中执行;

三、创建Office类文件并封装,在需要导出Excel表的地方引入即可:

<?php

namespace app\index\controller;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; class Office
{ /**
* 导出excel表
* $data:要导出excel表的数据,接受一个二维数组
* $name:excel表的表名
* $head:excel表的表头,接受一个一维数组
* $key:$data中对应表头的键的数组,接受一个一维数组
* 备注:此函数缺点是,表头(对应列数)不能超过26;
*循环不够灵活,一个单元格中不方便存放两个数据库字段的值
*/
public function outdata($name='测试表', $data=[], $head=[], $keys=[])
{
$count = count($head); //计算表头数量 $spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet(); for ($i = 65; $i < $count + 65; $i++) { //数字转字母从65开始,循环设置表头:
$sheet->setCellValue(strtoupper(chr($i)) . '1', $head[$i - 65]);
} /*--------------开始从数据库提取信息插入Excel表中------------------*/ foreach ($data as $key => $item) { //循环设置单元格:
//$key+2,因为第一行是表头,所以写到表格时 从第二行开始写 for ($i = 65; $i < $count + 65; $i++) { //数字转字母从65开始:
$sheet->setCellValue(strtoupper(chr($i)) . ($key + 2), $item[$keys[$i - 65]]);
$spreadsheet->getActiveSheet()->getColumnDimension(strtoupper(chr($i)))->setWidth(20); //固定列宽
} } header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $name . '.xlsx"');
header('Cache-Control: max-age=0');
$writer = new Xlsx($spreadsheet);
$writer->save('php://output'); //删除清空:
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
exit;
}

调用示例:


$excel = new Office(); //设置表头:
$head = ['订单编号', '商品总数', '收货人', '联系电话', '收货地址']; //数据中对应的字段,用于读取相应数据:
$keys = ['order_sn', 'num', 'consignee', 'phone', 'detail']; $excel->outdata('订单表', $orders, $head, $keys);

参考:TP5使用Composer安装PhpSpreadsheet类库实现导出Excel表并封装 - 逍逍逍遥虎的博客 - CSDN博客
https://blog.csdn.net/qq_41962562/article/details/82315608

 

三、TP5,用composer加载了phpSpreadSheet后怎么用呢?

\vendor\phpoffice\phpspreadsheet\samples\里面有例程,仔细看下

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

 use PhpOffice\PhpSpreadsheet\IOFactory;
$path = ROOT_PATH . "public/" . $filename;
setlocale(LC_ALL, 'zh_CN'); //csv中文乱码
$inputFileType = IOFactory::identify($path);
$excelReader = IOFactory::createReader($inputFileType);
if ($inputFileType == 'Csv') { //csv文件读取设置
$excelReader->setInputEncoding('GBK');
$excelReader->setDelimiter(',');
}
$phpexcel = $excelReader->load($path);
$activeSheet = $phpexcel->getActiveSheet();
$sheet = $activeSheet->toArray();
上面一个是读取,写出的代码就不贴了,找下phpexcel的改下命名空间就差不多了,
 
 use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;

四、自己测试实例(亲测通过)

比如我们现在在thinkphp5中要用phpspreadsheet插件

第一步:找到插件官网的composer安装方法

Installation

Use composer to install PhpSpreadsheet into your project:

composer require phpoffice/phpspreadsheet

Note: If you want the unreleased, unstable development version use phpoffice/phpspreadsheet:dev-develop instead.

第二步:在我们tp5系统的更目录下打开cmd命令行,运行这句话(要先在电脑上面装composer)

第三步:直接use包运行实例,不需要引(require)包(thinkphp已经帮我们做了,再引会造成一些方法重复定义)

这样直接就用了

 <?php
namespace app\admin\controller\test\phpspreadsheet; use app\admin\controller\Base; use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; class Index extends Base
{
public function index()
{
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !'); $writer = new Xlsx($spreadsheet);
$writer->save('d://hello world.xlsx');
//dump('2222222222');die;
//return view();
}
}

注意:不需要自己在入口文件中再require包了

第十八行代码不需要

 <?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +---------------------------------------------------------------------- // [ 应用入口文件 ] // 定义应用目录
define('APP_PATH', __DIR__ . '/../application/');
// 加载框架引导文件
require __DIR__ . '/../thinkphp/start.php';
//require __DIR__ . '/../vendor/autoload.php';
 
 

最新文章

  1. js ShowDialogModal 关闭子页面并刷新父页面,保留查询条件
  2. C语言基础--变量存储细节
  3. C#全局作用符::
  4. lintcode :Trailing Zeros 尾部的零
  5. The Story of self Parameter in Python, Demystified
  6. ORACLE数据库闪回日志写满
  7. 单纯觉得是篇好文——跨终端Web之Hybrid App
  8. 记一下webstorm快键键
  9. BezierDemo开源项目的学习
  10. 【bzoj 4449】[Neerc2015]Distance on Triangulation
  11. 单片机的外围功能电路 LET′S TRY“嵌入式编程”: 2 of 6
  12. JSON格式说明
  13. 深度学习(TensorFlow)环境搭建:(三)Ubuntu16.04+CUDA8.0+cuDNN7+Anaconda4.4+Python3.6+TensorFlow1.3
  14. Windows SubSystem for Linux
  15. 学习CSS布局 - margin: auto;
  16. C# 16进制与字符串、字节数组之间的转换 (转载)
  17. MySql语句常用命令整理---多表查询
  18. 题解——CodeForces 438D The Child and Sequence
  19. C#基础知识回顾:2.使用DbProviderFactory实现多数据库访问
  20. kettle学习笔记(六)——kettle转换步骤

热门文章

  1. css选择器的权重
  2. Linux用户相关文件之密码文件
  3. Spring Cloud架构
  4. 于win2008R2虽然激活,但是一个小时之后就会自动强制关机的问题
  5. Scrapy框架2
  6. java考试
  7. 面向对象之继承(Day24)
  8. 字符编码 and 字节和字符串转换(待补充)
  9. C#:连接本地SQL Server语句
  10. ruby rails 安裝