初始化参数,先导入PHPExcel类
/**
* 读出Excel表格数据
* @param $filename 文件名
* @param string $encode 编码格式
* @return array Excel数据
* @throws PHPExcel_Reader_Exception
*/
function read($filename, $encode = 'utf-8')
{
require_once './PHPExcel/IOFactory.php';
$objPHPExcelReader = PHPExcel_IOFactory::load($filename);
$reader = $objPHPExcelReader->getWorksheetIterator();
//循环读取sheet
foreach($reader as $sheet) {
//读取表内容
$content = $sheet->getRowIterator();
//逐行处理
$res_arr = array();
foreach($content as $key => $items) {
$rows = $items->getRowIndex(); //行
$columns = $items->getCellIterator(); //列
$row_arr = array();
//确定从哪一行开始读取
if($rows < 2){
continue;
}
//逐列读取
foreach($columns as $head => $cell) {
//获取cell中数据
$data = $cell->getValue();
$row_arr[] = $data;
}
$res_arr[] = $row_arr;
}
return $res_arr;
}
}
if (!empty($_FILES['file_stu']['name'])) {
$tmp_file = $_FILES ['file_stu'] ['tmp_name'];
$file_types = explode(".", $_FILES ['file_stu'] ['name']);
$file_type = $file_types [count($file_types) - 1];
/*判别是不是.xls文件,判别是不是excel文件*/
if (strtolower($file_type) != "xls") {
echo '不是Excel文件,重新上传';
}
/*设置上传路径*/
$savePath = './excel/';
/*以时间来命名上传的文件*/
$str = date('Ymdhis');
$file_name = $str . "." . $file_type;
/*是否上传成功*/
if (!copy($tmp_file, $savePath . $file_name)) {
echo '上传失败';
}
$res = read($savePath . $file_name);
}else{
echo "读取失败";
}
来源:oschina
链接:https://my.oschina.net/u/4271062/blog/3598870