<?php

namespace App\Admin\Controllers;

use App\AdminUser;
use Illuminate\Http\Request; use Excel;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint; class ExcelController extends Controller
{
//
public function index()
{
return view('admin/excel/index');
} //导出
public function export(Request $request)
{
/* $cellData = array(['学号', '姓名', '成绩'], ['10001', 'AAAAA', '99']);
Excel::create('学生成绩', function ($excel) use ($cellData) {
$excel->sheet('score', function ($sheet) use ($cellData) {
$sheet->rows($cellData);
});
})->export('xls');*/ //方法二:
$users = AdminUser::all()->toArray();
$header[] = array('ID号', '姓名', '邮箱', '创建时间', '状态');
$arr = array();
foreach ($users as $one) {
$data = array($one['id'], $one['name'], $one['email'], $one['created_at'], $one['status']);
array_push($arr, $data);
}
$arrData = array_merge($header, $arr); Excel::create(iconv('UTF-8', 'UTF-8', '管理员表'), function ($excel) use ($arrData) {
$excel->sheet('score', function ($sheet) use ($arrData) {
$sheet->rows($arrData);
});
})->store('xlsx')->export('xlsx');
} //导入
public function import(Request $request)
{
/*$filePath = 'storage/exports/' . iconv('UTF-8', 'GBK', '学生成绩') . '.xls';
Excel::load($filePath, function ($reader) {
$data = $reader->all();
dd($data);
});*/ // $file=Input::file('file'); dd(base_path());
$file = $request->file('file');
$tabl_name = date('YmdHis', time()) . rand(100, 999);
$entension = $file->getClientOriginalExtension(); //上传文件的后缀.
$new_name = $tabl_name . '.' . $entension;
if ($file->isValid()) {
$path = $request->file('file')->storeAs('import', $new_name);
// 更新文件本地地址 storage/import/20180720105908.xlsx
$path = '/public/storage/' . $path;
// dd($path); ///public/storage/import/20180720124843.xlsx"
Excel::load($path, function ($reader) use ($tabl_name) {
//$data = $reader->all();
//获取Excel的第几张表
$reader = $reader->getSheet(0);
//获取表中数据
$data = $reader->toArray();
// dd($data);
$result = $this->create_table($tabl_name, $data);
// dd($result);
});
} return redirect('/admin/files');
} //创建表
public function create_table($table_name, $field_arr)
{
$tmp = $table_name;
$val = $field_arr;
//创建表结构 因为已经有db_import表不再需要执行创建表结构程序。
//注意Excel标题最好是英文
/* $tables = DB::select("show tables");
$tables = array_column($tables, 'Tables_in_blog');*/
$tables = array_map('reset', \DB::select('SHOW TABLES'));
if (!in_array('db_import', $tables)) {
Schema::dropIfExists('db_import');
Schema::create("db_import", function (Blueprint $table) use ($tmp, $val) {
$fields = $val[0]; //列字段
$table->increments('id'); //主键
foreach ($fields as $key => $value) {
$table->string($fields[$key]);
}
});
} //填充数据
$value_str = array();
if ($id = DB::table('db_import')->max('id')) {
$id = $id + 1;
} else {
$id = 1;
}
//$id = DB::table('db_import')->max('id') ? DB::table('db_import')->max('id') + 1 : 1;
foreach ($val as $key => $value) {
if ($key != 0) {
// $content = implode(',', $value);
// $content2 = explode(',', $content);
foreach ($value as $key2 => $va2) {
if (!empty($va2)) {
$value_str[] = "'$va2'";
}
}
$news = implode(',', $value_str);
if (!empty($news)) {
$news = "$id," . $news;
DB::insert("insert into db_import VALUES ($news)");
} $value_str = array();
$id = $id + 1;
}
}
return $id;
} }

最新文章

  1. 【Git】关于VSCode 内置Git问题
  2. 深入理解javascript原型和闭包(12)——简介【作用域】
  3. [android]判断位置服务是否打开
  4. web_submit_data函数上传图片
  5. 使用Asyncio的Coroutine来实现一个有限状态机
  6. Spring 4 官方文档学习(十一)Web MVC 框架之约定优于配置
  7. windows安装TortoiseGit详细使用教程【基础篇】
  8. STL 查找vector容器中的指定对象:find()与find_if()算法
  9. JavaScript闭包函数的写法
  10. Unhandled Exxception “Unhandled exception type IOException”?
  11. 【排序算法】快速排序算法 Java实现
  12. ACM Strange fuction
  13. mac终端命令及pycharm常用快捷键记录
  14. c/c++ linux epoll系列3 利用epoll_wait设置timeout时间长度
  15. poj3660 cow contest
  16. C#中委托,匿名函数,lamda表达式复习
  17. JMM中的重排序及内存屏障
  18. Excel两列查找重复值
  19. redis集群中的主从复制架构(3主3从)
  20. Hbase 学习(十一)使用hive往hbase当中导入数据

热门文章

  1. UVA1204 Fun Game
  2. Leetcode77. Combinations组合
  3. Javascript-正则表达式常用字符集及方法
  4. Hdu 4251 区间中位数(划分树)
  5. 2016年中国独角兽企业估值榜 TOP300
  6. FileIntputStream / FileOutputStream 类
  7. iOS 自定义 URL Scheme 完全指南
  8. epiinfo是美国CDC开发维护的流行病学数据录入和分析软件,在DOS时代占主流,随着Windows的普及用的人越来越少了,epiinfo重新开发Windows版本后,体积庞大且不好用。在数据录入方面已被EpiData取代,不过epiinfo的分析模块,比如地理信息系统某些情况下还是挺有用的。
  9. nth-child和nth-of-type的使用案列
  10. Nginx教程(一) Nginx入门教程 (转)