//1.html 上传页面
<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="leadExcel" value="true">
    <table align="center" width="90%" border="0">
    <tr>
       <td>
        <input type="file" name="inputExcel"><input type="submit" value="导入数据">
       </td>
    </tr>
    </table>
</form>//upload.php执行程序文件<?php
if($_POST['leadExcel'] == "true")
{
    $filename = $_FILES['inputExcel']['name'];
    $tmp_name = $_FILES['inputExcel']['tmp_name'];
    $msg = uploadFile($filename,$tmp_name);
    echo $msg;
}
//导入Excel文件
function uploadFile($file,$filetempname) 
{
    //自己设置的上传文件存放路径
    $filePath = 'upFile/';
    $str = "";   
    //下面的路径按照你PHPExcel的路径来修改
    require_once 'PHPExcel/PHPExcel.php';
    require_once 'PHPExcel/PHPExcel/IOFactory.php';
    require_once 'PHPExcel/PHPExcel/Reader/Excel5.php';    //注意设置时区
    $time=date("y-m-d-H-i-s");//去当前上传的时间 
    //获取上传文件的扩展名
    $extend=strrchr($file,'.');
    //上传后的文件名
    $name=$time.$extend;
    $uploadfile=$filePath.$name;//上传后的文件名地址 
    //move_uploaded_file() 函数将上传的文件移动到新位置。若成功,则返回 true,否则返回 false。
    $result=move_uploaded_file($filetempname,$uploadfile);//假如上传到当前目录下
    //echo $result;
    if($result) //如果上传文件成功,就执行导入excel操作
    {
        include "conn.php";
        $objReader=PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format 
        $objPHPExcel=$objReader->load($uploadfile); 
        $sheet=$objPHPExcel->getSheet(0); 
        $highestRow=$sheet->getHighestRow();           //取得总行数 
        $highestColumn=$sheet->getHighestColumn(); //取得总列数        $objWorksheet = $objPHPExcel->getActiveSheet();
        $highestRow = $objWorksheet->getHighestRow(); 
        echo 'highestRow='.$highestRow;
        echo "<br>";
        $highestColumn = $objWorksheet->getHighestColumn();
        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
        echo 'highestColumnIndex='.$highestColumnIndex;
        echo "<br>";
        $headtitle=array(); 
        for ($row = 1;$row <= $highestRow;$row++) 
        {
            $strs=array();
            //注意highestColumnIndex的列数索引从0开始
            for ($col = 0;$col < $highestColumnIndex;$col++)
            {
                $strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
            }    
            $sql="INSERT INTO test_list(`num`,`sex`) VALUES (
            '{$strs[0]}',
            '{$strs[1]}'
            )";
            //die($sql);
            if(!mysql_query($sql))
            {
return false;
echo 'sql语句有误';
            }
        }
    }
    else
    {
       $msg = "导入失败!";
    } 
    return $msg;
}
?>导入失败,请大家帮我看一下啊!在这里先谢谢啦!急死了。MySQLExcelSQLphpexcel

解决方案 »

  1.   

    如果 echo $msg; 输出 导入失败!
    那么就表示,这段代码中有问题
        //自己设置的上传文件存放路径
        $filePath = 'upFile/';
        $str = "";   
        //下面的路径按照你PHPExcel的路径来修改
        require_once 'PHPExcel/PHPExcel.php';
        require_once 'PHPExcel/PHPExcel/IOFactory.php';
        require_once 'PHPExcel/PHPExcel/Reader/Excel5.php';    //注意设置时区
        $time=date("y-m-d-H-i-s");//去当前上传的时间 
        //获取上传文件的扩展名
        $extend=strrchr($file,'.');
        //上传后的文件名
        $name=$time.$extend;
        $uploadfile=$filePath.$name;//上传后的文件名地址 
        //move_uploaded_file() 函数将上传的文件移动到新位置。若成功,则返回 true,否则返回 false。
        $result=move_uploaded_file($filetempname,$uploadfile);//假如上传到当前目录下
        //echo $result;
    请逐行排查