>  기사  >  php教程  >  php excel读取 - red-j

php excel读取 - red-j

WBOY
WBOY원래의
2016-05-20 10:13:13814검색

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

<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>

 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.