原创 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

Laravel 5

[plain]view plaincopy
  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

You can use the facade for shorter code. Add this to your aliasses:

The class is binded to the ioC as 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.

The config files can now be found at app/config/packages/maatwebsite/excel

Laravel 5

To publish the config settings in Laravel 5 use:

This will add an excel.php config file to your config folder.

详细用法请参考官网 http://www.maatwebsite.nl/laravel-excel/docs/getting-started

转载于:https://www.cnblogs.com/mouseleo/p/8628169.html

Laravel 上使用 phpexcel的两种方式相关推荐

  1. [汇总信息] Laravel 上使用 phpexcel的两种方式

    文章采集与网上 方式1.使用原生的phpexcel , http://blog.csdn.net/CSwfe/article/details/52748046?locationNum=1 1.在app ...

  2. django + python上传文件的两种方式

    突然心血来潮,研究了下django+python上传文件的两种方式. 第一:直接采用文件读写的方式上传 1. settings.py文件中设置文件的存放路径和文件读取路径 MEDIA_ROOT = o ...

  3. Android之从网络上获取图片的两种方式讲解:thread+handle和AsyncTask方式

    从网络上获取图片是一个比较耗时的操作,放在主线程会导致阻塞主线程,响应超时,所以我们不能把它放在主线程里操作,必须放在一个子线程里,我打算采用两种方式去实现.1.采用thread去获取图片,获取到后通 ...

  4. vue前端实现上传文件的两种方式

    1.使用form表单的形式 第一种方式就是使用FormData的方式进行上传 html代码: <el-form :model="upform" :rules="up ...

  5. LayUI upload上传组件上传文件的两种方式(手动上传、自动上传)

    1 手动上传 上传文件分为两步,第一步选择文件,第二步上传文件. HTML代码: <input type='button' id='selectFile' value='选择文件'> &l ...

  6. WebUploader 上传文件的两种方式(手动上传,自动上传)

    1 手动上传 上传文件分为两步,第一步选择文件,第二步上传文件. HTML代码: <input type='button' id='selectFile' value='选择文件'> &l ...

  7. flask中使用FileField上传文件的两种方式+前端页面上传文件(flask三种上传文件方式)

    文章目录 上传文件方式一: 1.index.html文件: 2.主文件main.py: 上传文件方式二: 1.index2.html文件: 2.main.py文件: 上传文件方式三: 1.index3 ...

  8. Android从网络上获取图片的两种方式

    1,获得图片的bitmap格式 参见以前写的,这种方式是使用Url连接,位图工厂直接从连接解析 package cn.com.hh.http;import java.io.IOException; i ...

  9. 从网络上下载文件的两种方式

    Delphi 编程,从网络上下载文件,我一般用 UrlDownloadToFile API函数,简单,实用. function DownloadFile(const strURL, strDownFi ...

最新文章

  1. python类的成员函数_python特殊成员函数
  2. python 复制文件夹
  3. linux内核学习之三:linux中的32位与64位
  4. 【工具相关】web-HTML/CSS/JS Prettify的使用
  5. 关于配置环境变量相关解答
  6. java gui设置圆形按钮_简单的设置圆形按钮
  7. Computer Vision阅读文章总结纪要
  8. 音频处理之语音加速播放
  9. 一文读懂 Spring Bean 的生命周期
  10. Multisim14仿真基本模拟电路之 10. 3. 2比例放大电路的仿真实验与分析
  11. HTML5期末大作业:京东网站设计——仿京东(7页) 大学生简单个人静态HTML网页设计作品 DIV布局个人介绍网页模板代码 DW学生个人网站制作成品下载
  12. lenovo启动热键_常见电脑启动项热键
  13. 系统测试总结报告模板
  14. 2021-最新Web前端经典面试试题及答案-史上最全前端面试题(含答案)---JavaScript篇
  15. 网站浏览器可以打开,在微信中打不开,排查问题的过程
  16. python爬取虎扑评论_python爬去虎扑数据信息,完成可视化
  17. 华夏旅游CMS商城系统操作手册
  18. 腾讯云服务器高性能云盘和SSD云硬盘区别及选择
  19. http post 415错误
  20. 初出茅庐的小李第63博客之FastLED库的使用

热门文章

  1. 一劳永逸关闭Windwos默认共享
  2. OBS源代码阅读笔记
  3. css 选择器 伪元素_CSS伪元素-解释选择器之前和之后
  4. 编码中统一更该变量的快捷键_更多项目想法,以提高您的编码技能
  5. MySQL 常见操作指令
  6. cvpr 深度估计_无监督单目视频深度估计中的uncertainty方法(CVPR#x27;20)
  7. 几何图形在logo设计中的有哪些情感意义?
  8. 学Java技术,这些问题要避免
  9. 智能POS常见问题整理
  10. 20165334 四则运算阶段性总结(第二周)