xlsx文件上传到工作空间,并将数据存入数据库Mysql
controller层
Upload.php
<?php
class Upload extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload()
{
$config['upload_path'] = 'F:/Apache24/htdocs/uploads';
$config['allowed_types'] = 'xlsx|doc|txt|docx';
$config['max_size'] = 1000;
$config['max_width'] = 0; //设置为0表示无限制
$config['max_height'] = 0;
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
// data(), 该方法返回一个数组,包含上传文件的所有信息
// data('file_name') 也可以只返回其中的一项
$uploadResult =$this->upload->data();
echo $uploadResult["full_path"];
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
echo '<br>';
echo dirname(__FILE__);
require_once dirname(__FILE__) . '/../../PHPExcel/Classes/PHPExcel/IOFactory.php';
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load( $uploadResult["full_path"]);
echo "<br>";
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) { // 工作空间
echo 'Worksheet - ' , $worksheet->getTitle() , EOL;
foreach ($worksheet->getRowIterator() as $row) { // 工作空间的 行容器
echo "<br>";
echo ' Row number -> ' , $row->getRowIndex() , EOL;
$cellIterator = $row->getCellIterator(); //行容器中的列容器
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
$arr=array();
foreach ($cellIterator as $cell) { // 每个单元格容器
if (!is_null($cell)) {
echo "<br>";
// echo ' Cell -> ' , $cell->getCoordinate() , '->' , $cell->getCalculatedValue() , EOL; // 单元格的坐标 单元格的值
echo "<br><br>";
$data=$cell->getCalculatedValue();
$arr[]=$data;
}
}
print_r($arr);
$this->load->model("login_model");
$this->login_model->to_sql($arr);
}
}
}
}
}
?>
model层
login_model.php
<?php
class login_model extends CI_Model{
public function __construct()
{$this->load->database();
}
public function to_sql($data){
$colomnArr =array("col1","col2","col3");
if(count($data)<count($colomnArr)){
return;
}
$db_array=array();
//遍历一行的每一列组成一个数组
foreach ($colomnArr as $index=>$item){
$db_array[$colomnArr[$index]] = $data[$index];
}
$this->db->insert("sql",$db_array);
echo $this->db->last_query();
echo '<hr>';
return $this->db->affected_rows();
}
}
view层界面:
upload_form.php
<html>
<head>
<title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<!--上传文件的建议纯手写,不建议使用CI框架的form_open_multipart()-->
<form action="/upload/do_upload" enctype="multipart/form-data" method="post">
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>
upload_success.php
<html>
<head>
</head>
<body>
<h3>Your file was successfully uploaded!</h3>
<ul>
<?php foreach ($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
<p><?php echo anchor('/upload', 'Upload Another File!'); ?></p>
</body>
</html>