介绍:
PhpSpreadsheet是PHPExcel的下一个版本。它打破了兼容性,大大提高了代码库质量(命名空间,PSR合规性,最新PHP语言功能的使用等)。
由于所有努力都转移到了PhpSpreadsheet,因此将不再维护PHPExcel。PHPExcel,补丁和新功能的所有贡献都应该针对PhpSpreadsheet开发分支。
前提:TP5项目中已经安装配置好Composer 管理工具包。

安装:
命令 composer require phpoffice/phpspreadsheet

前端上传页面:

 <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>index</title>
<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<!--[if lt IE 9]>
<script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
<![endif]-->
</head>
<body> <div class="container"> <form action="http://test.tp51.com/index/index/uploadexcel" enctype="multipart/form-data" method="post">
<div class="form-group">
<label for="exampleInputFile">选择文件</label>
<input type="file" id="excelFile" name="file" accept=".csv,.xls,.xlsx">
<p class="help-block">请选择表格文件,支持 csv,xls,xlsx 格式</p>
</div> <div class="form-group">
<label for="columm" class="control-label">选择总列数</label>
<select class="form-control" name="columm" id="columm"><option>A</option><option>B</option><option>C</option><option>D</option><option>E</option><option>F</option><option>G</option><option>H</option><option>I</option><option>J</option><option>K</option><option>L</option><option>M</option><option>N</option></select>
</div> <div class="form-group">
<button type="submit" class="btn btn-default">上传</button>
</div>
</form> </div> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script type="text/javascript">
<!--
$("button[type='submit']").on("click",function(){
var filestr = $("#excelFile").val();
var filemate = getFilemate(filestr);
console.log(filemate);
if (filemate =='xls' || filemate =='xlsx' || filemate =='csv') {
console.log("文件正确");
$("form").submit();
}else{
alert("文件格式不正确");
return false;
}
}); //获取文件格式-后缀
function getFilemate(o){
var index = o.lastIndexOf(".")
if (index==-1) {return false;}
return o.substring(index+1);
}
//-->
</script> </body>
</html>

后端接收处理:

 <?php
namespace app\index\controller; use think\Db;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; class Index
{
public function index(){
return view();
} //上传表格文件
public function uploadexcel(){
$input_columm = input('post.columm/s');
$file = request()->file("file");
$info = $file->validate(['size'=>5242880,'ext'=>'csv,xls,xlsx'])->rule("date")->move("./uploads/DMH",$this->setupname());
if ($info) {
//上传成功,处理表格文件
$data = array();
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader(ucfirst($info->getExtension()));
$spreadsheet = $reader->load($info->getPathname());
$sheet = $spreadsheet->getSheet(0); // 读取第一個工作表
$highest_row = $sheet->getHighestRow(); // 取得总行数
//$highest_columm = $sheet->getHighestColumn(); // 取得总列数
$highest_columm = $input_columm ? $input_columm : $sheet->getHighestColumn(); // 总列数,根据实际情况修改
for ($row = 1; $row <= $highest_row; $row++){ //行号从1开始
$arr = array();
for ($column = 'A'; $column <= $highest_columm; $column++){ //列数是以A列开始
$str = $sheet->getCell($column . $row)->getValue();
array_push($arr,$str);
}
if (array_filter($arr)) {
$data[$row] = array_filter($arr);
}
}
//var_dump($data);
return view('excel',['data'=>$data]);
} else {
echo $file->getError();
}
} //导出excel
public function outexcel(){
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World !');
$writer = new Xlsx($spreadsheet);
$writer->save('hello world2.xlsx');
} }

