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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值