[php]代码库
require_once('./excel/Classes/PHPExcel.php');
//require_once('./excel/Classes/PHPExcel/IOFactory.php');
$inputFileName = './xls/a.xls';
function readxls($inputFileName){
$objReader = new PHPExcel_Reader_Excel5();
$objPHPExcel = $objReader->load($inputFileName);
//print_r($objPHPExcel);
$tmp = array();
$objWorksheet = $objPHPExcel->getActiveSheet();
$i = 0;
foreach($objWorksheet->getRowIterator() as $row){
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach($cellIterator as $cell){
$tmp[$i][] = $cell->getValue();
}
$i++;
}
return $tmp;
}
$con = readxls($inputFileName);
print_r($con);
版本2:
function readxls($inputFileName){
require_once('/www/web/wwwroot/public_html/system/plugins/PHPExcel/PHPExcel.php');
$type = strtolower( pathinfo($inputFileName, PATHINFO_EXTENSION) );
if (!file_exists($inputFileName)) {
die('no file!');
}
//根据不同类型分别操作
if( $type=='xlsx'||$type=='xls' ){
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
}else if( $type=='csv' ){
$objReader = PHPExcel_IOFactory::createReader('CSV')
->setDelimiter(',')
->setInputEncoding('GBK') //不设置将导致中文列内容返回boolean(false)或乱码
->setEnclosure('"')
->setLineEnding("\r\n")
->setSheetIndex(0);
$objPHPExcel = $objReader->load($path);
}else{
die('Not supported file types!');
}
$tmp = array();
$objWorksheet = $objPHPExcel->getActiveSheet();
$i = 0;
foreach($objWorksheet->getRowIterator() as $row){
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach($cellIterator as $cell){
$tmp[$i][] = $cell->getValue();
}
$i++;
}
return $tmp;
}