标签:pre gets 需要 文件的 store ESS row 字段 btn
这是个我去公司之后曾经折磨我很久很久的功能查阅了很多资料但是功夫不负有心人在本人的不懈努力下还是实现了这个功
namespace App\Http\Controllers; use App\Http\Controllers\Admin\ContentTypes\File; use App\Models\Win1; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Storage; use Maatwebsite\Excel\Facades\Excel; use Illuminate\Routing\Controller; use Symfony\Component\CssSelector\Parser\Reader;
public function index(Excel $excel){ //将数据库中的信息转化为excel文件内容 $data=Win1::with(‘hasManyWindow‘)->get(); foreach ($data as $key){ $export[]=array( ‘id‘=>$key[‘id‘], ‘window‘=>$key[‘window‘], // 数据表中的两个字段 ); } $table_name=‘窗口名称‘; $excel::create($table_name,function ($excel)use($export){ $excel->sheet(‘Sheet1‘,function ($sheet)use($export){ $sheet->fromArray($export); }); })->store(‘xlsx‘)->export(‘xlsx‘); }
public function excelfile(Request $request) { //1.思路get传输过来的Excel文件地址 //2.循环读取数据保存到数组 //3.循环数组保存到数据库中 $flag=true; $file=$request->file(‘file‘); if($file){ //得到文件的路径 $realPath = $file->getRealPath(); //上传文件的后缀. $entension = $file -> getClientOriginalExtension(); // 获取上传的文件缓存在tmp文件夹下的绝对路径 $newpath=$file->getRealPath(); $tabl_name = date(‘YmdHis‘).mt_rand(100,999);//时间戳 if($flag==true){ Excel::load($realPath,function ($reader) use ($tabl_name){ //获取excel的第几张表 $reader = $reader->getSheet(0); //获取表中的数据 $data = $reader->toArray(); for($row=0;$row<count($data);$row++){ //echo $data[$row][‘0‘].‘ ‘; DB::table(‘windowmessage‘)->insert([‘id‘=>$data[$row][‘0‘],‘window‘=>$data[$row][‘1‘]]); } }); return ‘<script>alert("文件上传成功");window.location.href="importexcel"</script>‘; } }else{ return ‘<script>alert("文件为空上传失败!请重新上传");window.location.href="importexcel"</script>‘; } }
下面是blade模板中的代码(ps:给自己看的)
<a href="{{url(‘excelExport‘)}}" id="href"><div id="btn" >下载Excel</div></a>
{{--导入按钮--}}
<form style="display:none" action="{{url(‘excelfile‘)}}" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<input type="file" name="file" value="">
</form>
<input type="submit" value="批量导入" id="tijiao" >
<!-- 记得载入jquery文件 -->
<script>
$(‘#tijiao‘).click(function(){
$(this).prev(‘form‘).find(‘[name="file"]‘).trigger(‘click‘);
});
// 当表单文件有变化时执行提交动作
$(‘[name="file"]‘).change(function(){
if($(this).val()){
$(‘#tijiao‘).addClass(‘disabled‘ );
$(this).parent().submit();
}
});
</script>
标签:pre gets 需要 文件的 store ESS row 字段 btn
原文地址:https://www.cnblogs.com/yaoliuyang/p/12244744.html