excel上传--phpExcel读取xls、xlsx

$tmp_file = $_FILES ['excel'] ['tmp_name']; //临时文件
$name = $_FILES['excel']['name']; //上传文件名

根据上传类型做不同处理
if ($file_type == 'xls') {
$reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
}
if ($file_type == 'xlsx') {
$reader = new PHPExcel_Reader_Excel2007();
}

读excel文件
$PHPExcel = $reader->load($tmp_file, 'utf-8'); // 载入excel文件
$sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumm = $sheet->getHighestColumn(); // 取得总列数

把Excel数据保存数组中
$data = array();
for ($rowIndex = 1; $rowIndex <= $highestRow; $rowIndex++) { //循环读取每个单元格的内容。注意行从1开始,列从A开始
for ($colIndex = 'A'; $colIndex <= $highestColumm; $colIndex++) {
$addr = $colIndex . $rowIndex;
$cell = $sheet->getCell($addr)->getValue();
if ($cell instanceof PHPExcel_RichText) { //富文本转换字符串
$cell = $cell->__toString();
}
$data[$rowIndex][$colIndex] = $cell;
}
}
echo $data;
根据需要的数据做处理。。。

原文地址:https://www.cnblogs.com/liyang31/p/6952935.html