phpExcel导入:
1、首先我们tp框架下需要下载phpExcel,解压后放到vendor或其它目录下
2、下方是视图块代码
<form class="form" method="post" action="/index.php?g=index&m=index&a=index" enctype="multipart/form-data">
<table width="100%" border="0" cellspacing="0" cellpadding="0" id="addn">
<tr>
<td height="48" align="right"><strong>导入excel:</strong></td>
<td colspan="3" class="lt">
<input type="file" class="ipt" name="excel">
</td>
</tr>
<tr>
<td colspan="4">
<input class="bginput" type="reset" value="修 改" >
<input class="bginput" type="submit" name="dosubmit" value="添 加">
</td>
</tr>
</table>
</form>
3、下方为PHP端处理
$fileName = $_FILES['excel']['name']; //得到文件全名
$dotArray = explode('.', $fileName); //把文件名安.区分,拆分成数组
$type = end($dotArray);
if ($type != "xls" && $type != "xlsx") {
$this->success('不是Excel文件,请重新上传!');exit;
}
$path = md5(uniqid(rand())) . '.' . $type; //产生随机文件名
//下面必须是tmp_name 因为是从临时文件夹中移动
move_uploaded_file($_FILES['excel']['tmp_name'], $path); //从服务器临时文件拷贝到相应的文件夹下
$file_path = $path;
if (!file_exists($path)) {
$this->success('上传文件丢失!');exit;
}
//文件的扩展名
$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
if ($ext == 'xlsx') {
$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
$objPHPExcel = $objReader->load($file_path, 'utf-8');
} elseif ($ext == 'xls') {
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
$objPHPExcel = $objReader->load($file_path, 'utf-8');
}
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
$ar = array();
$i = 0;
$importRows = 0;
$t1 = microtime(true);
for ($j = 2; $j <= $highestRow; $j++) {
$importRows++;
$arr['title'] = (string)$objPHPExcel->getActiveSheet()->getCell("A$j")->getValue();
$arr['subtitle'] = $arr['title'];
print_r($arr);exit;exit;
}
当然,在我们找不到Excel类的时候下,最好手动引入一下,类似于这样
require_once 'phpexcelLib/PHPExcel.php';
require_once 'phpexcelLib/PHPExcel/IOFactory.php';
require_once 'phpexcelLib/PHPExcel/Reader/Excel5.php';
最后就OK啦!