• PHP实现excel


    导入

    [php] view plain copy
     
    1. public function excel_put(){  
    2.     //先做一个文件上传,保存文件  
    3.     $path=$_FILES['file'];  
    4.     $filePath = "uploads/".$path["name"];  
    5.     move_uploaded_file($path["tmp_name"],$filePath);  
    6.     //默认用excel2007读取excel,若格式不对,则用之前的版本进行读取  
    7.     //表格字段名字  
    8.     $data=array('B'=>'name','C'=>'pwd','D'=>'money1','E'=>'salt');  
    9.     $tablename='user1';//表名字  
    10.     $this->excel_fileput($filePath,$data,$tablename);      
    11. }  
    12. private function excel_fileput($filePath,$data,$tablename){  
    13.     $this->load->library("phpexcel");//ci框架中引入excel类  
    14.     $PHPExcel = new PHPExcel();  
    15.     $PHPReader = new PHPExcel_Reader_Excel2007();  
    16.     if(!$PHPReader->canRead($filePath)){  
    17.         $PHPReader = new PHPExcel_Reader_Excel5();  
    18.         if(!$PHPReader->canRead($filePath)){  
    19.             echo 'no Excel';  
    20.             return ;  
    21.         }  
    22.     }  
    23.     // 加载excel文件  
    24.     $PHPExcel = $PHPReader->load($filePath);  
    25.   
    26.     // 读取excel文件中的第一个工作表  
    27.     $currentSheet = $PHPExcel->getSheet(0);  
    28.     // 取得最大的列号  
    29.     $allColumn = $currentSheet->getHighestColumn();  
    30.     // 取得一共有多少行  
    31.     $allRow = $currentSheet->getHighestRow();  
    32.   
    33.     // 从第二行开始输出,因为excel表中第一行为列名  
    34.     for($currentRow = 2;$currentRow <= $allRow;$currentRow++){  
    35.         /**从第A列开始输出*/  
    36.         //echo $allColumn;  
    37.           
    38.         for($currentColumn= 'A';$currentColumn<= $allColumn; $currentColumn++){    
    39.             $val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();  
    40.             //print_r($val);  
    41.             //die;  
    42.               
    43.             if($currentColumn == 'A')  
    44.             {  
    45.                 //echo $val." ";  
    46.             }else if($currentColumn <= $allColumn){  
    47.                 $data1[$currentColumn]=$val;  
    48.             }  
    49.         }  
    50.         foreach($data as $key=>$val){  
    51.             $data2[$val]=$data1[$key];  
    52.         }  
    53.         $this->db->insert($tablename,$data2);  
    54.         //print_r($data2);  
    55.         //echo "</br>";         
    56.     }  
    57.     //echo " ";  
    58.     echo "导入成功";  
    59. }  



    导出

    [php] view plain copy
     
      1. header("Content-type:application/vnd.ms-excel");  
      2. header("Content-Disposition:attachment;filename=123.xls");  
      3.   
      4. $array=$this->db->get("shop_address")->result_array();  
      5. $str = "Id Name Pid ";  
      6. foreach ($array as $val) {  
      7.     $str .=  $val['id'] . " " .$val['name'] . " " . $val['pid'] . " ";  
      8. }  
      9. echo $str;    
  • 相关阅读:
    使用FolderBrowserDialog组件选择文件夹
    使用OpenFileDialog组件打开多个文
    使用OpenFileDialog组件打开对话框
    获取弹出对话框的相关返回值
    PAT 甲级 1139 First Contact (30 分)
    PAT 甲级 1139 First Contact (30 分)
    PAT 甲级 1138 Postorder Traversal (25 分)
    PAT 甲级 1138 Postorder Traversal (25 分)
    PAT 甲级 1137 Final Grading (25 分)
    PAT 甲级 1137 Final Grading (25 分)
  • 原文地址:https://www.cnblogs.com/hangxing1996/p/7087119.html
Copyright © 2020-2023  润新知