PHPExcel
这个神器,可以帮助我们用PHP程序轻松的使用excel
表格。
在此仅提供读取Excel
表格内容到内存的两种方式:
通过坐标获取单元格的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| require_once 'Excel/PHPExcel.php'; require_once 'Excel/PHPExcel/IOFactory.php'; require_once 'Excel/PHPExcel/Reader/Excel5.php';
if($fileExtensions==".xlsx"){ $objReader = \PHPExcel_IOFactory::createReader('Excel2007'); $objReader->setReadDataOnly(true); }else{ $objReader = \PHPExcel_IOFactory::createReader('Excel5'); $objReader->setReadDataOnly(true); }
$objPHPExcel = $objReader->load($filepath); $sheetCount = $objPHPExcel->getSheetCount();
for($i = 0; $i < $sheetCount; $i++) { $sheet = $objPHPExcel->getSheet($i); $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn(); $maxColumns = \PHPExcel_Cell::columnIndexFromString($highestColumn);
for($key = 1; $key <= $highestRow; $key++) { for($k = 0; $k < $maxColumns; $k++) { columnValue = $sheet->getCellByColumnAndRow($k, $key)->getValue(); } } }
|
通过遍历的方式获取单元格的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| require_once 'Excel/PHPExcel.php'; require_once 'Excel/PHPExcel/IOFactory.php'; require_once 'Excel/PHPExcel/Reader/Excel5.php';
if($fileExtensions=="xlsx"){ $objReader = \PHPExcel_IOFactory::createReader('Excel2007'); }else{ $objReader = \PHPExcel_IOFactory::createReader('Excel5'); }
$objPHPExcel = $objReader->load($basepath.$_FILES['file']['name']); $sheetCount = $objPHPExcel->getSheetCount(); for($i = 0; $i < $sheetCount; $i++) { $sheet = $objPHPExcel->getSheet($i); $highestRow = $sheet->getHighestRow(); $highestColumn = $sheet->getHighestColumn();
foreach($sheet->getRowIterator() as $key => $row) { $cellIterator = $row->getCellIterator(); $cellIterator->setIterateOnlyExistingCells(true);
foreach ($cellIterator as $cell) { if($key == 1) { $columnValue = (string)$cell->getValue(); } } }
|
$colums = [];
$goodsAttrRef = [];//excel中的技术参数数据存到内存中