php读取excel文件,输出的值乱码解决方法

WBOY
Freigeben: 2016-06-13 13:37:46
Original
1072 Leute haben es durchsucht

php读取excel文件,输出的值乱码
$handle = fopen("./home.csv","r");
 while($items = fgetcsv($handle,1000)){
  foreach($items as $k=>$v){
  $v = iconv('gb2312','utf-8',$v); 
  var_dump($v);
  }  
 }

------解决方案--------------------
明白了,你说的是这个问题
这是 php5.3 的一个BUG,php5.2 是正常的

你得换个方法解决了
------解决方案--------------------

PHP code
$fname = $_FILES['MyFile']['tmp_name'];//获取上传的CSV文件
    $handle=fopen("$fname","r");//打开文件
    
    //判断导入文件类型是否为csv
    //如果为csv继续执行
    if($_FILES['MyFile']['type'] !='application/vnd.ms-excel')
    {
    if($_FILES['MyFile']['type'] != 'text/comma-separated-values')
      {
        echo "<script>alert('您上传的文件类型不符,请重新上传');window.location='subHotel_import.php';</script>";exit();
      }
    }
      
        $num = 1;
        $total_num = 100;//设置每次添加的数据条数
        //删除表中原有数据
       $db->query("delete from hotel_activities_2");
      //打开导入的csv
      //循环添加到表中
      while(!feof($handle))
        {
                        
            $content = fgets($handle);
            $data =  preg_split("/,/",$content);
          
            if($data[0] =='')
            {    
               continue;
            }     
            $subHotel_id = preg_replace('/[^0-9]/','',$data[0]);
            $subHotel_type= (int)preg_replace('/[^0-9]/','',$data[4]);
            if($num == $total_num )
            {
                
                $num=1;
                   $values .=  " ($subHotel_id,'".$data[1]."','".$data[2]."','".$data[3]."',$subHotel_type),";
                   $values = rtrim($values,',');                
                $sql ="insert into hotel_activities_2 (hotel_id,start_date,end_date,content,s_type) values " .$values;                
                $aaa = $db->query($sql);
                
                $values ='';
            }
            else 
            {
                $values .=  " ($subHotel_id,'".$data[1]."','".$data[2]."','".$data[3]."',$subHotel_type),";
                $num++;            
            }               
        }
        if(mysql_error!==1){
            $values = rtrim($values,',');
            $sql ="insert into hotel_activities_2 (hotel_id,start_date,end_date,content,s_type) values " .$values;     
            $db->query($sql);               
                echo "<script>alert('导入成功!!!');window.location='subHotel_query_file.php';</script>";
            }else
            {
            echo "<script>alert('导入失败!!!');window.location='subHotel_import.php';</script>";
            }
            
                             
    fclose($handle);
<br><font color="#e78608">------解决方案--------------------</font><br>在 csv 格式标准中:字符串是需要用双引号括起的,但微软的工具软件却偏偏不加这个双引号<br>在 php5.3 以前的版本中,php 也认同这种做法<br>但自 php5.3 起,微软摆出了不合作姿态,于是 fgetcsv 也就残废了
<br><font color="#e78608">------解决方案--------------------</font><br>读取excel数据要用到一个插件,直接这样读取肯定是不行的。<br>下面这个是将ecxcel数据读取到数据库的方法:<br>7.$handle = fopen (”test.csv”,”r”); <br>8.$sql=”insert into scores(idcard,names ,num,sex,nation,score) values(’”; <br>9.while ($data = fgetcsv ($handle, 1000, “,”)) { <br>10.$num = count ($data); <br>11.for ($c=0; $c 12.if($c==$num-1){$sql=$sql.$data[$c].”‘)”;break;} <br>13.$sql=$sql.$data[$c].”‘,’”; <br>14.} <br>15.print “”; <br>16.echo $sql.””; <br>17.$db->query($sql); <br>18.echo “SQL语句执行成功!”; <br>19.$sql=”insert into scores(idcard,names ,num,sex,nation,score) values(’”; <br>20.} <br>21.fclose ($handle); <br>22.$time_end = getmicrotime();
                 
              
              
        
            
Nach dem Login kopieren
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!