首页 > php教程 > php手册 > 正文

php excel读取 - red-j

WBOY
发布: 2016-05-20 10:13:13
原创
814 人浏览过

当然首先要判断是否有文件和文件类型,接着把文件保存到某个路径中

<span style="color: #008000;">/*</span><span style="color: #008000;">*
 * 读取excel数据
 * @author Red
 * @date
 * @param $filename 文件所在路径+文件名
 * @param string $encode
 * @param $file_type
 * @return array
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">function</span> readExcel(<span style="color: #800080;">$filename</span>, <span style="color: #800080;">$encode</span> = 'utf-8', <span style="color: #800080;">$file_type</span><span style="color: #000000;">)
{
    </span><span style="color: #0000ff;">include</span> './plugins/excel/PHPExcel.php'<span style="color: #000000;">;
    </span><span style="color: #0000ff;">include</span> './plugins/excel/PHPExcel/Reader/Excel2007.php'<span style="color: #000000;">;
    </span><span style="color: #0000ff;">include</span> './plugins/excel/PHPExcel/Writer/Excel5.php'<span style="color: #000000;">;
    </span><span style="color: #0000ff;">include</span> './plugins/excel/PHPExcel/Writer/Excel2007.php'<span style="color: #000000;">;

    </span><span style="color: #0000ff;">if</span> (<span style="color: #800080;">$file_type</span> == 'xlsx'<span style="color: #000000;">) {
        </span><span style="color: #800080;">$objReader</span> = PHPExcel_IOFactory::createReader('Excel2007'<span style="color: #000000;">);
    } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
        </span><span style="color: #800080;">$objReader</span> = PHPExcel_IOFactory::createReader('Excel5'<span style="color: #000000;">);
    }

    </span><span style="color: #800080;">$objReader</span>->setReadDataOnly(<span style="color: #0000ff;">true</span><span style="color: #000000;">);

    </span><span style="color: #800080;">$objPHPExcel</span>        = <span style="color: #800080;">$objReader</span>->load(<span style="color: #800080;">$filename</span><span style="color: #000000;">);
    </span><span style="color: #800080;">$objWorksheet</span>       = <span style="color: #800080;">$objPHPExcel</span>-><span style="color: #000000;">getActiveSheet();
    </span><span style="color: #800080;">$hightestrow</span>        = <span style="color: #800080;">$objWorksheet</span>-><span style="color: #000000;">getHighestRow();
    </span><span style="color: #800080;">$highestColumn</span>      = <span style="color: #800080;">$objWorksheet</span>-><span style="color: #000000;">getHighestColumn();
    </span><span style="color: #800080;">$highestColumnIndex</span> = PHPExcel_Cell::columnIndexFromString(<span style="color: #800080;">$highestColumn</span><span style="color: #000000;">);
    </span><span style="color: #800080;">$excelData</span>          = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
   //从第二行开始
    </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$row</span> = 2; <span style="color: #800080;">$row</span> $hightestrow; <span style="color: #800080;">$row</span>++<span style="color: #000000;">) {
        </span><span style="color: #0000ff;">for</span> (<span style="color: #800080;">$col</span> = 0; <span style="color: #800080;">$col</span> $highestColumnIndex; <span style="color: #800080;">$col</span>++<span style="color: #000000;">) {
            </span><span style="color: #800080;">$excelData</span>[<span style="color: #800080;">$row</span>][] = (<span style="color: #0000ff;">string</span>)<span style="color: #800080;">$objWorksheet</span>->getCellByColumnAndRow(<span style="color: #800080;">$col</span>, <span style="color: #800080;">$row</span>)-><span style="color: #000000;">getValue();
        }
    }

    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$excelData</span><span style="color: #000000;">;
}</span>
登录后复制

 

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板