使用php读取到excel文件中的内容
1.下载PHPExcel类
2.代码:
header("Content-type:text/html;charset=utf-8");
require_once 'PHPExcel/Classes/PHPExcel/IOFactory.php';
$path ='test.xlsx';
$objPHPExcel = PHPExcel_IOFactory::load($path);
$sheet = $objPHPExcel->getSheet(0);
//获取行数与列数,注意列数需要转换
$highestRowNum = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
$highestColumnNum = PHPExcel_Cell::columnIndexFromString($highestColumn); //取得字段,这里测试表格中的第一行为数据的字段,因此先取出用来作后面数组的键名
$filed = array();
for($i=0; $i<$highestColumnNum;$i++){
$cellName = PHPExcel_Cell::stringFromColumnIndex($i).'1';
$cellVal = $sheet->getCell($cellName)->getValue();//取得列内容
$filed []= $cellVal;
} //开始取出数据并存入数组
$data = array();
for($i=2;$i<=$highestRowNum;$i++){
$row = array();
for($j=0; $j<$highestColumnNum;$j++){
$cellName = PHPExcel_Cell::stringFromColumnIndex($j).$i;
$cellVal = $sheet->getCell($cellName)->getValue();
$row[ $filed[$j] ] = $cellVal;
}
$data []= $row;
}
print_r($data);
打印出的$data即为表格中的信息