• Excel 表 导入导出


    //导出
    public function exports($ids = [],$action='HP'){
    switch ($action){
    case 'HP':
    $comment = ['会员卡卡号','余额数量','会员卡类型','备注'];//第一行标题内容
    $field = ['cardnums', 'price_num', 'statustype','remark'];//第二行列字段内容
    if(!empty($ids)){//查询指定的几条数据
    $res = db('membercardgroup')
    ->field($field)
    ->where(['id'=>['in',$ids]])
    ->select();
    foreach($res as &$re){
    if($re['statustype']==0){
    $re['statustype'] = '金额卡';
    }else{
    $re['statustype'] = '整箱卡';
    }
    }
    }else{//查询所有数据
    $res =db('membercardgroup')
    ->field($field)
    // ->limit(5)
    ->select();
    foreach($res as &$re){
    if($re['statustype']==0){
    $re['statustype'] = '金额卡';
    }else{
    $re['statustype'] = '整箱卡';
    }
    }
    // echo '<pre/>';print_r($res);die;
    }
    break;
    default:
    $this->displayByError('行动有误,撤退!!');
    }
    $strTable ='<table width="500" border="1">';
    $strTable .= '<tr>';
    //第一行的标题
    foreach ($comment as $key => $value){
    $strTable .= '<td style="text-align:left;font-size:16px;" width="*">'.$value.'</td>'." ";
    }
    $strTable .= '</tr>';
     
    //第二行的列字段
    $strTable .= '<tr>';
    foreach ($field as $key => $value){
    $strTable .= '<td style="text-align:left;font-size:14px;" width="*">'.$value.'</td>'." ";
    }
    $strTable .= '</tr>';
     
    //第三行开始是导入数据库的数据
    $strTable .= '<tr>';
    for ($i = 0;$i < count($res);$i++){
    for ($j = 0; $j < count($field);$j++){
    $strTable .= '<td style="text-align:left;font-size:12px;" rowspan="*">'.$res[$i][$field[$j]].'</td>'." ";
    }
    $strTable .= '</tr>';
    }
    $strTable .='</table>';
    unset($res);
    $this->downloadExcel($strTable,'GoodsExcel');
    exit();
     
    }
     
    /**
    * 导出excel需要设置的header
    * @param $strTable /表格内容
    * @param $filename /文件名
    */
    public function downloadExcel($strTable,$filename)
    {
    header("Content-type: application/vnd.ms-excel");//将查询结果导出到Excel
    header("Content-Type: application/force-download");//告诉浏览器强制下载
    header("Content-Disposition: attachment; filename=".$filename."_".date('Y-m-d H-i-s',time()).".xls");//attachment作为附件下载,inline在线下载,filename设置文件名
    header('Expires:0');//浏览器不会响应缓存
    header('Pragma:public');//Public指示响应可被任何缓存区缓存。
    echo '<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'.$strTable.'</html>';
    }
     
     
     
     
    //导入
    public function upload()
    {
    require_once '/www/wwwroot/www/extend/PHPExcel/Classes/PHPExcel/IOFactory.php';//导入PHPExcel文件中的IOFactory.php类
    // echo 111;die;
    $file = request()->file('file');//获取文件,file是请求的参数名
    $info = $file->move(ROOT_PATH . 'public' . DS . 'excel');//move将文件移动到项目文件的xxx
    // echo '<pre/>';print_r(__FILE__);die;
    if($info){
    libxml_use_internal_errors(true);
     
    $excel_path = $info->getSaveName(); //获取上传文件名
    // $excel_suffix = $info->getExtension(); //获取上传文件后缀
    $file_name = './../public' . DS . 'excel' . DS . $excel_path; //上传文件的地址
     
    $obj_PHPExcel = PHPExcel_IOFactory::load($file_name); //加载文件内容
    // echo '<pre/>';print_r($obj_PHPExcel);die;
    $excel_array=$obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式
    array_shift($excel_array); //删除第一个数组(标题);
    $arr = reset($excel_array); //获取字段名
    unset($excel_array[0]); //删除字段名,剩下的都是存储到数据库的数据了!!
    $data = [];
    for($i = 0;$i < count($excel_array);$i++){
    foreach ($arr as $key => $value){
    $data[$i][$value] = $excel_array[$i+1][$key];//使数组的键值就是数据表的字段名
    }
    }
    foreach($data as &$v){
    if($v['statustype']=='金额卡'){
    $ins['statustype'] = 0;
    }elseif($v['statustype']=='整箱卡'){
    $ins['statustype'] = 1;
    }else{
    continue;
    }
     
    $ins['title'] = $v['title'];
    $ins['cardnums'] = $v['cardnums'];
    $ins['code'] = $v['code'];
    $ins['price_num'] = $v['price_num'];
    $ins['image'] = $v['image'];
    $ins['status'] = $v['status'];
    $ins['addtime'] = time();
    // echo '<pre/>';print_r($ins);die;
    $resint = db('membercardgroup')->insert($ins);
    }
     
    return json(['code'=>1,'msg'=>'成功','url'=>'https://'.$_SERVER['HTTP_HOST'].'/public/excel/' . $excel_path]);
    }
    }

  • 相关阅读:
    一个短信验证码倒计时插件
    记一次图片优化经历
    前端开发中两种常见的图片加载方式
    《javascript面向对象精要》读书笔记
    less hack 兼容
    第一次项目总结
    你总说毕业遥遥无期,可转眼就各奔东西
    【翻译】理念:无冲突的扩展本地DOM原型
    【翻译】jQuery是有害的
    202002280156-《统治世界的10种算法(摘自极客大学堂)》
  • 原文地址:https://www.cnblogs.com/DevilBoy/p/14087641.html
Copyright © 2020-2023  润新知