學習記錄:
一個是通過 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); }