TP5.0 excel 导入导出

2022-07-07 09:08:43 浏览数 (1)

引第三方的phpexcel类库放到 ThinkPHPLibraryVendordemo下,自己建的文件夹demo

再将Excel.class放到ThinkPHPLibraryOrgclass下,自己建的文件夹class

控制器:

代码语言:javascript复制
<?php
namespace AdminController;
use ThinkController;
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 ThinkUpload();
                $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();
    }
}

视图

代码语言:javascript复制
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>

视图

代码语言:javascript复制
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>

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/113684.html原文链接:https://javaforall.cn

0 人点赞