phpExcel操作

下载PHPExcel资源

https://github.com/PHPOffice/PHPExcel

引入PHPExcel资源

include "/lib/PHPExcel/Classes/PHPExcel/IOFactory.php";

构造myExcelUtil类

private $fileName = null;
private $sheet = 0;

/**
* myExcelUtil构造函数,构造参数为文件路径
*
* @param String $fileName
*/
public function __construct($fileName = null)
{
$this->fileName = $fileName;
}

读出一个sheet表

/**
* 打印输出一个sheet表,默认第一个
*
*/
public function readSheet()
{
$this->readSheetBySheet(0);
}

/**
* 打印输出一个sheet表,指定sheet表的索引数字或指定sheet表名
*
* @param String|Int $sheetIndex
*/
public function readSheetBySheet($sheetIndex = 0)
{
//$filename = 'xuehua04.xls';
date_default_timezone_set('PRC');
// 读取excel文件
try {
$inputFileType = PHPExcel_IOFactory::identify($this->fileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($this->fileName);
} catch (Exception $e) {
die("加载文件发生错误:" . pathinfo($this->fileName, PATHINFO_BASENAME) . ":" . $e->getMessage());
}

// 确定要读取的sheet
try {
$sheet = $objPHPExcel->getSheet($sheetIndex);
} catch (PHPExcel_Exception $e) {
}
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();

// 获取一行的数据
for ($row = 1; $row <= $highestRow; $row++) {
// Read a row of data into an array
$rowData = $sheet->rangeToArray("A" . $row . ":" . $highestColumn . $row, NULL, TRUE, FALSE);
//这里得到的rowData都是一行的数据,得到数据后自行处理,我们这里只打出来看看效果
print_r($rowData);
}
}

猜你喜欢

转载自www.cnblogs.com/annanl/p/10888458.html