zoukankan      html  css  js  c++  java
  • Thinkphp导入导出excel表

    Thinkphp 操作 excel表 导入导入导出功能

    1、下载 phpreportexcl 的使用包  Excel  放在 THINKPHP 的程序包目录下第三方插件库  INC/THINKPHP/Vendor

          2、导出功能:

    function export(){

                  header("Content-type:application/octet-stream");

                  header("Accept-Ranges:bytes");

                  header("Content-type:application/vnd.ms-excel");  

                  header("Content-Disposition:attachment;filename=stu_report_".date("Y-m-d").".xls");

                  header("Pragma: no-cache");

                  header("Expires: 0");

                 

                  //导出xls 开始

                  $tag1 = iconv("UTF-8", "GB2312",'日期');

                  $tag2 = iconv("UTF-8", "GB2312",'学员姓名');

                  echo "$tag1 $tag2 ";

                 

                  $stu = M (‘stu’);

    $arr = $stu -> select();

    foreach($arr as $key=>$val){

           $date        =    date('Y-m-d',$val['pay_time']);

           $stu_name    =    iconv("UTF-8", "GB2312", $val['stuname']);

    echo "$date $stu_name ";

    }

     

    }

    3、 导入功能:

           确保导入的xls 中的字段和数据中的可以对号;

    导入静态页面:

    <script>

    function import_check(){

        var f_content = form1.file.value;

           if(f_content==''){

                   alert('请选择您要上传的文件!');

                   return false;

           }

        var fileext=f_content.substring(f_content.lastIndexOf("."),f_content.length)

            fileext=fileext.toLowerCase()

        if (fileext!='.xls')

            {

            alert("对不起,导入数据格式必须是xls格式文件哦,请您调整格式后重新上传,谢谢 ");           

            return false;

            }

    }

    </script>

    <form id="form1" name="form1" enctype="multipart/form-data" action="/index.php/Admin/import" method="post">

      <input name="file" type="file" id="file" size="50" />

      <input type="submit" class='background_img' value="上传文档" style='margin-top:2px;' onclick="return import_check();"/>

    </form>

     

    导入页面处理方法:

     

    if($_POST){

                   header("Content-type: text/html; charset=utf-8");

                   error_reporting(E_ALL ^ E_NOTICE);

                   $Import_TmpFile = $_FILES['file']['tmp_name'];

                   Vendor('Excel.reader');  //导入thinkphp 中第三方插件库

                   $data = new Spreadsheet_Excel_Reader();

                   $data->setOutputEncoding('UTF-8');

                   $data->read($Import_TmpFile);

                   $array =array();

                   for ($i = 1; $i < $data->sheets[0]['numRows']; $i++) {

                          for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {

                                 $array[$i][$j] = $data->sheets[0]['cells'][$i+1][$j];

                          }

                   }

    Print_r($array);

    foreach( $array as $tmp){

                          $fmess = D ('a_fee_mess');

                          $fmess_add['stuname'] = $tmp['2'];

                          $fmess_add['fee']     = $tmp['3'];

                          $fmess_add['pay_time'] = strtotime(str_replace('/','-',$tmp[1]));

                          $fmess->add($fmess_add);

    }

    }

     

    参考:http://gaoke0820.blog.163.com/blog/static/21664965201171265813216/

  • 相关阅读:
    HDU1255 覆盖的面积 —— 求矩形交面积 线段树 + 扫描线 + 离散化
    HDU1542 Atlantis —— 求矩形面积并 线段树 + 扫描线 + 离散化
    HDU1540 Tunnel Warfare —— 线段树 区间合并
    HDU3974 Assign the task —— dfs时间戳 + 线段树
    HDU4027 Can you answer these queries? —— 线段树 区间修改
    POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值
    ZOJ1610 Count the Colors —— 线段树 区间染色
    HDU1698 Just a Hook —— 线段树 区间染色
    POJ2528 Mayor's posters —— 线段树染色 + 离散化
    POJ3468 A Simple Problem with Integers —— 线段树 区间修改
  • 原文地址:https://www.cnblogs.com/phpxuetang/p/4975276.html
Copyright © 2011-2022 走看看