码迷,mamicode.com
首页 > Web开发 > 详细

php excel读取

时间:2016-05-13 10:37:33      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

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

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

    if ($file_type == ‘xlsx‘) {
        $objReader = PHPExcel_IOFactory::createReader(‘Excel2007‘);
    } else {
        $objReader = PHPExcel_IOFactory::createReader(‘Excel5‘);
    }

    $objReader->setReadDataOnly(true);

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

    return $excelData;
}

 

php excel读取

标签:

原文地址:http://www.cnblogs.com/red-j/p/5486158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!