当前位置:网站首页>tp5导入excel到数据库
tp5导入excel到数据库
2022-07-21 05:08:00 【流情】
1.前提准备(本地环境)
准备好本地php站点环境,可以用phpstudy(小皮面板)或者wamp。数据库可视化工具建议navica。这里用的tp版本是thinkphp5.0.24的。就tp版本而言,我觉得影响是不大的。
将excel表格数据导入数据库可以走navica手动导入,但是我们走的是tp框架。所以流程是:
前端客户端上传excel文件到服务器上,然后导入数据库。
这里用到了phpExcle依赖。github下载地址:https://github.com/PHPOffice/PHPExcel
下载完之后将Classes目录复制到本地tp5框架的vendor目录下,并改名:PHPExcel
2.使用PHPExcel
这是模块控制器下的方法,将文件上传到这个接口方法即可。
//获取文件后缀名
public function getExt1($filename)
{
$arr = explode('.',$filename);
return array_pop($arr);
}
public function save(){
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Headers: Origin, No-Cache, X-Requested-With, If-Modified-Since, Pragma, Last-Modified, Cache-Control, Expires, Content-Type, X-E4M-With");
header("Content-Type: text/html; charset=utf-8");
$loginstatus = $this->checkToken();
if($loginstatus){
if(request() -> isPost())
{
vendor("PHPExcel.PHPExcel");
$objPHPExcel =new \PHPExcel();
//var_dump($objPHPExcel);die;
//获取表单上传文件
$file = request()->file('file');
//print_r($file);die;
$info = $file->validate(['ext' => 'xlsx,xls'])->move(ROOT_PATH . 'uploads/file'); //上传验证后缀名,以及上传之后移动的地址
if($info)
{
$exclePath = $info->getSaveName(); //获取文件名
$file_name = ROOT_PATH . 'uploads/file/' . DS . $exclePath;//上传文件的地址
$name = $this->getExt1($file);
if($name =='xlsx' ){
$objReader =\PHPExcel_IOFactory::createReader('Excel2007');
}else {
$objReader =\PHPExcel_IOFactory::createReader('Excel5');
}
//$objReader =\PHPExcel_IOFactory::createReader("Excel2007");
$obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8'); //加载文件内容,编码utf-8
$excel_array=$obj_PHPExcel->getSheet(0)->toArray(); //转换为数组格式
//print_r($excel_array);die;
array_shift($excel_array); //删除第一个数组(标题);
$city = [];
$i=0;
foreach($excel_array as $k=>$v) {
//$click_time = strtotime($v[2]);//点击时间转为时间戳
$city[$k]['product'] = isset($v[0])?$v[0]:"无";
$city[$k]['supplier'] = isset($v[1])?$v[1]:"无";
$city[$k]['model'] = isset($v[2])?$v[2]:"无";
$city[$k]['phone'] = $v[3];
$city[$k]['describe'] = $v[4];
$city[$k]['type'] = $v[5];
$city[$k]['contacts'] = $v[6];
$city[$k]['Goodsservices'] = $v[7];
$city[$k]['stock'] = $v[8];
$city[$k]['time'] = $v[9];
$i++;
}
// print_r($city);die;
$count=Db::table("qy_business")->insertAll($city);
$array=array('code'=>'0','msg'=>'success','data'=>array('count'=>$count));
echo json_encode($array);
}else
{
echo $file->getError();
}
}
}
}
本地请求示例结果如下:
3.数据库表和excel文件数据对应
这两张表对比一下,然后再看上面的代码就更容易理解了。这里的字段id作为主键是自动递增的。
其他的数据都是一一对应的。
码字不易,转载请注明出处!有问题可以留言或私信,看到会回复。
边栏推荐
- 使用nodemon工具使Nodejs服务器自动重启
- 17. [application of setw() function]
- Output statements on the console
- 用大白话让你理解TypeScript的要点.(三)
- 线性薛定谔方程实现界面推移
- 移动安全入门指南
- 宽字节注入学习记录
- oh-my-zsh 效率插件
- Realization of serpentine digital lattice Write a cube function with the input parameter of num. the requirements are as follows through JS: when num=3, the output table effect is: [[1,2,3] [6,5,4] [7
- [good article record] zorb framework construction process of embedded framework
猜你喜欢
随机推荐
Reset Form
1. Vite acquaintance and vite construction project
用大白话让你理解TypeScript的要点.(三)
The most common throttling scenario is the pull-down refresh and pull-up load of uniapp
Koa2快速搭建服务器
Wechat applet makes a slot machine lottery animation
thinkphp6使用EasyWeChat5.x之公众号开发(一)
Zero dimensional interior ballistic equations of solid rocket motor
Optimization of image multi picture pages
Storage principle of JS data type in memory
OpenFOAM中的多孔介质
How to play FLV format videos locally for free
【极客大挑战 2019】Easy,Love,Baby-SQL
MySQL import and export & View & Index & execution plan
Use the nodemon tool to restart the nodejs server automatically
专栏开设的意义
网页服务器/客户端搭建(nodejs启动exe程序)
BUUCTF-web-随便注
Fundamentals of computational heat transfer
QML implements CSDN search box with irregular rounded corners