<?php
use PhpOffice\PhpSpreadsheet\IOFactory as PHPExcel_IOFactory;
/**
* 读取excel文件内容
* @param string $filename 完整的文件路径
* @return array 读取表格的结果数据
*/
function readExcelFile($filename)
{
$fileParts = pathinfo($filename);
$filetype = strtolower($fileParts['extension']);
if (strtolower($filetype)=='xls') {
$objReader = PHPExcel_IOFactory::createReader('Xls');
} elseif (strtolower($filetype)=='xlsx') {
$objReader = PHPExcel_IOFactory::createReader('Xlsx');
} elseif (strtolower($filetype)=='csv') {
$objReader = PHPExcel_IOFactory::createReader('Csv')
->setDelimiter(',')
->setInputEncoding('GBK') //处理csv读取中文异常问题
->setEnclosure('"');
}
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // 获取总行数
$highestColumn = $objWorksheet->getHighestColumn();// 获取最大列号
$excelResult = [];
// 从第2行开始读取
$startRow = 2;
for ($j = $startRow; $j <= $highestRow; $j++) {
// 从A列读取数据
for ($k = 'A'; $k <= $highestColumn; $k++) {
// 读取单元格
$excelResult[$j][$k] = (string)$objWorksheet->getCell("$k$j")->getValue();
}
}
return $excelResult;
}