之前介绍过一些php导出excel 的方法,本节分享通过php导入excel的方法,供大家参考。
例子,phpexcel导入excel文件。
<?php
//载入PHPExcel类
include(dirname(__FILE__).'/phpexcel/PHPExcel.php');
$Obj = new PHPExcel_Reader_Excel5();
$Obj->setReadDataOnly(true);
//读取demo.xls文件
$phpExcel = $Obj->load(dirname(__FILE__).'/output.xls');
//获取当前活动sheet
$objWorksheet = $phpExcel->getActiveSheet();
//获取行数
$highestRow = $objWorksheet->getHighestRow();
//获取列数
//by www.jb200.com
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
//循环输出数据
$data = array();
for($row = 1; $row <= $highestRow; ++$row) {
for($col = 0; $col < $highestColumnIndex; ++$col) {
$val = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
$data[$row][$col] = trim($val);
}
}
echo '<pre>';
print_r($data);
echo '</pre>';