链接:https://pan.baidu.com/s/1XnakEk5Apz9ZOsWPG7yNaA
提取码:5mm1
下载PHPExcel
include "../web/Classes/PHPExcel/IOFactory.php";
// 允许上传的图片后缀
$allowedExts = array("xls", "xlsx");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if (($_FILES["file"]["size"] < 200004800) && in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] < 0)
{
return "{
\"code\": 0
,\"msg\": \"错误::".$_FILES['file']['error']."\"
,\"data\": {
\"src\": \"http://cdn.layui.com/123.jpg\"
}
}";
}
else
{
$file_type = $extension;
/* echo "上传文件名: " . $_FILES["file"]["name"] . "<br>";
echo "文件类型: " . $_FILES["file"]["type"] . "<br>";
echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"];die();*/
$objReader="";
if ($file_type== 'xls')//判断excel表类型为2003还是2007
{
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
} elseif ($file_type== 'xlsx') {
$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
}
$PHPExcel=$objReader->load($_FILES["file"]["tmp_name"]);
//获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推
$currentSheet=$PHPExcel->getSheet(0);
//获取总列数
$allColumn=$currentSheet->getHighestColumn();
//获取总行数
$allRow=$currentSheet->getHighestRow();
//循环获取表中的数据,$currentRow表示当前行,从哪行开始读取数据,索引值从0开始
$excelData = array();
for ($row = 2; $row <= $allRow; $row++) {
$model = new DmTextDisModel();
$model->content = trim($currentSheet->getCell('B'.$row)->getValue());
$model->type = trim($currentSheet->getCell('C'.$row)->getValue());
$model->used = trim($currentSheet->getCell('D'.$row)->getValue());
$model->save();
}
return "{
\"code\": 0
,\"msg\": \"上传成功!\"
,\"data\": {
\"src\": \"\"
}
}";
}
}
- THE END -
最后修改:2020年3月19日
非特殊说明,本博所有文章均为博主原创。
如若转载,请注明出处:https://www.95app.top/php-upload-excel-read/
共有 0 条评论