欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

PHPExcel读取excel文件示例

shiping1 的头像

 

下面是自己亲自做的 读取 excel2003表格肯定有用的

<?php
/**
 *
 * @copyright 2007-2012 Xiaoqiang.
 * @author Xiaoqiang.Wu <jamblues@gmail.com>
 * @version 1.01
 */
 
error_reporting(E_ALL);
date_default_timezone_set('Asia/ShangHai');
/** PHPExcel_IOFactory */
require_once './Classes/PHPExcel/IOFactory.php';
$excelFile = 'qq_excel-2003表.xls';
$excelFile = iconv('UTF-8','GBK',$excelFile);
// Check prerequisites
if (!file_exists($excelFile)) {
     exit("not found $excelFile \n");
}
//$reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
//$PHPExcel = $reader->load($excelFile); // 载入excel文件

//$sheet = $PHPExcel->getSheet(0); // 读取第一個工作表

//不用上面的三行代码,有下面的四行代码 ,这样  xls(Excel97-2003工作簿)  xlxs(2007工作簿) 两种格式都能够处理了
//drupal7  中 $excelFile的路径为 public://  开头
//当 xlsx 时,为什么要把这里的 public:// 的路径,就成真正的路径,然后PHPExcel程序才能读取
$filepath =  str_replace('public://upload_assets','sites/default/files/upload_assets',$filepath);

$reader = PHPExcel_IOFactory::createReaderForFile($filepath); //设置以Excel5格式(Excel97-2003工作簿)
$PHPExcel = $reader->load($filepath); // 载入excel文件
$sheet = $PHPExcel->getActiveSheet();  //读取第一个工作表
 



$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumm = $sheet->getHighestColumn(); // 取得总列数
 
/** 循环读取每个单元格的数据 */
for ($row = 1; $row <= $highestRow; $row++)
{//行数是以第1行开始
        for ($column = 'A'; $column <= $highestColumm; $column++) {//列数是以A列开始
            $dataset[] = $sheet->getCell($column.$row)->getValue();
            //var_dump($dataset);
            echo $column.$row.":".$sheet->getCell($column.$row)->getValue()."<br />";
        }    
}
?>



PHPExcel读取excel文件示例

PHPExcel是一个非常方便生成Excel格式文件的类,官方下载包中带有大量如何生成各种样式excel文件的示例,但没有一个读取Excel文件的完整例子.Xiaoqiang根据网上的资料,整理了一份简单读取Excel文件的例子.

 
传统方法:

<?php
/**
 *
 * @copyright 2007-2012 Xiaoqiang.
 * @author Xiaoqiang.Wu <jamblues@gmail.com>
 * @version 1.01
 */
 
error_reporting(E_ALL);
 
date_default_timezone_set('Asia/ShangHai');
 
/** PHPExcel_IOFactory */
require_once '../Classes/PHPExcel/IOFactory.php';
 
 
// Check prerequisites
if (!file_exists("31excel5.xls")) {
	exit("not found 31excel5.xls.\n");
}
 
$reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
$PHPExcel = $reader->load("31excel5.xls"); // 载入excel文件
$sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumm = $sheet->getHighestColumn(); // 取得总列数
$highestColumm= PHPExcel_Cell::columnIndexFromString($colsNum); //字母列转换为数字列 如:AA变为27
 
/** 循环读取每个单元格的数据 */
for ($row = 1; $row <= $highestRow; $row++){//行数是以第1行开始
    for ($column = 0; $column < $highestColumm; $column++) {//列数是以第0列开始
        $columnName = PHPExcel_Cell::stringFromColumnIndex($column);
        echo $columnName.$row.":".$sheet->getCellByColumnAndRow($column, $row)->getValue()."<br />";
    }
}
 
?>

精简方法:

<?php
/**
 *
 * @copyright 2007-2012 Xiaoqiang.
 * @author Xiaoqiang.Wu <jamblues@gmail.com>
 * @version 1.01
 */
 
error_reporting(E_ALL);
 
date_default_timezone_set('Asia/ShangHai');
 
/** PHPExcel_IOFactory */
require_once '../Classes/PHPExcel/IOFactory.php';
 
 
// Check prerequisites
if (!file_exists("31excel5.xls")) {
	exit("not found 31excel5.xls.\n");
}
 
$reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
$PHPExcel = $reader->load("31excel5.xls"); // 载入excel文件
$sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumm = $sheet->getHighestColumn(); // 取得总列数
 
/** 循环读取每个单元格的数据 */
for ($row = 1; $row <= $highestRow; $row++){//行数是以第1行开始
    for ($column = 'A'; $column <= $highestColumm; $column++) {//列数是以A列开始
        $dataset[] = $sheet->getCell($column.$row)->getValue();
        echo $column.$row.":".$sheet->getCell($column.$row)->getValue()."<br />";
    }
}
 
?>

PHPExcel最新版下载:
http://phpexcel.codeplex.com/

本文固定链接: http://extjs.org.cn/fatjames/archives/379 | 吴小强的博客

该日志由 Xiaoqiang.Wu 于2012年03月29日发表在 PHP 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。 
原创文章转载请注明: PHPExcel读取excel文件示例 | 吴小强的博客
关键字: , , 


普通分类: