本文介绍了phpexcel生成excel文件,以及phpexcel读取excel文件的方法,phpexcel是一个强大的php类库,用来读写不同的文件格式,比如说excel 2007,pdf格式,html格式等。
phpexcel生成与读取excel文件
在php网站开发中经常会遇到报表的生成和读取,csv和excel都是常用的报表格式,csv相对来说比较简单,这里主要介绍用php 来生成和读取excel文件。
首先要引入一个类库:phpexcel,phpexcel是一个强大的php类库,用来读写不同的文件格式,比如说excel 2007,pdf格式,html格式等,这个类库是建立在microsoft’s openxml和php 的基础上的,对excel提供的强大的支持,比如设置工作薄,字体样式,图片以及边框等。
对phpexcel类库不熟悉的朋友,可以阅读下《phpexcel中文帮助手册》中的内容,具体实例大家可以phpexcel快速开发指南中的相关例子。
下面来看下php中使得phpexcel读写excel文件的例子。
首先,phpexcel生成excel文件的方法:
以下代码中函数arraytoexcel的功能:把一个二维数组的数据生成一个excel文件,并且保存在服务器上。
 
复制代码 代码示例:
require_once 'classes/phpexcel/reader/excel2007.php';  
require_once 'classes/phpexcel/reader/excel5.php';  
include 'classes/phpexcel/iofactory.php';  
 
function arraytoexcel($data){  
    $objphpexcel = new phpexcel();  
    $objphpexcel->setactivesheetindex(0);  
    $objphpexcel->getactivesheet()->settitle('firstsheet');  
    $objphpexcel->getdefaultstyle()->getfont()->setname('arial');  
    $objphpexcel->getdefaultstyle()->getfont()->setsize(10);  
    //add data  
 
    $i = 2;  
    foreach ($data as $line){  
        $objphpexcel->getactivesheet()->setcellvalue('a'.$i, $line['from']);          
        $objphpexcel->getactivesheet()->getcell('a'.$i)->setdatatype('n');  
        $objphpexcel->getactivesheet()->setcellvalue('b'.$i, $line['to']);        
        $objphpexcel->getactivesheet()->getcell('b'.$i)->setdatatype('n');  
        $i++;  
    }  
    $objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel5');  
    $file = 'excel.xls';  
    $objwriter->save($file);  
}
 
>>> 更多php教程内容,请关注本站php编程栏目。
如果不希望保存在服务器上,希望生成以后直接下载到客户端,可以在输出文件时添加以下代码,而不使用 $objwriter->save($file);
 
复制代码 代码示例:
header("pragma: public");  
header("expires: 0");  
header("cache-control:must-revalidate, post-check=0, pre-check=0");  
header("content-type:application/force-download");  
header("content-type:application/vnd.ms-execl");  
header("content-type:application/octet-stream");  
header("content-type:application/download");  
header('content-disposition:attachment;filename="excel.xls"');  
header("content-transfer-encoding:binary");  
$objwriter->save('php://output');
下面来看一个读取excel文件内容的实例:
以下代码函数exceltoarray的功能是把一个excel里的内容重新整理放到一个数组了。
 
复制代码 代码示例:
require_once 'classes/phpexcel.php';  
require_once 'classes/phpexcel/iofactory.php';  
function exceltoarray($file){  
    $objreader = phpexcel_iofactory::createreader('excel5');  
    $objreader->setreaddataonly(true);  
    $objphpexcel = $objreader->load($file);  
    $objworksheet = $objphpexcel->getactivesheet();  
    $highestrow = $objworksheet->gethighestrow();   
    $highestcolumn = $objworksheet->gethighestcolumn();   
    $highestcolumnindex = phpexcel_cell::columnindexfromstring($highestcolumn);  
    $exceldata = array();  
    for ($row = 2; $row <= $highestrow; ++$row) {  
        for ($col = 0; $col <= $highestcolumnindex; ++$col) {  
            $exceldata[$row][] = $objworksheet->getcellbycolumnandrow($col, $row)->getvalue();  
        }  
    }  
    return $exceldata;  
}