原创 2017年06月24日 20:24:31
  • 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类的路径

[html] view
plain
 copy

  1. "autoload": {
  2. "classmap": [
  3. "database",
  4. "app/libs/phpExcel"
  5. ],
  6. "psr-4": {
  7. "App\\": "app/"
  8. }
  9. },


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;
 

[php] view plain copy
  1. public function phpexcel()
  2. {
  3. //$objPHPExcel = new PHPExcel();
  4. //print_r($objPHPExcel);
  5. $query =DB::table('goods')->get();
  6. //$query =$this ->db->query($sql);
  7. //print_r($query);
  8. if(!$query)return false;
  9. //Starting the PHPExcel library
  10. //加载PHPExcel类
  11. //$this->load->library('PHPExcel');
  12. //$this->load ->library('PHPExcel/IOFactory');
  13. $objPHPExcel= new PHPExcel();
  14. include_once('../app/libs/phpexcel/phpexcel/IOFactory.php');
  15. $objPHPExcel->getProperties()-> setTitle("export") ->setDescription("none");
  16. $objPHPExcel-> setActiveSheetIndex(0);
  17. //Fieldnamesinthefirstrow
  18. $fields = DB::select("select COLUMN_NAME from information_schema.COLUMNS where
  19. table_name = 'goods';");
  20. //print_r($fields);die;
  21. $col = 0;
  22. foreach($fields as $field){
  23. $field =$field['COLUMN_NAME'];
  24. $objPHPExcel-> getActiveSheet() -> setCellValueByColumnAndRow($col, 1,$field);
  25. $col++;
  26. }
  27. // die;
  28. //Fetchingthetabledata
  29. $row = 2;
  30. foreach($query as $data)
  31. {
  32. $col =0;
  33. foreach($fields $field)
  34. {
  35. //print_r($data);
  36. $field =$field['COLUMN_NAME'];
  37. $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col,$row,!empty($data["$field"])?$data["$field"]:'');
  38. $col++;
  39. }
  40. $row++;
  41. }
  42. //die;
  43. $objPHPExcel-> setActiveSheetIndex(0);
  44. $objWriter =IOFactory :: createWriter($objPHPExcel, 'Excel5');
  45. //Sendingheaderstoforcetheusertodownloadthefile
  46. header('Content-Type:application/vnd.ms-excel');
  47. //header('Content-Disposition:attachment;filename="Products_' .date('dMy') . '.xls"');
  48. header('Content-Disposition:attachment;filename="Brand_' .date('Y-m-d') . '.xls"');
  49. header('Cache-Control:max-age=0');
  50. $objWriter-> save('php://output');
  51. }
  52. 控制器中use IOFactory;   use PHPExcel_Cell;
  53. public function ru(Request $request){
  54. $tmp_file =$_FILES ['file_stu'] ['tmp_name'];
  55. $file_types =explode ( ".", $_FILES ['file_stu'] ['name'] );
  56. $file_type =$file_types [count ( $file_types ) - 1];
  57. /*判别是不是.xls文件,判别是不是excel文件*/
  58. if (strtolower( $file_type ) != "xls"){
  59. $this->error ( '不是Excel文件,重新上传' );
  60. }
  61. $savePath ="./excel/";
  62. /*以时间来命名上传的文件*/
  63. $str =date('Ymdhis');
  64. $file_name =$str . "." . $file_type;
  65. //echo$file_name;die;
  66. $request->file('file_stu')->move($savePath, $file_name);
  67. /*是否上传成功*/
  68. /*if(!copy($tmp_file,$savePath.$file_name)){
  69. $this->error ( '上传失败' );
  70. }*/
  71. //要获得新的文件路径+名字
  72. $fullpath =$savePath.$file_name;
  73. //echo$fullpath;die;
  74. $re =$this->read($fullpath,'utf-8');
  75. //print_r($re);die;
  76. for($i=1;$i<count($re);$i++){
  77. //print_r($re);
  78. //echo$re[$i][1];
  79. $adds =DB::table('goods')->insert(['gname' => $re[$i][1], 'gprice' =>$re[$i][2]]);
  80. }
  81. //die;
  82. if($adds){
  83. echo"<script>alert('导入成功');location.href='daoru'</script>";
  84. }else{
  85. echo"<script>alert('导入失败');location.href='daoru'</script>";
  86. }
  87. }
  88. public function read($filename,$encode='utf-8')
  89. {
  90. // ../  一般情况不管处于什么子目录子需要这样子即可 例如\app\Admin\Controllers\WechatMercharntPay\OrderListTodayController.php
  91. include_once('../app/libs/phpexcel/phpexcel/IOFactory.php');
  92. //$this->load ->library('PHPExcel/IOFactory');
  93. $objReader =IOFactory::createReader('Excel5');
  94. $objReader->setReadDataOnly(true);
  95. $objPHPExcel= $objReader->load($filename);
  96. $objWorksheet= $objPHPExcel->getActiveSheet();
  97. $highestRow =$objWorksheet->getHighestRow();
  98. //echo$highestRow;die;
  99. $highestColumn = $objWorksheet->getHighestColumn();
  100. //echo$highestColumn;die;
  101. $highestColumnIndex =PHPExcel_Cell::columnIndexFromString($highestColumn);
  102. $excelData =array();
  103. for($row = 1;$row <= $highestRow; $row++) {
  104. for ($col= 0; $col < $highestColumnIndex; $col++) {
  105. $excelData[$row][]=(string)$objWorksheet->getCellByColumnAndRow($col,$row)->getValue();
  106. }
  107. }
  108. return $excelData;
  109. }


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

增加

[php] view plain copy
  1. /*
  2. * @desc 返回所有数据
  3. * @author 绍兴远帆软件有限公司 主营 ewshop网店系统 远帆自动售货机系统 手机话费流量充值系统 票务销售系统 点餐外卖系统 网店进销存系统
  4. * @website http://www.ewshop.net/
  5. * */
  6. public function readAll()
  7. {
  8. // ../  一般情况不管处于什么子目录子需要这样子即可 例如\app\Admin\Controllers\WechatMercharntPay\OrderListTodayController.php
  9. //include_once('../app/libs/phpexcel/phpexcel/IOFactory.php');
  10. //$this->load ->library('PHPExcel/IOFactory');
  11. //$this->reader =IOFactory::createReader('Excel5');
  12. //$this->reader->setReadDataOnly(true);
  13. $objPHPExcel= $this->excel;
  14. $objWorksheet= $objPHPExcel->getActiveSheet();
  15. $highestRow =$objWorksheet->getHighestRow();
  16. //echo$highestRow;die;
  17. $highestColumn = $objWorksheet->getHighestColumn();
  18. //echo$highestColumn;die;
  19. $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
  20. $excelData =array();
  21. for($row = 1;$row <= $highestRow; $row++) {
  22. for ($col= 0; $col < $highestColumnIndex; $col++) {
  23. $excelData[$row][]=(string)$objWorksheet->getCellByColumnAndRow($col,$row)->getValue();
  24. }
  25. }
  26. return $excelData;
  27. }

Laravel 4

"maatwebsite/excel": "~1.3"

Laravel 5


"maatwebsite/excel": "~2.0"



[plain] view plain copy


  1. "require-dev": {

  2. "fzaninotto/faker": "~1.4",

  3. "mockery/mockery": "0.9.*",

  4. "phpunit/phpunit": "~4.0",

  5. "phpspec/phpspec": "~2.1",

  6. "maatwebsite/excel": "~2.0.0"

  7. },

