範本頁/index.html
<span style="color:#000000;"><meta charset='utf-8'></meta> <form action="{:U('Index/uploads')}" method="post" enctype="multipart/form-data"> 上传<select onchange='aa()' id='sel'><?php for($i=1;$i<=10 ;$i++ ){?> <option value=<?php echo $i;?>><?php echo $i;?></option><?php }?></select>个<p> <div id="div" class=""> <input type="file" name="file"><p> </div><input type="submit" value="提交" /> </form> <script type="text/javascript"> function aa(){ var sel=document.getElementById('sel').value; var str=''; for (var i=0;i<=sel-1 ;i++ ) { str+='<input type="file" name="file'+i+'"><p>'; } document.getElementById('div').innerHTML=str; } </script></span>
$upload->maxSize = 3145728 上傳尺寸>exts = array('jpg', 'gif', 'png', 'jpeg','xls');// 設定附件上傳類型// 上傳檔案$info = $upload->upload();
if(!$info){// 上傳錯誤提示錯誤訊息
$this->error($upload->getError());
}else{// 上傳成功
foreach($info as $file){
$ files='./Public/'.$file['savepath'].$file['savename'];
vendor("Excel.PHPExcel");
vendor("Excel.PHPExcel.PHPExcel_IOFactory");
$PHPHPExcel = new PHPExcel();
//var_dump($PHPExcel);
$PHPReader = new PHPExcel_IOFactory();
//var_dump($PHPReader);die;
$xlsPath =$files ;路徑
//$type = 'Excel2007'; //設定要解析的Excel類型Excel5(2003或以下版本)或Excel2007
//echo $xlsPath;
$type = 'Excel5';
$xlsReader = $PHPReader- >createReader($type);
$xlsReader->setReadDataOnly(true);
$xlsReader->setLoadSheetsOnly(true);
$Sheets = $xlsReader->load($xlsPath);
//開始讀取a
/開始讀取a
Sheet = $Sheets->getSheet(0)->toArray(); //讀取第一個工作表(注意編號從0開始) 如果讀取多個可以做一個循環0,1,2,3.. ..
//得到二維數組,每個小數組是excel表格內容的一行裡麵包含此行的每列的資料
//echo '';<br>//print_r($Sheet);<br>foreach ($Sheet as $k=>$v){ <br>$model = M('student');// 取得成功上傳的檔案資訊<br>$data['name']=$v[0];<br>$result= $model->add($data);<br>if($result){<br>$this->success("入庫成功",U('Index/excellist'));<br>}else{<br>$this->error ("入庫失敗");<br>}<br>}<br>}<br>}<br>}<br><br><br><br>