Home  >  Article  >  php教程  >  php excel读取 - red-j

php excel读取 - red-j

WBOY
WBOYOriginal
2016-05-20 10:13:13812browse

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

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

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn