php上传解析Excel
学习记录:
一个是通过 excel_reader 来解析(只能解析 .xls的Excel,如果后缀名是自己加的,或者自己修改的那种也不行,xlsx中另存为xls的那种才可以),虽然只能解析一种,但是只要一个文件就好
这个文件的引用在执行代码的时候会报错,百度搜可以搜到解决方法,很简单。
一个是通过 PHPExcel 来解析(xlsx/xls 都可以)需要很多文件
文件都是放在了Controller下面
前端页面都是一样
<form id="upfile" enctype="multipart/form-data"> <input type="file"class="form-control" name="file" id="file" style="width:600px;" > <button class="btn btn-warning" id="conds" onclick="upload()"><i class="icon-cog"></i>上传</button> </form>
function upload(){
var file_name=$("#file").val();
var file_siffix =file_name.replace(/.+\./,"");
if(file_siffix=="xls"||file_siffix=="xlsx" ){
var form = new FormData(document.getElementById("upfile"));
$.ajax({
url:"upexcel",
type:"post",
data:form,
processData:false,
contentType:false,
error:function(msg){
},
success:function(msg){
}
})
}else{
alert("请上传.xls/.xlsx后缀的表格");
}
}
第一种通过 excel_reader2.php 这个文件需要下载然后放到controller文件下
然后后台写的方法
public function upexcel(){ $path = "你保存的文件路径"; // 获取到的原本的文件名称 $oldname = $_FILES["file"]["name"]; //修改后的文件名称 $suf = substr($_FILES["file"]["name"], strrpos($_FILES["file"]["name"], '.')+1); $nowtime = time(); //当前时间戳 $newname=$nowtime.".".$suf; //换过后的名字 因为不能读中文所以改成了时间戳 if(move_uploaded_file($_FILES["file"]["tmp_name"], $path.$newname)){ //如果保存成功 //上传成功后去读取excel的内容、 require_once 'excel_reader2.php'; $data = new \Spreadsheet_Excel_Reader(); $data->setOutputEncoding('utf-8');//设置编码格式 $data->read("/www/wwwroot/这里要具体到路径/".$path.$newname); $arr = $data->sheets[0]['cells'];//cell数组 for($i=0;$i<=count($arr);$i++){ if($i>=3){ //从第三行开始转换成array(key,value) $temp = array(); foreach($arr[$i] as $item=>$val){ if($item==1){ $temp["numid"]=$val; }else if($item==2){ $temp["dutytime"]=$val; }else if($item==3){ $temp["dutyer"]=$val; } else if($item==4){ $temp["holidyer"]=$val; } else if($item==5){ $temp["liader"]=$val; }else if($item==6){ $temp["remark"]=$val; } } $list[] = $temp; //把数组放到list里面然后插入到数据库中 } } $result=$表->insertAll($list); //插入数据库 }
第二种 通过PHPExcel
首先去下载PHPExcel要用的文件,下载的是一个classes的Zip 加压后只需要拿这两个,放到controller文件夹下

后台: //上传解析excel xlsx xls public function upexcel(){ $path = "保存路径"; // 获取到的原本的文件名称
上面部分都一样
if(move_uploaded_file($_FILES["file"]["tmp_name"], $path.$newname)){
//从这里就不一样了
$file = iconv("utf-8", "gb2312", $path . $newname); //转码
require_once 'PHPExcel.php';
if ($suf == "xlsx") {
$objRead = new \PHPExcel_Reader_Excel2007(); //创建对象 xlsx
} else if ($suf == "xls") {
$objRead = new \PHPExcel_Reader_Excel5(); //xls
}
$cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
$obj = $objRead->load("/www/wwwroot/haiguan.hacy88.com/public/".$file); //建立excel对象
$currSheet = $obj->getSheet(0); //获取指定的sheet表
$columnH = $currSheet->getHighestColumn(); //取得最大的列号
$columnCnt = array_search($columnH, $cellName);
$rowCnt = $currSheet->getHighestRow(); //获取总行数
$data = array();
for($_row=1; $_row<=$rowCnt; $_row++){ //读取内容
for($_column=0; $_column<=$columnCnt; $_column++){
$cellId = $cellName[$_column].$_row;
$cellValue = $currSheet->getCell($cellId)->getValue();
$data[$_row][$cellName[$_column]] = $cellValue;
}
}
//下面基本都一样了
for($i=0;$i<=count($data);$i++){
// $nowtime numid dutytime dutyer holidyer liader
if($i>=3){ //从第三个个数据开始拿东西 这里的数组的key 和第一种不同这里是 ABED 所以修改下
$temp = array();
$temp["timeid"]=$nowtime;
foreach($data[$i] as $item=>$val){
if($item=="A"){
$temp["numid"]=$val;
}else if($item=="B"){
$temp["dutytime"]=$val;
}else if($item=="C"){
$temp["dutyer"]=$val;
}
else if($item=="D"){
$temp["holidyer"]=$val;
}
else if($item=="E"){
$temp["liader"]=$val;
}else if($item=="F"){
$temp["remark"]=$val;
}
}
$list[] = $temp;;
}
}
$result=$duty->insertAll($list);
}

浙公网安备 33010602011771号