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;
請逐行排查
我不知道哪個地方出錯了,找不出來?
~幫幫忙哦~ 感激不盡...
一步步斷點調試。