PHPExcel - 使用PHP读取excel文件
官网地址:http://phpexcel.codeplex.com/
安装
1. 安装 php_zip 扩展
# pecl install zip
在 php.ini 中添加扩展
extension = "zip.so"
2. 安装 PHPExcel 库文件
# ls
PHPExcel PHPExcel.php
# cp -rf * /usr/local/php/lib/php/PEAR/
PHPExcel PHPExcel.php
# cp -rf * /usr/local/php/lib/php/PEAR/
示例
<?php
error_reporting(E_ALL);
require_once 'PHPExcel/IOFactory.php';
$file = "./example.xlsx";
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($file);
$objPHPExcel->setActiveSheetIndex(0);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
echo $objWorksheet->getCellByColumnAndRow($col, $row)->getCalculatedValue() . ' ';
}
echo "\n";
}
echo "\n";
?>
error_reporting(E_ALL);
require_once 'PHPExcel/IOFactory.php';
$file = "./example.xlsx";
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load($file);
$objPHPExcel->setActiveSheetIndex(0);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
echo $objWorksheet->getCellByColumnAndRow($col, $row)->getCalculatedValue() . ' ';
}
echo "\n";
}
echo "\n";
?>