<?php //首先导入PHPExcel require_once 'PHPExcel.php'; $filePath = "test.xlsx"; //建立reader对象 $PHPReader = new PHPExcel_Reader_Excel2007(); if(!$PHPReader->canRead($filePath)){ $PHPReader = new PHPExcel_Reader_Excel5(); if(!$PHPReader->canRead($filePath)){ echo 'no Excel'; return ; } } //建立excel对象,此时你即可以通过excel对象读取文件,也可以通过它写入文件 $PHPExcel = $PHPReader->load($filePath); /**获取工作表数量*/ $sheetCount = $PHPExcel->getSheetCount(); for($sheetid=0;$sheetid<=$sheetCount-1;$sheetid++){ /**读取excel文件中的第N个工作表*/ $currentSheet = $PHPExcel->getSheet($sheetid); //获取sheet名 $currentSheetName = $currentSheet->getTitle(); /**取得最大的列号*/ $allColumn = $currentSheet->getHighestColumn(); /**取得一共有多少行*/ $allRow = $currentSheet->getHighestRow(); //循环读取每个单元格的内容。注意行从1开始,列从A开始 for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){ $tmpline = NULL; for($colIndex='A';$colIndex<=$allColumn;$colIndex++){ $addr = $colIndex.$rowIndex; $cell = $currentSheet->getCell($addr)->getValue(); if($cell instanceof PHPExcel_RichText) //富文本转换字符串 $cell = $cell->__toString(); $tmpline .= $cell.","; } $tmpline .= "\n"; $tmpline=str_replace(",\n","\n",$tmpline); echo $currentSheetName.",".$tmpline; } } ?>
65 queries in 1.977 seconds |