Coder Social home page Coder Social logo

aluckyboy.github.io's People

aluckyboy.github.io's Issues

laravel5.5导出Excel表

1.简介
laravel5.5集成了Excel,从而可以简单的实现Excel的导入和导出。我的业务逻辑是,查询对应用户的报名表,并将查询的数据导出到Excel表。

2.安装环境
首先在Laravel项目根目录下使用Composer安装依赖:使用命令:composer require maatwebsite/excel ~2.1.0

安装完成后,在config/app.php 中注册服务提供者到providers数组:Maatwebsite\Excel\ExcelServiceProvider::class,

同样在config/app.php中注册门面到aliases数组:'Excel' => Maatwebsite\Excel\Facades\Excel::class,

添加完成后 ,执行如下Artisan命令:php artisan vendor:publish,执行成功后会在config目录下生成一个配置文件excel.php。

3.导出Excel文件
在你的需要导出数据的控制器中创建对应的方法 public function makesexcel,我的控制器是ArticleController,同时定义好对应的路由:Route::post('/makesexcel','ArticleController@makesexcel');

我的控制器业务代码:
public function makesexcel(){
$id = Input::get('id');//获取到文章ID
//查询对应的文章
$thisart = Article::where("id",$id)->first();
//查询对应的所有的报名消息
$baomings = Activity::where('art_id',$id)->get()->toArray();
foreach($baomings as $k=>$v){
//查询报名人
$actname = DB::table("users")->where('id',$v['user_id'])->first();
$baomings[$k]['user_id'] = $actname->user_nickname;
$baomings[$k]['art_id'] = $thisart->art_title;
}
$newarr = ['id'=>'序号','art_id'=>'文章标题','user_id'=>'报名账号','user_name'=>'参与者姓名','user_tel'=>'参与者手机','user_time'=>'报名时间'];
array_unshift($baomings,$newarr);//加入标题
Excel::create(iconv('UTF-8', 'GBK', $thisart->art_title."活动报名表"),
function($excel) use ($baomings){
$excel->sheet('score', function($sheet) use ($baomings){
$sheet->rows($baomings);
});
})->store('xls')->export('xls');
}

我的前端html代码:

下载Excel表

我的前端ajax代码:

$(".makesexcel").click(function(){
var $btn = $(".makesexcel").button('loading');
var id = '{{$thisactivity->id}}';//获取到文章ID
$.ajax('/admin/article/makesexcel',{
type:"post",
headers: {accept: "application/json", 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
data:'id='+id,//对应的文章ID
success:function(data){
if(data != ''){
window.location.href='/excel/{{$thisactivity->art_title}}活动报名表.xls';
$(".makesexcel").html(' 已下载 ');
}
},
error:function(){
alert("网络连接失败!,请稍后重试!");
$btn.button('reset');
}
});
});

4.注意的地方
我的逻辑是先将Excel表保存到服务器上,然后在对应的ajax返回成功的地方直接访问Excel表在服务器上的地址。由于Excel表默认的保存路径是storage/exports目录下。但是laravel默认允许访问的目录是public,所以直接访问目录storage/exports下的Excel表是访问不到的,可以将保存的地址放在public下。然后通过/目录名/Excel名称就可以访问了。

注:修改保存路径是在config/excel.php ;将默认的 'path' => storage_path('exports'),改为 'path' => public_path('excel'),

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.