- 首页 > it技术 > >
/**
* 读取excel
* @$filename绝对路径
* @return array
*/
function read_excel($filename)
{
try {
vendor('PHPExcel.PHPExcel');
//兼容2003和2007版本
$objReader = PHPExcel_IOFactory::createReader('Excel5');
if (!$objReader->canRead($filename)) {
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
if (!$objReader->canRead($filename)) {
return false;
}
}$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$excelData = https://www.it610.com/article/array();
if ($highestRow> 20001) {
throw new Exception('至多一次导入20000条数据');
}
for ($row = 1;
$row <= $highestRow;
$row++) {
for ($col = 0;
$col < $highestColumnIndex;
$col++) {
$excelData[$row][] = (string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
}
} catch (Exception $e) {
$msg = $e->getLine() . ' ' . $e->getMessage() . ' ' . $e->getFile();
return $msg;
}
return $excelData;
}
推荐阅读