php使用phpexcel类操作excel文件数据

php使用phpexcel类操作excel文件数据

首先下载phpexcel git地址:https://github.com/PHPOffice/PHPExcel/releases
这里下载了1.8.1.zip

解压之后进入目录,Classes目录,复制PHPExcel.php和PHPExcel文件夹,文件和文件夹要放在同一目录。

读取excel文件

header("Content-type: text/html; charset=utf-8");  
//引入文件
require_once 'phpexcel.php';  
require_once 'PHPExcel\IOFactory.php';  
require_once 'PHPExcel\Reader\Excel2007.php'; 
$data = '1.xls';

$objReader         = PHPExcel_IOFactory::createReader('Excel2007');/*Excel5 for 2003 excel2007 for 2007*/  
$objPHPExcel 	= $objReader->load($data); //Excel 路径  
$sheet 			= $objPHPExcel->getSheet(0);  
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow 	= $sheet->getHighestRow(); // 取得总行数  
$highestColumn 	= $sheet->getHighestColumn(); // 取得总列数 这里拿到的是ABCDF的列
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); 将ABCF转为数字

for ($row = 2;$row <= $highestRow;$row++)
{  
    $strs=array();  

    //注意highestColumnIndex的列数索引从0开始  
    for ($col = 0;$col < $highestColumnIndex;$col++)
    {  
        $strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
        echo $strs[$col].'<br>';
    } 
   // print_r($strs);  
} 

猜你喜欢

转载自blog.csdn.net/wodecc_u/article/details/76676748