TP5.0 excel 导入导出
程序员文章站
2022-04-24 19:51:13
介绍下excel导入导出 引第三方的phpexcel类库放到 ThinkPHP\Library\Vendor\demo下,自己建的文件夹demo 再将Excel.class放到ThinkPHP\Library\Org\class下,自己建的文件夹class 控制器: 视图: 视图 谢谢大家浏览到这里 ......
介绍下excel导入导出
引第三方的phpexcel类库放到 thinkphp\library\vendor\demo下,自己建的文件夹demo
再将excel.class放到thinkphp\library\org\class下,自己建的文件夹class
控制器:
<?php
namespace admin\controller;
use think\controller;
class excelcontroller extends controller {
public function excellist(){
$this->display();
}
// 导入
public function import(){
if(!empty($_files['file_stu']['name'])){
$tmp_file = $_files['file_stu']['tmp_name']; //临时文件名
$file_types = explode('.',$_files['file_stu']['name']); // 拆分文件名
$file_type = $file_types [count ( $file_types ) - 1]; // 文件类型
/*判断是否为excel文件*/
if($file_type == 'xls' || $file_type == 'xlsx'|| $file_type == 'csv'){ // 符合类型
/*上传业务*/
$upload = new \think\upload();
$upload->maxsize = 3145728 ;
$upload->exts = array('xls', 'csv', 'xlsx');
$upload->rootpath = './public';
$upload->savepath = '/excel/';
$upload->savename = date('ymdhis');
$info = $upload->upload();
if(!$info) { // 上传错误提示错误信息
$this->error($upload->geterror());
}else{ // 上传成功
// 读取文件
$filename='./public'.$info['file_stu']['savepath'].$info['file_stu']['savename'];
import("org.yufan.excelreader");
vendor('phpexcel.phpexcel');
$reader = \phpexcel_iofactory::createreader('excel2007'); //设置以excel5格式(excel97-2003工作簿)
$phpexcel = $reader->load($filename); // 载入excel文件
$sheet = $phpexcel->getsheet(0); // 读取第一個工作表
$highestrow = $sheet->gethighestrow(); // 取得总行数
var_dump($highestrow);
$highestcolumm = $sheet->gethighestcolumn(); // 取得总列数
/** 循环读取每个单元格的数据 */
$data = array();
for ($row = 2; $row <= $highestrow; $row++){//行数是以第1行开始
if($column = 'a'){
$data['name'] = $sheet->getcell($column.$row)->getvalue();
}
if($column = 'b'){
$data['account'] = $sheet->getcell($column.$row)->getvalue();
}
if($column = 'c'){
$data['password'] = $sheet->getcell($column.$row)->getvalue();
}
m('data')->add($data);
}
$this->success('导入数据库成功',u('excel/show'));
}
} else{ // 不符合类型业务
$this->error('不是excel文件,请重新上传...');
}
}else{
$this->error('(⊙o⊙)~没传数据就导入');
}
}
//导出
public function export(){
import("org.yufan.excel");
$list = m('data')->select();
if($list == null){
$this->error('数据库信息为空...',__app__.'/admin/excel/show');
}else{
$row=array();
$row[0]=array('平台名称','帐号','密码');
$i=1;
foreach($list as $v){
$row[$i]['name'] = $v['name'];
$row[$i]['account'] = $v['account'];
$row[$i]['password'] = $v['password'];
$i++;
}
$xls = new \excel_xml('utf-8', false, 'datalist');
$xls->addarray($row);
$xls->generatexml(date('ymdhis'));
}
}
public function show(){
$m = m('data');
$data = $m->select();
$this->assign('data',$data);
$this->display();
}
}
视图:
excellist.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>excel导入导出</title>
</head>
<body>
<div class="page-header">
<h1>tp demo
<small>excel导入导出练习</small>
</h1>
</div>
<form method="post" action="{:u('excel/import')}" class="form-signin" enctype="multipart/form-data" >
<input name="file_stu" type="file" class="form-control">
<button class="btn btn-lg btn-primary btn-block">导入</button>
</form>
</body>
</html>
视图
show.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>表格展示</title>
</head>
<body>
<div class="page-header">
<h1>tp demo
<small>excel导入导出练习</small>
</h1>
</div>
<table class="table">
<tr>
<td><h4><b>平台名称</b></h4></td>
<td><h4><b>帐号</b></h4></td>
<td><h4><b>密码</b></h4></td>
</tr>
<foreach name="data" item="vo">
<tr>
<td>{$vo.name}</td>
<td>{$vo.account}</td>
<td>{$vo.password}</td>
</tr>
</foreach>
</table>
<form action="{:u('excel/export')}" class="form-signin">
<button class="btn btn-lg btn-primary btn-block">导出数据库数据</button>
</form>
</body>
</html>
谢谢大家浏览到这里~~~~