首页 > php教程 > php手册 > php excel读取,phpexcel

php excel读取,phpexcel

WBOY
发布: 2016-06-13 08:40:29
原创
1139 人浏览过

php excel读取,phpexcel

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

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

    </span><span>if</span> (<span>$file_type</span> == 'xlsx'<span>) {
        </span><span>$objReader</span> = PHPExcel_IOFactory::createReader('Excel2007'<span>);
    } </span><span>else</span><span> {
        </span><span>$objReader</span> = PHPExcel_IOFactory::createReader('Excel5'<span>);
    }

    </span><span>$objReader</span>->setReadDataOnly(<span>true</span><span>);

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

    </span><span>return</span> <span>$excelData</span><span>;
}</span>
登录后复制

 

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