当前位置: 移动技术网 > IT编程>开发语言>PHP > php上传excel表格并获取数据

php上传excel表格并获取数据

2017年12月12日  | 移动技术网IT编程  | 我要评论

哈儿和他的八个女人,芙殇19楼,邓超怕杨幂红毯产子

这个是最近需要做的一个功能,在网上也查看了很多相关的文章,基本上大同小异,在这里整理一下。

一:首先是html部分

<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" /> 
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

二:就是去接收和处理上传的文件了。php部分

//文件存放的路径
$save_path = "/upload/"; 

//文件存放的文件夹
$save_files = $this->geturl();

这个是以年月日新建的文件夹,仅供参考。

//先检查当前文件夹是否存在,如不存在,创建文件夹
function geturl()
{
$year = date('y');
$month= date('m');
$day= date('d');

$str = $year.$month.$day;
if(strtoupper(substr(php_os,0,3))=='win'){
$path = getcwd() . "/upload/".$str;
}else{
$path = "/mnt/erp/".$str;
}
if(!file_exists($path))//判断文件夹是否存在
{
mkdir($path);
}

//return $path."/";
return $str."/";
}

//这个是上传文件到需要保存的位置,
if (!@move_uploaded_file($_files[$upload_name]["tmp_name"], $save_path.$file_path)) {
$error = "error|上传文件错误.";
exit(0);
}

下面开始获取你上传的excel数据了

//获取上传表格的数据
$file_name = $save_path.$file_path;                    //获取上传文件的地址名称
require_once apppath . 'views/idc/config/phpexcel.php';       
require_once apppath . 'views/idc/config/phpexcel/iofactory.php';
require_once apppath . 'views/idc/config/phpexcel/cell.php';

$objreader = phpexcel_iofactory::createreader('excel2007'); //建立reader对象

$objphpexcel = $objreader->load($file_name);
$sheet = $objphpexcel->getsheet();
$highestrow = $sheet->gethighestdatarow(); // 取得总行数

$highestcolumn_num = phpexcel_cell::columnindexfromstring($sheet->gethighestdatacolumn()); //列数

//$columns = phpexcel_cell::getcolumn($highestcolumn_num);
$columns = array('a','b','c','d','e','f','g');

$arr_result = array();
$dealer_element = array();

for ($j = 2; $j <= $highestrow; $j++) {
for ($k = 0; $k < count($columns); $k++) {
//读取单元格
$value = $objphpexcel->getactivesheet()->getcell($columns[$k] . $j)->getvalue();//这个就是获取每个单元格的值

$value = trim($value);
if (empty($value)) {
$value = null;
}
$dealer_element[$k] = $value;
//这里可以根据要求,做一些数据的验证
}

$arr_result[$j] = $dealer_element;
}
echo json_encode($arr_result);

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持移动技术网

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网