public function import_user(){
//excel 文件
if(!empty($_FILES['data']['name'])){
$upload=$this->uploadfile('data','tempxls',5242880,array('xls','xlsx'));
if($upload['status']){
$path=$upload['filepath'];
}else{
$this->error($upload['msg']);
}session
//讀取Excel文件
Vendor("PHPExcel.PHPExcel");
$objPHPExcel = new \PHPExcel();
$PHPReader = new \PHPExcel_Reader_Excel2007(); //默認是excel2007
$filePath="./SmsData/".$path; //Excel地址
if(!$PHPReader->canRead($filePath)){ echo 222;
$PHPReader = new \PHPExcel_Reader_Excel5(); //若是不成功的時候用之前的版原本讀取
if(!$PHPReader->canRead($filePath)){
$this->error("No Excel",U('Sms/import_user'));
exit;
}
}
$data = array();
$i = 0;
$PHPExcel = $PHPReader->load($filePath);
$currentSheet = $PHPExcel->getSheet(0);
//取得一共有多少列
$allColumn = $currentSheet->getHighestColumn();
//取得一共有多少行
$allRow = $currentSheet->getHighestRow();
//循環讀取數據,默認是utf-8輸出
for($currentRow = 2;$currentRow<=$allRow;$currentRow++)
{
$data[$i]['uid'] = I("session.uid");
$data[$i]['name'] = $currentSheet->getCell("A".$currentRow)->getValue();
$data[$i]['mobile'] = $currentSheet->getCell("B".$currentRow)->getValue();
$data[$i]['add_time'] = time();
$data[$i]['num'] = 0;
$i++;
/*
for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++)
{
$address = $currentColumn.$currentRow;
echo $address."@";
echo $currentSheet->getCell($address)->getValue()."\t";
//$data[$currentRow]['name'] =
}
*/
}
$res = M("SmsData")->addAll($data);
if($res){
$this->success('導入成功',U('Sms/index'));
}else{
$this->error('導入失敗',U('Sms/import_user'));
}
exit;
}
$this->display();
}app
(HTML)post
<form id="article-form" action="{:U('index')}" method="post" onsubmit="return checkform()"enctype="multipart/form-data">
<div class="form-group field-product-price">
<label class="control-label">上傳表格xls,xlsx</label>
<input type="file" name="data" accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"/>
<div class="help-block"></div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-success" >導入</button>
</div>
</form>ui