添加完后执行
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的两种方式的更多相关文章

  1. 转载:删除github上文件夹的两种方式

    http://www.jianshu.com/p/286be61bb9b8 删除github上文件夹的两种方式(解决已经加入ignore的文件夹无法从远程仓库删除的问题) 如果此文件夹已被加入git追 ...

  2. button上加上图片的两种方式

    ////  ViewController.m//  UIButtonDemo////  Created by hehe on 15/9/15.//  Copyright (c) 2015年 wang. ...

  3. Github 上传代码的两种方式

    上传本地代码/文件->Github 折腾了半天时间... Github前期准备部分 1)登录github,新建一个 repository 2)repository 命名 3)Github是一个托 ...

  4. thinkphp 返回上一页的两种方式

    <div > <a class="details_back" href="{:U('Admin/SinglePageManagement/index') ...

  5. Django上传文件的两种方式

    基于form表单上传文件 HTML <h3>基于form表单的上传文件</h3> <form action="" method="post& ...

  6. springMVC两种方式实现多文件上传及效率比较

    springMVC实现 多文件上传的方式有两种,一种是我们经常使用的以字节流的方式进行文件上传,另外一种是使用springMVC包装好的解析器进行上传.这两种方式对于实 现多文件上传效率上却有着很大的 ...

  7. egg.js 通过 form 和 ajax 两种方式上传文件并自定义目录和文件名

    egg.js 通过 form 和 ajax 两种方式上传文件并自定义目录和文件名 评论:10 · 阅读:8437· 喜欢:0 一.需求 二.CSRF 校验 三.通过 form 表单上传文件 四.通过 ...

  8. curl文件上传有两种方式,一种是post_fileds,一种是infile

    curl文件上传有两种方式,一种是POSTFIELDS,一种是INFILE,POSTFIELDS传递@实际地址,INFILE传递文件流句柄! );curl_setopt($ch, CURLOPT_PO ...

  9. Android手机上监听短信的两种方式

    Android手机上监听短信有两种方式: 1. 接受系统的短信广播,操作短信内容. 优点:操作方便,适合简单的短信应用. 缺点:来信会在状态栏显示通知信息. AndroidManifest.xml: ...

随机推荐

  1. Linux命令行下如何终止当前程序

    Linux命令行下如何终止当前程序 快捷键: Ctrl+c 在命令行下起着终止当前执行程序的作用, Ctrl+d 相当于exit命令,退出当前shell Ctrl+s 挂起当前shell(保护作用很明 ...

  2. 编码风格和PEP8规范

    编码风格 错误认知 这很浪费时间 我是个艺术家 所有人都能穿的鞋不会合任何人的脚 我善长制定编码规范 正确认知 促进团队合作 减少bug处理 提高可读性,降低维护成本 有助于代码审查 养成习惯,有助于 ...

  3. yum lnmp

    1.关闭防火墙 [root@CentOS ~]# chkconfig iptables off   2.关闭selinux vi /etc/sysconfig/selinux //将SELINUX=e ...

  4. CUDA入门

    CUDA入门 鉴于自己的毕设需要使用GPU CUDA这项技术,想找一本入门的教材,选择了Jason Sanders等所著的书<CUDA By Example an Introduction to ...

  5. leetcode202

    public class Solution { private int SumSqares(int n) { //将一个数字的各个数位的值分开存储 var list = new List<int ...

  6. leetcode492

    public class Solution { public int[] ConstructRectangle(int area) { Dictionary<int, int> dic = ...

  7. location 对象属性

    Location 对象属性 hash 返回一个URL的锚部分 host 返回一个URL的主机名和端口 hostname 返回URL的主机名 href 返回完整的URL pathname 返回的URL路 ...

  8. mysql闯关练习

    1.表关系                 班级表:class       学生表:student       cid caption grade_id   sid sname gender clas ...

  9. Gulp的安装与配置

    http://blog.csdn.net/itlsx/article/details/49981459

  10. 数学分析中jensen不等式由浅入深进行教学(转)

    中国知网:数学分析中Jensen不等式由浅入深进行教学