急、急、急 phpexcel导入excel到mysql出现问题?
16lz
2021-01-22
//1.html上传页面
<formaction="upload.php"method="post"enctype="multipart/form-data">
<inputtype="hidden"name="leadExcel"value="true">
<tablealign="center"width="90%"border="0">
<tr>
<td>
<inputtype="file"name="inputExcel"><inputtype="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文件
functionuploadFile($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');//useexcel2007for2007format
$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="INSERTINTOtest_list(`num`,`sex`)VALUES(
'{$strs[0]}',
'{$strs[1]}'
)";
//die($sql);
if(!mysql_query($sql))
{
returnfalse;
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;
请逐行排查
<formaction="upload.php"method="post"enctype="multipart/form-data">
<inputtype="hidden"name="leadExcel"value="true">
<tablealign="center"width="90%"border="0">
<tr>
<td>
<inputtype="file"name="inputExcel"><inputtype="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文件
functionuploadFile($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');//useexcel2007for2007format
$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="INSERTINTOtest_list(`num`,`sex`)VALUES(
'{$strs[0]}',
'{$strs[1]}'
)";
//die($sql);
if(!mysql_query($sql))
{
returnfalse;
echo'sql语句有误';
}
}
}
else
{
$msg="导入失败!";
}
return$msg;
}
?>
导入失败,请大家帮我看一下啊!在这里先谢谢啦!急死了。
4 个解决方案
#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;
请逐行排查
更多相关文章
- asp.net按路径把图片上传到sql数据库
- 七牛---Android SDK断点续传与暂停上传Demo
- java文件上传输入输出流的问题
- Android——Tomcat版本更新下载+头像照片上传到Tomcat
- 如何将文件路径从.java类文件传递到本机jni文件
- 路径提供者文档目录是一个安全的位置吗?
- android获取本地视频路径
- [置顶] Android获取存储卡路径的方式
- Android 打包VersionCode自增、APK文件名修改、上传蒲公英