PhpSpreadsheet处理表格的更多相关文章

  1. PhpSpreadsheet处理表格2

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  2. phpspreadsheet导出数据到Excel

    之前我们使用PHP导出Excel数据时使用的是PHPExcel库,但是phpoffice已经官方宣布PHPExcel已经被废弃不在维护,推荐使用phpspreadsheet,如下图所示 我们可以通过c ...

  3. PHP7 学习笔记(十二)PHPExcel vs PhpSpreadsheet and PHP_XLSXWriter

    前言 PhpSpreadsheet是PHPExcel的下一个版本. 它打破了兼容性,极大地提高了代码库的质量(命名空间,PSR合规性,使用最新的PHP语言功能等).由于所有努力都转移到了PhpSpre ...

  4. php+phpspreadsheet读取Excel数据存入mysql

    先生成Excel模板,然后导入Excel数据到mysql,每条数据对应图片上传到阿里云 <?php /** * Created by PhpStorm. * User: Administrato ...

  5. 详解PhpSpreadsheet设置单元格

    PhpSpreadsheet提供了丰富的API接口,可以设置诸多单元格以及文档属性,包括样式.图片.日期.函数等等诸多应用,总之你想要什么样的Excel表格,PhpSpreadsheet都能做到. 在 ...

  6. 使用PhpSpreadsheet将Excel导入到MySQL数据库

    本文以导入学生成绩表为例,给大家讲解使用PhpSpreadsheet将Excel导入的MySQL数据库. 准备 首先我们需要准备一张MySQL表,表名t_student,表结构如下: CREATE T ...

  7. TP5使用Composer安装phpoffice/phpspreadsheet,导出Excel文件

    1.composer安装: composer require phpoffice/phpspreadsheet 2.点击导出按钮,触发控制器里面的方法 wdjzdc() 3. 在控制中引入 use P ...

  8. phpspreadsheet开发手记

    坑安装简单示例通过模板来生成文件释放内存单元格根据索引获取英文列设置值合并单元格居中显示宽度设置批量设置单元格格式直接输出下载自动计算列宽函数formula单元格变可点击的超链 PhpSpreadsh ...

  9. PhpSpreadsheet如何读取excel文件

    PhpSpreadsheet如何读取excel文件 一.总结 一句话总结:万能的百度,直接搜代码就好,绝对有,毕竟github上面4000+的关注,说明很多人用了这个,使用照着demo倒是异常简单 二 ...

随机推荐

  1. PL/SQL简单使用——导入、导出数据表

    1.使用PL/SQL导出.导入表 在使用PL/SQL操作oracle数据库时,经常使用的一个操作就是将自己写的数据表导出,或者想把他人的数据表导入到自己的数据库中.虽然是很简单的操作,但自己之前一直出 ...

  2. unique_ptr

    [unique_ptr] unique_ptr 不共享它的指针.它无法复制到其他 unique_ptr,无法通过值传递到函数,也无法用于需要副本的任何标准模板库 (STL) 算法.只能移动unique ...

  3. UniRX简述

    UniRX:是一个Unit3D的编程框架,专注于解决异步逻辑,使得异步逻辑的实现更加简单优雅. 例如:实现“只处理第一次鼠标点击事件”: Observable.EveryUpdate() .Where ...

  4. 【笔记】Python基础六:模块module介绍及常用模块

    一,module模块和包的介绍 1,在Python中,一个.py文件就称之为一个模块(Module). 2,使用模块的好处? 最大的好处是大大提高了代码的可维护性 其次,编写代码不必从零开始,我们编写 ...

  5. 【笔记】Python基础一 :变量,控制结构,运算符及数据类型之数字,字符串,列表,元组,字典

    一,开发语言介绍 高级语言:Java,C#,Python  ==>产生字节码 低级语言:C,汇编                   ==>产生机器码 高级语言开发效率高,低级语言运行效率 ...

  6. pandas,对dataFrame中某一个列的数据进行处理

    背景:dataFrame的数据,想对某一个列做逻辑处理,生成新的列,或覆盖原有列的值   下面例子中的df均为pandas.DataFrame()的数据   1.增加新列,或更改某列的值 df[&qu ...

  7. VirtualBox 休眠恢复后无法联网解决办法

    环境:lubuntu 首先ifconfig查看网卡名,我这里是enp0s3. 接着: sudo ifconfig enp0s3 down sudo ifconfig enp0s3 up sudo dh ...

  8. git clone失败

    操作: $ git clone https://github.com/zjun615/DragListView.gitCloning into 'DragListView'...fatal: unab ...

  9. hibernate多生成一个外键以及映射文件中含有<list-index>标签

    (原文地址: http://blog.csdn.net/xiaoxian8023/article/details/15380529) 一.Inverse是hibernate双向关系中的基本概念.inv ...

  10. TZOJ 2519 Regetni(N个点求三角形面积为整数总数)

    描述 Background Hello Earthling. We're from the planet Regetni and need your help to make lots of mone ...