急、急、急 phpexcel匯入excel到mysql出現問題?

來源:互聯網
上載者:User
MySQL Excel SQL phpexcel

//1.html 上傳頁面


//upload.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 "
";
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);//總列數
echo 'highestColumnIndex='.$highestColumnIndex;
echo "
";
$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;
}
?>

匯入失敗,請大家幫我看一下啊!在這裡先謝謝啦!急死了。


回複討論(解決方案)

如果 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;
請逐行排查

我不知道哪個地方出錯了,找不出來?

~幫幫忙哦~ 感激不盡...

一步步斷點調試。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.