Laravel 上使用 phpexcel的两种方式
- 1229
文章采集与网上
方式1。使用原生的phpexcel ,
http://blog.csdn.net/CSwfe/article/details/52748046?locationNum=1
1、在app目录下创建一个新的文件夹,命名libs(可自定义) app/libs/phpExcel
2、(可选)考虑到后面可能会引用很多库,so,在libs下再创建一个phpExcel文件夹,把phpExcel类放入此文件夹下。
3、找到根目录下的composer.json文件
4、找到composer.json中定义的(看我备注)
//加入phpExcel类的路径
plain copy
- "autoload": {
- "classmap": [
- "database",
- "app/libs/phpExcel"
- ],
- "psr-4": {
- "App\\": "app/"
- }
- },
5、安装composer,windows下可以在百度上下载
6、运行命令行进入项目根目录,执行“composer dumpautoload”,
7、在控制器中use PHPExcel
8、在方法中实例化phpExccel对象,打印该对象看phpExcel类是否引入成功。
$objPHPExcel = new PHPExcel();
print_r($objPHPExcel);
==========以上是引入phpExcel类步骤(其它第三方类与此类似)============
<span style="font-size:18px;">以下开始excel导入导出</span>
//导出 控制器中use PHPExcel; use IOFactory;
- public function phpexcel()
- {
- //$objPHPExcel = new PHPExcel();
- //print_r($objPHPExcel);
- $query =DB::table('goods')->get();
- //$query =$this ->db->query($sql);
- //print_r($query);
- if(!$query)return false;
- //Starting the PHPExcel library
- //加载PHPExcel类
- //$this->load->library('PHPExcel');
- //$this->load ->library('PHPExcel/IOFactory');
- $objPHPExcel= new PHPExcel();
- include_once('../app/libs/phpexcel/phpexcel/IOFactory.php');
- $objPHPExcel->getProperties()-> setTitle("export") ->setDescription("none");
- $objPHPExcel-> setActiveSheetIndex(0);
- //Fieldnamesinthefirstrow
- $fields = DB::select("select COLUMN_NAME from information_schema.COLUMNS where
- table_name = 'goods';");
- //print_r($fields);die;
- $col = 0;
- foreach($fields as $field){
- $field =$field['COLUMN_NAME'];
- $objPHPExcel-> getActiveSheet() -> setCellValueByColumnAndRow($col, 1,$field);
- $col++;
- }
- // die;
- //Fetchingthetabledata
- $row = 2;
- foreach($query as $data)
- {
- $col =0;
- foreach($fields $field)
- {
- //print_r($data);
- $field =$field['COLUMN_NAME'];
- $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$row,!empty($data["$field"])?$data["$field"]:'');
- $col++;
- }
- $row++;
- }
- //die;
- $objPHPExcel-> setActiveSheetIndex(0);
- $objWriter =IOFactory :: createWriter($objPHPExcel, 'Excel5');
- //Sendingheaderstoforcetheusertodownloadthefile
- header('Content-Type:application/vnd.ms-excel');
- //header('Content-Disposition:attachment;filename="Products_' .date('dMy') . '.xls"');
- header('Content-Disposition:attachment;filename="Brand_' .date('Y-m-d') . '.xls"');
- header('Cache-Control:max-age=0');
- $objWriter-> save('php://output');
- }
- 控制器中use IOFactory; use PHPExcel_Cell;
- public function ru(Request $request){
- $tmp_file =$_FILES ['file_stu'] ['tmp_name'];
- $file_types =explode ( ".", $_FILES ['file_stu'] ['name'] );
- $file_type =$file_types [count ( $file_types ) - 1];
- /*判别是不是.xls文件,判别是不是excel文件*/
- if (strtolower( $file_type ) != "xls"){
- $this->error ( '不是Excel文件,重新上传' );
- }
- $savePath ="./excel/";
- /*以时间来命名上传的文件*/
- $str =date('Ymdhis');
- $file_name =$str . "." . $file_type;
- //echo$file_name;die;
- $request->file('file_stu')->move($savePath, $file_name);
- /*是否上传成功*/
- /*if(!copy($tmp_file,$savePath.$file_name)){
- $this->error ( '上传失败' );
- }*/
- //要获得新的文件路径+名字
- $fullpath =$savePath.$file_name;
- //echo$fullpath;die;
- $re =$this->read($fullpath,'utf-8');
- //print_r($re);die;
- for($i=1;$i<count($re);$i++){
- //print_r($re);
- //echo$re[$i][1];
- $adds =DB::table('goods')->insert(['gname' => $re[$i][1], 'gprice' =>$re[$i][2]]);
- }
- //die;
- if($adds){
- echo"<script>alert('导入成功');location.href='daoru'</script>";
- }else{
- echo"<script>alert('导入失败');location.href='daoru'</script>";
- }
- }
- public function read($filename,$encode='utf-8')
- {
- // ../ 一般情况不管处于什么子目录子需要这样子即可 例如\app\Admin\Controllers\WechatMercharntPay\OrderListTodayController.php
- include_once('../app/libs/phpexcel/phpexcel/IOFactory.php');
- //$this->load ->library('PHPExcel/IOFactory');
- $objReader =IOFactory::createReader('Excel5');
- $objReader->setReadDataOnly(true);
- $objPHPExcel= $objReader->load($filename);
- $objWorksheet= $objPHPExcel->getActiveSheet();
- $highestRow =$objWorksheet->getHighestRow();
- //echo$highestRow;die;
- $highestColumn = $objWorksheet->getHighestColumn();
- //echo$highestColumn;die;
- $highestColumnIndex =PHPExcel_Cell::columnIndexFromString($highestColumn);
- $excelData =array();
- for($row = 1;$row <= $highestRow; $row++) {
- for ($col= 0; $col < $highestColumnIndex; $col++) {
- $excelData[$row][]=(string)$objWorksheet->getCellByColumnAndRow($col,$row)->getValue();
- }
- }
- return $excelData;
- }
phpExcel导入导出终于完成了,赶快尝试一下吧
第二方法 用集成方法 maatwebsite 集成类,但是不支持一些文件。已经修复
http://blog.csdn.net/zhwxl_zyx/article/details/47251491
并修复其中的问题
\vendor\maatwebsite\excel\src\Maatwebsite\Excel\Readers\LaravelExcelReader.php
修改后的版本
https://github.com/yanggg1133/Laravel-Excel
增加
- /*
- * @desc 返回所有数据
- * @author 绍兴远帆软件有限公司 主营 ewshop网店系统 远帆自动售货机系统 手机话费流量充值系统 票务销售系统 点餐外卖系统 网店进销存系统
- * @website http://www.ewshop.net/
- * */
- public function readAll()
- {
- // ../ 一般情况不管处于什么子目录子需要这样子即可 例如\app\Admin\Controllers\WechatMercharntPay\OrderListTodayController.php
- //include_once('../app/libs/phpexcel/phpexcel/IOFactory.php');
- //$this->load ->library('PHPExcel/IOFactory');
- //$this->reader =IOFactory::createReader('Excel5');
- //$this->reader->setReadDataOnly(true);
- $objPHPExcel= $this->excel;
- $objWorksheet= $objPHPExcel->getActiveSheet();
- $highestRow =$objWorksheet->getHighestRow();
- //echo$highestRow;die;
- $highestColumn = $objWorksheet->getHighestColumn();
- //echo$highestColumn;die;
- $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
- $excelData =array();
- for($row = 1;$row <= $highestRow; $row++) {
- for ($col= 0; $col < $highestColumnIndex; $col++) {
- $excelData[$row][]=(string)$objWorksheet->getCellByColumnAndRow($col,$row)->getValue();
- }
- }
- return $excelData;
- }
Laravel 4
"maatwebsite/excel": "~1.3"
Laravel 5
"maatwebsite/excel": "~2.0"
- "require-dev": {
- "fzaninotto/faker": "~1.4",
- "mockery/mockery": "0.9.*",
- "phpunit/phpunit": "~4.0",
- "phpspec/phpspec": "~2.1",
- "maatwebsite/excel": "~2.0.0"
- },
添加完后执行
composer update
After updating composer, add the ServiceProvider to
the providers array in app/config/app.php
'Maatwebsite\Excel\ExcelServiceProvider',
You can use the facade for shorter code. Add this to
your aliasses:
'Excel' => 'Maatwebsite\Excel\Facades\Excel',
The class is binded to the ioC as excel
$excel = App::make('excel');
Laravel 4
Laravel Excel includes several config settings for
import-, export-, view- and CSV-specific settings. Use the artisan publish
command to publish the config file to your project.
php artisan config:publish maatwebsite/excel
The config files can now be found
at app/config/packages/maatwebsite/excel
Laravel 5
To publish the config settings in Laravel 5
use:
php artisan vendor:publish
This will add an excel.php config
file to your config folder.
详细用法请参考官网 http://www.maatwebsite.nl/laravel-excel/docs/getting-started
Laravel 上使用 phpexcel的两种方式的更多相关文章
- 转载:删除github上文件夹的两种方式
http://www.jianshu.com/p/286be61bb9b8 删除github上文件夹的两种方式(解决已经加入ignore的文件夹无法从远程仓库删除的问题) 如果此文件夹已被加入git追 ...
- button上加上图片的两种方式
//// ViewController.m// UIButtonDemo//// Created by hehe on 15/9/15.// Copyright (c) 2015年 wang. ...
- Github 上传代码的两种方式
上传本地代码/文件->Github 折腾了半天时间... Github前期准备部分 1)登录github,新建一个 repository 2)repository 命名 3)Github是一个托 ...
- thinkphp 返回上一页的两种方式
<div > <a class="details_back" href="{:U('Admin/SinglePageManagement/index') ...
- Django上传文件的两种方式
基于form表单上传文件 HTML <h3>基于form表单的上传文件</h3> <form action="" method="post& ...
- springMVC两种方式实现多文件上传及效率比较
springMVC实现 多文件上传的方式有两种,一种是我们经常使用的以字节流的方式进行文件上传,另外一种是使用springMVC包装好的解析器进行上传.这两种方式对于实 现多文件上传效率上却有着很大的 ...
- egg.js 通过 form 和 ajax 两种方式上传文件并自定义目录和文件名
egg.js 通过 form 和 ajax 两种方式上传文件并自定义目录和文件名 评论:10 · 阅读:8437· 喜欢:0 一.需求 二.CSRF 校验 三.通过 form 表单上传文件 四.通过 ...
- curl文件上传有两种方式,一种是post_fileds,一种是infile
curl文件上传有两种方式,一种是POSTFIELDS,一种是INFILE,POSTFIELDS传递@实际地址,INFILE传递文件流句柄! );curl_setopt($ch, CURLOPT_PO ...
- Android手机上监听短信的两种方式
Android手机上监听短信有两种方式: 1. 接受系统的短信广播,操作短信内容. 优点:操作方便,适合简单的短信应用. 缺点:来信会在状态栏显示通知信息. AndroidManifest.xml: ...
随机推荐
- python作用域与LEGB规则
作用域 什么是命名空间 比如有一个学校,有10个班级,在7班和8班中都有一个叫“小王”的同学,如果在学校的广播中呼叫“小王”时,7班和8班中的这2个人就纳闷了,你是喊谁呢!!!如果是“7班的小王”的话 ...
- python聚合云图
今天一时兴起,想用python爬爬自己的博客,通过数据聚合,制作高逼格的云图(对词汇出现频率视觉上的展示),看看最近我到底写了啥文章. 一.直接上几张我的博客数据的云图 1.1 爬取文章的标题的聚合 ...
- 解决python执行linux查看文件命令后,因为^@特殊符号导致部分内容不显示的问题
问题现象 文件a的内容如下: 查看第3行到第5行的内容:sed -n '3,5p' /tmp/test/a 可见在命令行执行,是没有问题的. 在python中使用paramiko模块执行linux命令 ...
- maven 在pom.xml 中指定仓库位置
...... 在pom.xml 中添加 仓库位置(这样遇到私服没有的依赖,就会去这下载) </properties> <repositories><!-- 代码库 --& ...
- 安装新操作系统 Windows 路径设置 节省C盘空间
1.QQ个人文件夹设置到D盘,D:\ProgramData\QQ 2.IE收藏夹设置到D盘,IE缓存设置到D盘 例如我想把收藏夹默认的保存路径改到D:\study下.关闭Internet Explor ...
- File 操作
文件乱码 服务器地址 try-with-resource 属性文件获取 文件排序 文件过滤 文件下载 流文件传递 文件乱码: WINDOWS系统桌面默认使用GBK,Linux系统默认使用UTF-8. ...
- Redis基本操作-list
Redis的5种数据结构:string.list.hash.set和zset; Redis 所有的数据结构都是以唯一的 key 字符串作为名称,然后通过这个唯一 key 值来获取相应的 value 数 ...
- (Python)numpy的argmax用法
解释 还是从一维数组出发.看下面的例子. import numpy as np a = np.array([3, 1, 2, 4, 6, 1]) print(np.argmax(a))4 argm ...
- devel包
devel 包主要是供开发用,至少包括以下2个东西:1. 头文件2. 链接库有的还含有开发文档或演示代码. 以 glib 和 glib-devel 为例: 如果你安装基于 glib 开发的程序,只需要 ...
- 双机\RAC\Dataguard的区别
Oracle 双机/RAC/Dataguard的区别 Data Guard 是Oracle的远程复制技术,它有物理和逻辑之分,但是总的来说,它需要在异地有一套独立的系统,这是两套硬件配置可以不同的系统 ...