<?php 
include_once("inc/auth.inc.php");//include_once("PHPExcel/PHPExcel.php");
//include_once("PHPExcel/PHPExcel/IOFactory.php");
//include_once("PHPExcel/PHPExcel/Reader/Excel5.php");
  header("Content-Type:text/html;charset=utf-8");
//<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
/****** Include the EXCEL Reader Factory ***********///header("Content-Type:text/html;charset=utf-8");
/****** Include the EXCEL Reader Factory ***********/
error_reporting(0);
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'PHPExcel/IOFactory.php';
echo  $_FILES['excelupload']['name'];if(isset($_POST) && !empty($_FILES['excelupload']['name']))
{
    //print_r($_FILES['excelupload']);
    
    $namearr = explode(".",$_FILES['excelupload']['name']);
    if(end($namearr) != 'xls' && end($namearr) != 'xlsx')
    {
        echo '<p> Invalid File </p>';
        $invalid = 1;
    }
    if($invalid != 1)
    {
        $target_dir = "uploads/";
        $target_file = $target_dir . basename($_FILES["excelupload"]["name"]);
        
      //  echo $target_file;
      //  $target_file = iconv('latin1', 'GBK', $target_file);
        $response = move_uploaded_file($_FILES['excelupload']['tmp_name'],$target_file);
        if($response)
        {
        
            try {
                $objPHPExcel = PHPExcel_IOFactory::load($target_file);
            } catch(Exception $e) {
                die('Error : Unable to load the file : "'.pathinfo($_FILES['excelupload']['name'],PATHINFO_BASENAME).'": '.$e->getMessage());
            }
            $allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
       //     $objWriter->setUseBOM(true);
            //print_r($allDataInSheet);
            $arrayCount = count($allDataInSheet); // Total Number of rows in the uploaded EXCEL file
            //echo $arrayCount;
      //      $string = "INSERT INTO `test2` (`Sr.No.`,`Emp ID`, `Name`, `Address`) VALUES ";
          
            for($i=2;$i<=$arrayCount;$i++){
            
                $create_dept= trim($allDataInSheet[$i]["B"]);
                $manager = trim($allDataInSheet[$i]["C"]);
                $name = trim($allDataInSheet[$i]["D"]);
                $num = trim($allDataInSheet[$i]["E"]);
                $status = trim($allDataInSheet[$i]["F"]);
                $start_time = trim($allDataInSheet[$i]["G"]);
                $est_end_time = trim($allDataInSheet[$i]["H"]);              
                $type = trim($allDataInSheet[$i]["I"]);
                $budget = trim($allDataInSheet[$i]["J"]);
                $pay = trim($allDataInSheet[$i]["K"]);
                $cost = trim($allDataInSheet[$i]["L"]);
                $desc = trim($allDataInSheet[$i]["M"]);
                $dept = trim($allDataInSheet[$i]["N"]);
                $l2type = trim($allDataInSheet[$i]["O"]);
               echo $create_dept;
               echo $name;
               // $conn = mysql_connect("localhost","root","");
              //  mysql_select_db("test",$conn);
              //  mysql_query("set names 'UTF-8'");
               
                $string = "INSERT INTO testdb (
                PROJ_ID,
                PROJ_CREATE_DEPT, 
                PROJ_MANAGER,
                PROJ_NAME,
                PROJ_NUM,
                PROJ_STATUS,
                PROJ_START_TIME,
                PROJ_EST_END_TIME,
                PROJ_CREATE_TYPE,
                PROJ_BUDGET,
                PROJ_SIGNED_COST,
                PROJ_REAL_COST,
                PROJ_DESCRIPTION,
               PROJ_DEPT,
               PROJ_L2_TYPE,
               PROJ_UPDATE_TIME
                ) VALUES (
                '',
                '$create_dept',
                '$manager',
                '$name',
                '$num',
                '$status',
                '$start_time',
                '$est_end_time',
                '$type',
                '$budget',
                '$pay',
                '$cost',
                '$desc',
                '$dept',
                '$l2type',
                now()
                )";
                
                exequery(TD::conn(),$string);
         }
       }
    }// End Invalid Condition
            echo "<div style='color:#3C7C64'>Import Succesful</div>";
            }
            
            ?>show variables like "%char%";
+--------------------------+--------------------------------+
| Variable_name            | Value                          |
+--------------------------+--------------------------------+
| character_set_client     | utf8                           |
| character_set_connection | utf8                           |
| character_set_database   | utf8                           |
| character_set_filesystem | binary                         |
| character_set_results    | utf8                           |
| character_set_server     | utf8                           |
| character_set_system     | utf8                           |
| character_sets_dir       | E:\MYOA\mysql5\share\charsets\ |
+--------------------------+--------------------------------+我在页面上直接打印变量echo $create_dept;
               echo $name;能正常输入中文,但mysql中是乱码,而且其他页面输入中文的问题不存在乱码的情况。请高手指点下。

解决方案 »

  1.   

    mysql_query("set names utf8");
    必须要有!
      

  2.   


    我之前加了,加在exequery(TD::conn(),$string); 之前,试了也不行啊
      

  3.   

    exequery(TD::conn(),$string); 是什么?
    如果是调用你的数据库操作类,那么你的在他里面设置字符集
      

  4.   

    噢,可是如果conn.php是zend加密的怎么办
      

  5.   


    $conn = mysql_connect("localhost","root","myoa888");
                    mysql_select_db("TD_OA",$conn);
                    mysql_query("set names 'UTF-8'");
                    mysql_query($string);加了这一段,貌似写不进去呢,他会覆盖前面的数据库连接吗
      

  6.   

    不是  mysql_query("set names 'UTF-8'");
    是  mysql_query("set names utf8");