excel上传--phpExcel读取xls、xlsx

你。 提交于 2020-01-19 07:51:16
$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;根据需要的数据做处理。。。
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!