Home  >  Article  >  Backend Development  >  Example of uploading multiple excel files into the database in tp3.2

Example of uploading multiple excel files into the database in tp3.2

WBOY
WBOYOriginal
2016-08-08 09:25:141126browse

Template page/index.html

<span style="color:#000000;"><meta charset=&#39;utf-8&#39;></meta>
<form action="{:U(&#39;Index/uploads&#39;)}" method="post" enctype="multipart/form-data">
上传<select onchange=&#39;aa()&#39; id=&#39;sel&#39;><?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+=&#39;<input type="file" name="file&#39;+i+&#39;"><p>';
		}
		document.getElementById('div').innerHTML=str;
		}
</script></span>

Click to jump to the control and put the PHPexcel file used into TinkPHP/vendor/Excel/(Excel folder required Create it manually)

public function uploads(){
$upload = new ThinkUpload(); // Instantiate the upload class
$upload->maxSize = 3145728 ; // Set the attachment upload size
$upload ->exts = array('jpg', 'gif', 'png', 'jpeg','xls');//Set attachment upload type
// Upload files
$info = $upload->upload( );
if(!$info){//Upload error message
$this->error($upload->getError());
}else{//Upload successful
foreach($info as $ file){
$files='./Public/'.$file['savepath'].$file['savename'];
vendor("Excel.PHPExcel");
vendor("Excel.PHPExcel.PHPExcel_IOFactory" );
$PHPExcel = new PHPExcel();
//var_dump($PHPExcel);
$PHPReader = new PHPExcel_IOFactory();
//var_dump($PHPReader);die;
$xlsPath =$files; //Specify Exls path to be read
//$type = 'Excel2007'; //Set the Excel type to parse Excel5 (2003 or below) or Excel2007
//echo $xlsPath;
$type = 'Excel5';
$ xlsReader = $PHPReader->createReader($type);
$xlsReader->setReadDataOnly(true);
$xlsReader->setLoadSheetsOnly(true);
$Sheets = $xlsReader->load($xlsPath);
//Start reading a
$Sheet = $Sheets->getSheet(0)->toArray(); //Read the first worksheet (note that the number starts from 0) You can do this if you read multiple A loop of 0,1,2,3....
//Get a two-dimensional array. Each small array is a row of the excel table content, which contains the data of each column of this row
//echo '

' ;
//print_r($Sheet);
foreach($Sheet as $k=>$v){
$model = M('student');// Get successfully uploaded file information
$data['name ']=$v[0];
$result=$model->add($data);
if($result){
$this->success("Successful storage",U('Index/ excellist'));
}else{
$this->error("Storage failed");
}
}
}
}
}

The above introduces the example of uploading multiple excel files and importing into the database in tp3.2, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn